简体   繁体   English

即使我使用 pip 安装了请求,“没有名为请求的模块”

[英]'No module named requests' even if I installed requests with pip

I'm trying to test if requests module has been well installed.我正在尝试测试请求模块是否安装良好。 But I'm getting the following error :但我收到以下错误:

raceback (most recent call last):
  File "/Users/macbookpro/Desktop/test.py", line 1, in <module>
    import requests
ImportError: No module named requests

when trying to run the following test script:尝试运行以下测试脚本时:

import requests
print 'test'

But I have installed requests with pip, and pip list command gives the following result :但是我已经使用 pip 安装了请求,并且pip list命令给出了以下结果:

MBPdeMacBook2:~ macbookpro$ pip list
arrow (0.7.0)
beautifulsoup4 (4.4.1)
classifier (1.6.5)
coursera-dl (0.6.1)
Django (1.8.6)
html5lib (1.0b8)
keyring (9.0)
lxml (3.6.0)
Pillow (3.4.2)
pip (8.0.2)
pyasn1 (0.1.9)
requests (2.14.2)
setuptools (19.4)
six (1.10.0)
urllib3 (1.16)
vboxapi (1.0)
virtualenv (13.1.2)
wheel (0.26.0)

Why requests isn't being imported ?为什么没有导入请求?

EDIT :编辑 :

MBPdeMacBook2:~ macbookpro$ which python
/Library/Frameworks/Python.framework/Versions/2.7/bin/python
MBPdeMacBook2:~ macbookpro$ which pip
/usr/local/bin/pip
MBPdeMacBook2:~ macbookpro$ python --version
Python 2.7.11
MBPdeMacBook2:~ macbookpro$ pip --version
pip 8.0.2 from /usr/local/lib/python2.7/site-packages (python 2.7)

In general, you should get into the habit of working in a virtualenv .一般来说,你应该养成在virtualenv中工作的习惯。 I find the documentation here to be helpful.我发现这里的文档很有帮助。

If you install all of your dependencies within the virtual environment, you'll be (mostly) sure that you are installing those deps.如果您在虚拟环境中安装所有依赖项,您将(大部分)确定您正在安装这些 deps。 in the same environment that you're running the jobs in.在您运行作业的同一环境中。

For your case, on the command line go to the directory where your code lives and run对于您的情况,在命令行上转到代码所在的目录并运行

pip install virtualenv
virtualenv my_project
source my_project/bin/activate

Now that the virtualenv is active you can现在 virtualenv 处于活动状态,您可以

pip install requests

Only what is installed in the virtualenv will be available.只有安装在 virtualenv 中的才可用。 This will keep your system clean.这将保持您的系统清洁。 Each project should get its own virtualenv, meaning only the dependencies needed for each project will be available to them.每个项目都应该有自己的 virtualenv,这意味着只有每个项目所需的依赖项对它们可用。 This way you could, say, have version 1 of some dependency installed for one project and version 2 for another.通过这种方式,您可以为一个项目安装某个依赖项的版本 1,为另一个项目安装版本 2。 They won't come into conflict.他们不会发生冲突。

After you have installed all the dependencies, run安装所有依赖项后,运行

pip freeze > requirements.txt

To get a list of all the dependencies for the project saved.获取已保存项目的所有依赖项的列表。 Next time you need to install these, you simply run下次您需要安装这些时,您只需运行

pip install -r requirements.txt

Once you are done working in the virtualenv, run在 virtualenv 中完成工作后,运行

deactivate

I am not 100% sure, but the paths from which python and which pip may indicate that you have two versions installed.我不是 100% 确定,但是来自which pythonwhich pip的路径可能表明您安装了两个版本。 The Python version being the old one that was shipped with OS X, and another version. Python 版本是 OS X 附带的旧版本和另一个版本。

I would advice you to install Python27 (or even better Python3) from brew.我建议您从 brew 安装 Python27(甚至更好的 Python3)。

