我尝试导入请求:
import requests
但我得到一个错误:
ImportError:没有模块命名请求
我尝试导入请求:
import requests
但我得到一个错误:
ImportError:没有模块命名请求
当前回答
我已经安装了python2.7和python3.6
打开命令行~/.bash_profile,我发现#Setting PATH for Python 3.6,所以我将路径更改为path=“/usr/local/Cellar/python/2.7.13/bin:${path}”,(请确保您的python2.7路径),然后保存。这对我有用。
其他回答
在我的案例中,请求已经安装,但需要升级。以下命令成功了
$ sudo pip install requests --upgrade
遵循本教程:
$ 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
尽管我安装了几次“requests”,但还是出现了同样的错误。问题是我在全局Python环境中安装请求,而不是在应用程序虚拟环境中。在虚拟环境中安装请求后,错误消失了。因此,这里有一篇关于如何在应用程序虚拟环境中安装“请求”的好书:虚拟环境和软件包
面对同样的问题,但无法用上述解决方案解决,所以我尝试了这种方法,并成功了:-
卷曲https://bootstrap.pypa.io/pip/2.7/get-pip.py--输出get-pyp.pysudo python2get-pipp.pypython-m pip安装请求
向应用程序添加第三方包
关注此链接https://cloud.google.com/appengine/docs/python/tools/libraries27?hl=en#vendoring
步骤1:在项目的根目录中创建一个名为appengine_config.py的文件,然后添加以下行:
来自google.appengine.ext导入供应商
添加“lib”文件夹中安装的所有库。
供应商.add('lib')
步骤2:在项目的根目录下创建一个目录并将其命名为“lib”。
步骤3:使用pipinstall-t-lib请求
步骤4:部署到应用程序引擎。