我尝试导入请求:

import requests

但我得到一个错误:

ImportError:没有模块命名请求


当前回答

如果您命中了pip安装请求,并且已经满足了Requirement的输出消息,但仍然会收到错误:ImportError:No module named requests。

当您发现自己处于不同的口译员/虚拟环境中时,可能会发生这种情况。

您可以将模块的路径复制并附加到工作环境中。注意:此路径通常带有消息Requirement already confirmed

在导入请求之前,应该导入sys,然后追加复制的路径。

例子:命令提示符:pip安装请求输出:已满足要求:/usr/local/lib/python3.9/site-packages中的请求

import sys
sys.path.append("/usr/local/lib/python3.9/site-packages")
import requests 

其他回答

如果要在windows上请求导入:

pip install request

然后美容组4:

pip3 install beautifulsoup4

对于windows,只需将路径作为cd和python的“脚本”路径,然后执行命令easy_install.exe请求。然后尝试导入请求。。。

遵循本教程:

$ 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

Python常见安装问题

如果Homebrew在macOS上破坏了您的路径,这些命令也很有用。

python -m pip install requests

or

python3 -m pip install requests

并行安装多个版本的Python?

我已经安装了python2.7和python3.6

打开命令行~/.bash_profile,我发现#Setting PATH for Python 3.6,所以我将路径更改为path=“/usr/local/Cellar/python/2.7.13/bin:${path}”,(请确保您的python2.7路径),然后保存。这对我有用。