You can install brew with a single command , and another one for installing Python27/3.您可以使用单个命令安装 brew,另一个用于安装 Python27/3。 When this is done you set the PATH variable in your shell rc file and you should be good to go.完成后,您可以在 shell rc 文件中设置PATH变量,然后就可以开始了。

I have Python27 installed (via brew) and my (working environment) reports the following paths:我已经安装了 Python27(通过 brew)并且我的(工作环境)报告了以下路径:

which python: /usr/local/bin/python
which pip: /usr/local/bin/pip

And并且

python --version: 2.7.15
pip --version: pip 9.0.1 from /usr/local/lib/python2.7/site-packages (python2.7)

I experienced this same issue on Ubuntu 18.04 LTS, to check that, I first checked if requests library was installed on my system or not.我在 Ubuntu 18.04 LTS 上遇到了同样的问题,为了检查这一点,我首先检查了我的系统上是否安装了请求库。

Run these commands on your terminal inside the virtual environment in which you working在您工作的虚拟环境中的终端上运行这些命令

$ python

Then the python command line opens, then run然后python命令行打开,然后运行

>>> import requests

After this if you get an ImportError saying, No module named requests, then it means the dependency has not been installed properly.在此之后,如果您收到一个 ImportError 说,No module named requests,则表示该依赖项尚未正确安装。 If there is no such error, then it means the dependency is installed successfully.如果没有这样的错误,则说明依赖安装成功。

This can occur for example if pip is actually pip3 and python is actually python2.7.例如,如果 pip 实际上是 pip3 而 python 实际上是 python2.7,就会发生这种情况。 In your case the which pip and which python eliminate this possibility but it just happened to me.在你的情况下 which pip 和 which python 消除了这种可能性,但它只是发生在我身上。

The solution was to do pip2 instead of pip;解决办法是用pip2代替pip; if the situation was reversed you can use pip3.如果情况逆转,您可以使用 pip3。

Simply go inside your virtual environment and run below commands:只需进入您的虚拟环境并运行以下命令:

1). 1)。 pip install --user pipenv 2). pip install --user pipenv 2)。 pipenv install requests pipenv 安装请求

after executing the above commands cd to your app folder inside the virtual environment and just run it.执行上述命令后cd到虚拟环境中的 app 文件夹并运行它。 Hopefully now it will run.希望现在它会运行。

Reference link: https://python-guide-pt-br.readthedocs.io/pt_BR/latest/dev/virtualenvs.html#make-sure-you-ve-got-python-pip参考链接: https : //python-guide-pt-br.readthedocs.io/pt_BR/latest/dev/virtualenvs.html#make-sure-you-ve-got-python-pip

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ModuleNotFoundError:即使在 pip 在 Pycharm 中安装了请求后,也没有名为“请求”的模块 - ModuleNotFoundError: No module named 'requests' even after pip installed requests in Pycharm “没有模块命名请求”,即使我安装了 pip,请求并将其添加到 PATH - “No Module Named Requests” even though I installed pip, requests, and added it to PATH ImportError:即使已安装模块,也没有名为请求的模块 - ImportError: No module named requests - even though it is installed 错误:即使安装了请求,也没有名为请求的模块 - ERROR : No Module named Requests even after requests is installed 运行 python 可执行文件时没有名为“请求”的模块,即使它已安装 - No module named 'requests' when running python executable, even it's installed Python没有名为“请求”的模块甚至安装请求 - Python no module named 'requests' even install requests ModuleNotFoundError:没有名为“请求”的模块。 即使已经安装了 requests - ModuleNotFoundError: No module named 'requests'. Even though requests has already been installed ModuleNotFoundError:即使在“pip3 install requests-html”和“requests_html”之后也没有名为“requests_html”的模块 - ModuleNotFoundError: No module named 'requests_html' even after 'pip3 install requests-html' and 'requests_html' 使用pip安装后,“ImportError:没有名为&#39;requests&#39;的模块” - “ImportError: no module named 'requests'” after installing with pip ModuleNotFoundError:pip安装后没有名为“请求”的模块 - ModuleNotFoundError: No module named 'requests' after pip install
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM