【问题标题】:Exception has occurred: ModuleNotFoundError No module named 'requests'发生异常:ModuleNotFoundError 没有名为“请求”的模块
【发布时间】:2019-04-18 10:45:18
【问题描述】:
Exception has occurred: ModuleNotFoundError
No module named 'requests'. 

但请求位于:C:\Users\piotr\Environments\bitcoin_notifications\Lib\site-packages\requests

我输入了 pip install requests 以将它们安装到我的 venv 中。

(bitcoin_notifications) PS C:\Users\piotr> pip install requests
Collecting requests
  Downloading https://files.pythonhosted.org/packages/7d/e3/20f3d364d6c8e5d2353c72a6777857kB)
    100% |████████████████████████████████| 61kB 868kB/s
Collecting certifi>=2017.4.17 (from requests)
  Downloading https://files.pythonhosted.org/packages/60/75/f692a584e85b7eaba0e03827b3d5s/60/75/f692a584e85b7eaba0e03827b3d51f45f571c2e793dd2.py3-none-any.whl (158kB)731e598828d380aa/certifi-2019.3.9-py2.py3-none-any.w2MB/shl (158kB)
    100% |████████████████████████████████| 163kB 2.s/bc/a9/01ffebfb562e4274b6487b4bb1dd2MB/s                                               y3-none-any.whl (133kB)
Collecting chardet<3.1.0,>=3.0.2 (from requests)    5MB/s
  Downloading https://files.pythonhosted.org/packages/bc/a9/01ffebfb562e4274b6487b4bb1ddec7ca55ec7510b22s/14/2c/cd551d81dbe15200be1cf41cd038e4c51f14098443b8/chardet-3.0.4-py2.py3-none-any.whl ne-any.whl (58kB)
(133kB)                                             MB/s
    100% |████████████████████████████████| 143kB 1.5MB/s                                               s/df/1c/59cca3abf96f991f2ec3131a4ffeCollecting idna<2.9,>=2.5 (from requests)
  Downloading https://files.pythonhosted.org/packages/14/2c/cd551d81dbe15200be1cf41cd03869a46fe7226e7450af7a6545bfc474c9/idna-2.8-py2.py3-none-any.whl (58kB)69a46fe7226e7450af7a6545bfc474c9/idna-2.8-py2.py3-none-any.whl (58kB)    100% |████████████████████████████████| 61kB 2.4MB/s

我导入请求:import requests

还有:

Exception has occurred: ModuleNotFoundError
No module named 'requests'
  File "C:\Users\piotr\Documents\bitcoin_notifications.py", line 1, in <module>
    import requests
  File "C:\Users\piotr\AppData\Local\Programs\Python\Python37\Lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Users\piotr\AppData\Local\Programs\Python\Python37\Lib\runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "C:\Users\piotr\AppData\Local\Programs\Python\Python37\Lib\runpy.py", line 263, in run_path
    pkg_name=pkg_name, script_name=fname)

【问题讨论】:

  • 您的请求似乎在virtualenv 中,但错误似乎表明您正在系统python 中尝试import requests。在尝试import requests之前您是否激活了virtualenv
  • 你能告诉我们产生这些错误的命令吗?有很多可能性...
  • PS C:\Users\piotr&gt; Environments\bitcoin_notifications\Scripts\activate (bitcoin_notifications) PS C:\Users\piotr&gt;
  • 当我再次调试时,Python 控制台作为第二个终端打开并且:ppcd : The term 'ppcd' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

标签: python python-3.x python-requests virtualenv python-venv


【解决方案1】:

如前所述,您没有在 pip install 命令之前激活您的 venv。

运行: venv\脚本\激活

来自您的虚拟环境所在的目录。

然后运行 ​​pip install requests

【讨论】:

  • PS C:\Users\piotr&gt; Environments\bitcoin_notifications\Scripts\activate
  • 我运行它并激活了venv:(bitcoin_notifications) PS C:\Users\piotr&gt;
  • 我输入了pip install requests,Powershell 告诉我Requirement already satisfied: requests in c:\users\piotr\environments\bitcoin_notifications\lib\site-packages
  • 这个错误依然出现...Exception has occurred: ModuleNotFoundError No module named 'requests'.
  • 你能发布你的命令和输出吗?每个细节都很重要。这比告诉我们你做了什么更有效
【解决方案2】:

正如其他人所说,您似乎没有正确激活虚拟环境。

但是:由于您使用的是 PowerShell,因此您需要“点源”文件,而不是运行脚本。

请参阅您之前的帖子以获取解决方案: POWERSHELL VENV The module 'bitcoin_notifications' could not be loaded

【讨论】:

    猜你喜欢
    • 2021-04-04
    • 2021-09-12
    • 2022-12-01
    • 2017-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-10
    相关资源
    最近更新 更多