web2py “无法导入requests”尽管已安装requests
我正在尝试运行一个web2py应用程序,在其中一个控制器里有以下代码。
文件名:default.py
import requests
def index():
...
在我当前激活的虚拟环境中,我已经使用pip安装了requests库,下面是安装的情况。
(web2pyenv) PS C:\Users\rajesh\Desktop\code\RealPython-Part2\web2py> pip install requests
Requirement already satisfied (use --upgrade to upgrade): requests in c:\users\rajesh\desktop\code\realpython-part2\web2py\web2pyenv\lib\site-packages
Cleaning up...
如果我打开一个python命令行,我可以导入requests库并进行一些常规操作。
(web2pyenv) PS C:\Users\rajesh\Desktop\code\RealPython-Part2\web2py> python
Python 2.7.4 (default, Apr 6 2013, 19:54:46) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> requests.get("http://google.com").status_code
200
>>>
但是当我尝试访问我的web2py应用程序时,仍然出现了以下错误。
Traceback (most recent call last):
File "gluon/restricted.py", line 224, in restricted
File "C:/Users/rajesh/Desktop/code/RealPython-Part2/web2py/start/applications/pulse/controllers/default.py", line 1, in <module>
import requests
File "gluon/custom_import.py", line 86, in custom_importer
ImportError: Cannot import module 'requests'
有没有人能告诉我我哪里做错了?
1 个回答
1
你很可能是在使用web2py的Windows二进制版本。这个版本自带了一个Python解释器,所以它不会使用你电脑上安装的Python版本(或者它的任何库)。只要你安装了Python,建议你使用源代码版本的web2py。使用源代码运行的话,你就可以导入任何已安装的模块。