我尝试导入请求:

import requests

但我得到一个错误:

ImportError:没有模块命名请求


当前回答

Python常见安装问题

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

python -m pip install requests

or

python3 -m pip install requests

并行安装多个版本的Python?

其他回答

在OSX上,该命令将取决于您的python安装风格。

Python 2.x-默认

sudo pip install requests

Python 3.x

sudo pip3 install requests

您会收到导入错误,因为请求不是内置模块,而是由其他人创建的,您需要安装请求。

在您的终端上使用以下命令,它将正常工作。

pip install requests

安装python请求库,此错误将得到解决。

在Windows Open命令行上

pip3 install requests

我的答案与@pi-k基本相同。在我的案例中,我的程序在本地运行,但未能在QA服务器上构建。(我怀疑devops的包的旧版本被阻止了,我的版本肯定太过时了)我刚刚决定升级所有的东西

$ pip install pip-review
$ pip-review --local --interactive

遵循本教程:

$ 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