我尝试导入请求:
import requests
但我得到一个错误:
ImportError:没有模块命名请求
我尝试导入请求:
import requests
但我得到一个错误:
ImportError:没有模块命名请求
当前回答
遵循本教程:
$ pipenv install requests
Installing requests...
Adding requests to Pipfile's [packages]...
Installation Succeeded
Pipfile.lock not found, creating...
Locking [dev-packages] dependencies...
Locking [packages] dependencies...
Building requirements...
Resolving dependencies...
Success!
Updated Pipfile.lock (a290a1)!
Installing dependencies from Pipfile.lock (a290a1)...
▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 0/0 — 00:00:00
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.
$ pipenv shell
Launching subshell in virtual environment...
. /home/[user]/.local/share/virtualenvs/[id]/bin/activate
$ python3
Python 3.10.5 (main, Jul 5 2022, 00:20:23) [GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> response = requests.get('https://httpbin.org/ip')
>>> print('Your IP is {0}'.format(response.json()['origin']))
Your IP is xx.xxx.xx.xxx
其他回答
如果要在windows上请求导入:
pip install request
然后美容组4:
pip3 install beautifulsoup4
我已经安装了python2.7和python3.6
打开命令行~/.bash_profile,我发现#Setting PATH for Python 3.6,所以我将路径更改为path=“/usr/local/Cellar/python/2.7.13/bin:${path}”,(请确保您的python2.7路径),然后保存。这对我有用。
如果你使用的是水蟒步骤1:其中python步骤2:以管理员模式打开anaconda提示符步骤3:cd<python路径>步骤4:在此位置安装软件包
尽管我安装了几次“requests”,但还是出现了同样的错误。问题是我在全局Python环境中安装请求,而不是在应用程序虚拟环境中。在虚拟环境中安装请求后,错误消失了。因此,这里有一篇关于如何在应用程序虚拟环境中安装“请求”的好书:虚拟环境和软件包
这可能有点太晚了,但即使未设置pip路径,也可以运行此命令。我使用的是在Windows 10上运行的Python 3.7,这是命令
py -m pip install requests
您还可以用任何其他未安装的库替换“requests”