问题:ImportError:没有名为请求的模块
每当我尝试导入时,都会出现错误提示No module Named requests
。
import requests
我得到的错误:
File "ex2.py", line 1, in <module>
import requests
ImportError: No module named requests
Whenever I try to import , I get an error saying No module Named requests
.
import requests
The error I get:
File "ex2.py", line 1, in <module>
import requests
ImportError: No module named requests
回答 0
Requests不是内置模块(默认的python安装不附带),因此您必须安装它:
OSX / Linux
如果已安装,请使用$ sudo pip install requests
(或pip3 install requests
用于python3) pip
。如果pip已安装但不在您的路径中,则可以使用python -m pip install requests
(或python3 -m pip install requests
用于python3)
或者,sudo easy_install -U requests
如果已easy_install
安装,也可以使用。
另外,您可以使用系统软件包管理器:
对于centos:yum install python-requests
对于Ubuntu:apt-get install python-requests
视窗
如果已安装Pip.exe并将其添加到Path Environment Variable中,请使用pip install requests
(或pip3 install requests
用于python3) pip
。如果pip已安装但不在您的路径中,则可以使用python -m pip install requests
(或python3 -m pip install requests
用于python3)
或者从命令提示符,使用> Path\easy_install.exe requests
,这里Path
是你的Python*\Scripts
文件夹,如果安装它。(例如:C:\Python32\Scripts
)
如果您要手动将库添加到Windows计算机,则可以下载压缩的库,解压缩它,然后将其放入Lib\site-packages
python路径的文件夹中。(例如:C:\Python27\Lib\site-packages
)
从来源(通用)
对于任何缺少的库,通常可从https://pypi.python.org/pypi/获得该源。您可以在此处下载请求:https://pypi.python.org/pypi/requests
在Mac OS X和Windows上,下载源zip后,解压缩它,并从未python setup.py install
压缩的dir 的termiminal / cmd中运行。
(来源)
Requests is not a built in module (does not come with the default python installation), so you will have to install it:
OSX/Linux
Use $ sudo pip install requests
(or pip3 install requests
for python3) if you have pip
installed. If pip is installed but not in your path you can use python -m pip install requests
(or python3 -m pip install requests
for python3)
Alternatively you can also use sudo easy_install -U requests
if you have easy_install
installed.
Alternatively you can use your systems package manager:
For centos: yum install python-requests
For Ubuntu: apt-get install python-requests
Windows
Use pip install requests
(or pip3 install requests
for python3) if you have pip
installed and Pip.exe added to the Path Environment Variable. If pip is installed but not in your path you can use python -m pip install requests
(or python3 -m pip install requests
for python3)
Alternatively from a cmd prompt, use > Path\easy_install.exe requests
, where Path
is your Python*\Scripts
folder, if it was installed. (For example: C:\Python32\Scripts
)
If you manually want to add a library to a windows machine, you can download the compressed library, uncompress it, and then place it into the Lib\site-packages
folder of your python path. (For example: C:\Python27\Lib\site-packages
)
From Source (Universal)
For any missing library, the source is usually available at https://pypi.python.org/pypi/. You can download requests here: https://pypi.python.org/pypi/requests
On mac osx and windows, after downloading the source zip, uncompress it and from the termiminal/cmd run python setup.py install
from the uncompressed dir.
(source)
回答 1
对我而言,您使用的是哪个版本的Python并不明显。
如果是Python 3,一个解决方案是 sudo pip3 install requests
It’s not obvious to me which version of Python you are using.
If it’s Python 3, a solution would be sudo pip3 install requests
回答 2
要requests
在适用于Python2的Debian / Ubuntu上安装模块:
$ sudo apt-get install python-requests
对于Python3,命令为:
$ sudo apt-get install python3-requests
To install requests
module on Debian/Ubuntu for Python2:
$ sudo apt-get install python-requests
And for Python3 the command is:
$ sudo apt-get install python3-requests
回答 3
如果您使用的是Ubuntu,则需要安装 requests
运行以下命令:
pip install requests
如果遇到权限被拒绝的错误,请在命令前使用sudo:
sudo pip install requests
If you are using Ubuntu, there is need to install requests
run this command:
pip install requests
if you face permission denied error, use sudo before command:
sudo pip install requests
回答 4
这可能为时已晚,但是即使未设置pip path,也可以运行此命令。我正在Windows 10上运行Python 3.7,这是命令
py -m pip install requests
并且您还可以将“ requests”替换为任何其他已卸载的库
This may be a liittle bit too late but this command can be run even when pip path is not set. I am using Python 3.7 running on Windows 10 and this is the command
py -m pip install requests
and you can also replace ‘requests’ with any other uninstalled library
回答 5
在OSX上,该命令将取决于您安装的python的风格。
Python 2.x-默认
sudo pip install requests
Python 3.x
sudo pip3 install requests
On OSX, the command will depend on the flavour of python installation you have.
Python 2.x – Default
sudo pip install requests
Python 3.x
sudo pip3 install requests
回答 6
就我而言,请求已经安装,但需要升级。以下命令可以解决问题
$ sudo pip install requests --upgrade
In my case requests was already installed, but needed an upgrade. The following command did the trick
$ sudo pip install requests --upgrade
回答 7
在Windows打开命令行上
pip3 install requests
On Windows Open Command Line
pip3 install requests
回答 8
回答 9
Brew用户可以使用下面的参考,
安装请求的命令:
python3 -m pip install requests
自制软件和Python
pip是Python的软件包安装程序,您需要该软件包requests
。
Brew users can use reference below,
command to install requests:
python3 -m pip install requests
Homebrew and Python
pip is the package installer for Python and you need the package requests
.
回答 10
Adding Third-party Packages to the Application
Follow this link https://cloud.google.com/appengine/docs/python/tools/libraries27?hl=en#vendoring
step1 : Have a file by named a file named appengine_config.py in the root of your project, then add these lines:
from google.appengine.ext import vendor
Add any libraries installed in the “lib” folder.
vendor.add(‘lib’)
Step 2: create a directory and name it “lib” under root directory of project.
step 3: use pip install -t lib requests
step 4 : deploy to app engine.
回答 11
尝试sudo apt-get install python-requests
。
这对我有用。
Try sudo apt-get install python-requests
.
This worked for me.
回答 12
对于Windows,只需将路径指定为cd,然后将路径指定为python的“脚本”,然后执行命令easy_install.exe
请求即可。然后尝试导入请求…
For windows just give path as cd and path to the “Scripts” of python and then execute the command easy_install.exe
requests.Then try import requests…
回答 13
唯一对我有用的东西:
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py
pip install requests
The only thing that worked for me:
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py
pip install requests
回答 14
在过去的几个月中,我有几次遇到这个问题。我还没有看到针对fedora系统的好的解决方案,因此这里是另一个解决方案。我正在使用RHEL7,发现了以下内容:
如果您urllib3
通过进行了安装pip
,并且requests
通过进行了安装yum
,则即使安装了正确的软件包,也会出现问题。如果您urllib3
通过yum
和requests
安装了,则同样适用pip
。这是我为解决此问题所做的工作:
sudo pip uninstall requests
sudo pip uninstall urllib3
sudo yum remove python-urllib3
sudo yum remove python-requests
(确认已删除所有这些库)
sudo yum install python-urllib3
sudo yum install python-requests
请注意,这仅适用于运行Fedora,Redhat或CentOS的系统。
资料来源:
这个问题(在此答案的评论中)。
这个 github问题。
I have had this issue a couple times in the past few months. I haven’t seen a good solution for fedora systems posted, so here’s yet another solution. I’m using RHEL7, and I discovered the following:
If you have urllib3
installed via pip
, and requests
installed via yum
you will have issues, even if you have the correct packages installed. The same will apply if you have urllib3
installed via yum
, and requests
installed via pip
. Here’s what I did to fix the issue:
sudo pip uninstall requests
sudo pip uninstall urllib3
sudo yum remove python-urllib3
sudo yum remove python-requests
(confirm that all those libraries have been removed)
sudo yum install python-urllib3
sudo yum install python-requests
Just be aware that this will only work for systems that are running Fedora, Redhat, or CentOS.
Sources:
This very question (in the comments to this answer).
This github issue.
回答 15
我已经安装了python2.7和python3.6
打开〜/ .bash_profile的命令行, 我发现#Setting Python 3.6的PATH,所以我将路径更改为PATH =“ / usr / local / Cellar / python / 2.7.13 / bin:$ {PATH}”,(请确保您的python2.7的路径),然后保存。这个对我有用。
I have installed python2.7 and python3.6
Open Command Line to ~/.bash_profile I find that #Setting PATH for Python 3.6 , So I change the path to PATH=”/usr/local/Cellar/python/2.7.13/bin:${PATH}” , (please make sure your python2.7’s path) ,then save. It works for me.
回答 16
如果要request
在Windows上导入:
pip install request
然后beautifulsoup4
用于:
pip3 install beautifulsoup4
if you want request
import on windows:
pip install request
then beautifulsoup4
for:
pip3 install beautifulsoup4
回答 17
我解决了这个问题。您可以尝试这种方法。在此文件“ .bash_profile”中,添加类似alias python=/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7
I solved this problem.You can try this method.
In this file ‘.bash_profile’, Add codes like alias python=/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7
回答 18
如果您将anaconda用作Python软件包管理器,请执行以下操作:
conda install -c anaconda requests
通过pip安装请求对我没有帮助。
If you are using anaconda as your python package manager, execute the following:
conda install -c anaconda requests
Installing requests through pip didn’t help me.
回答 19
您必须确保您的请求模块未安装在最新版本的python中。
使用python 3.7时,请像这样运行python文件:
python3 myfile.py
或使用以下命令进入python交互模式:
python3
是的,这对我有用。像这样运行文件:python3 file.py
You must make sure your requests module is not being installed in a more recent version of python.
When using python 3.7, run your python file like:
python3 myfile.py
or enter python interactive mode with:
python3
Yes, this works for me. Run your file like this: python3 file.py
回答 20
如果Homebrew在macOS上修改了路径,这些命令也很有用。
python -m pip install requests
要么
python3 -m pip install requests
These commands are also useful if Homebrew screws up your path on macOS.
python -m pip install requests
or
python3 -m pip install requests
回答 21
我的答案与@ pi-k基本相同。就我而言,我的程序在本地运行,但无法在质量检查服务器上构建。(我怀疑devops阻止了该软件包的旧版本,而且我的版本肯定已经过时了)我只是决定升级所有内容
$ pip install pip-review
$ pip-review --local --interactive
My answer is basically the same as @pi-k. In my case my program worked locally but failed to build on QA servers. (I suspect devops had older versions of the package blocked and my version must have been too out-of-date) I just decided to upgrade everything
$ pip install pip-review
$ pip-review --local --interactive
回答 22
如果您使用的是anaconda 步骤1:python 步骤2:在管理员模式下打开anaconda提示 步骤3:cd < python path > 步骤4:在此位置安装软件包
If you are using anaconda step 1: where python step 2: open anaconda prompt in administrator mode step 3: cd <python path> step 4: install the package in this location
回答 23
就我而言,这表明请求Requirement已经满足。所以我用。
sudo pip3 install requests
In my case it was showing request Requirement already satisfied . so I use.
sudo pip3 install requests
回答 24
在pycharm IDE中
从菜单中的文件一键设置
2-接下来进行Python解释器
3点子
4-搜索请求并安装
或在终端pycharm中写入此命令
pip install requests
并通过以下方式使用:
import requests
in pycharm IDE
1-go to setting from File in menu
2-next go on Python interpreter
3-click on pip
4- search for requests and install it
or write this order in terminal pycharm
pip install requests
and use it by :
import requests
回答 25
我的问题是我有四个尝试使用的python不同的python库(即使我显式调用了/usr/bin/python
)。一旦我从路径中删除了shell别名和另外两个python,/usr/bin/python
就可以了import requests
。
-HTH
My problem was that I had four different python libraries that python was trying to use (even though I was explicitly calling /usr/bin/python
). Once I removed a shell alias and two other pythons from my path, /usr/bin/python
was able to import requests
.
-HTH
回答 26
问题可能是由于一台计算机具有多个版本的Python。确保要安装所有版本的请求模块。
就我而言,我有python版本2.7
和3.7
。我通过同时安装两个版本的python解决了此问题
The issue could be because of a machine having multiple versions of Python. Make sure that you are installing Request modules in all the versions.
In my case, I had python version 2.7
and 3.7
. I resolved this issue by installing with both versions of python
回答 27
试试这个,我已经安装了anaconda,在阅读了很多文章之后,我发现这是一个解决方法
import sys
print(sys.version)
print("\n \n")
print(sys.path)
sys.path.append('/usr/local/anaconda3/envs/py36/lib/python3.6/site-packages')
在python_version文件夹中提供站点包的路径。
Try this I have anaconda installed and after going through a lot of articles I found this as a fix
import sys
print(sys.version)
print("\n \n")
print(sys.path)
sys.path.append('/usr/local/anaconda3/envs/py36/lib/python3.6/site-packages')
Provide the path of site-packages inside python_version folder.
回答 28
也许您安装了多个版本的python。尝试使用其他版本(例如python3.7 xxx.py
)来确定哪个版本正确。
Maybe you have multiple versions of python installed. Try different versions, such as python3.7 xxx.py
, to identify which one is the right version.
回答 29
您还可以通过首先在目录中找到pip3.exe文件在Windows上使用pip安装:对我说==> cd c:\ python34 \ scripts然后运行==> pip3安装请求
you can also use pip install on windows by first locating the pip3.exe file in the directory: say for me==> cd c:\python34\scripts then run ==> pip3 install requests
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。