即使我已经安装了'requests'多次,但我仍然遇到了相同的错误。问题在于我是在全局Python环境中安装requests而不是在应用程序虚拟环境中安装。一旦我在虚拟环境中安装了requests,错误就消失了。因此,这里有一个关于如何在应用程序虚拟环境中安装'requests'的好文章:Virtual Environments and Packages
如果你使用Anaconda 步骤1: 确定Python的位置。 步骤2: 以管理员身份打开Anaconda命令提示符。 步骤3: 切换到Python路径。 步骤4: 在此位置安装软件包。
按照本教程所述:
$ 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
pip
或easy_install
安装了requests
库? - Thomas Orozcopython
看不到这些模块。使用python2.7
时,一切都正常。我想我需要清理一下我的环境。 - Sean Novak