Skip to main content
 首页 » 编程设计

python之Flask uWSGI之导入错误 : No module named request

2024年10月17日5lyj

我正在使用 Python 2.7.11::Anaconda 2.4.0 (x86_64)

我正在尝试学习 uWSGI。我能够从本教程开始获得“Hello World”Python uWSGI 应用程序:http://uwsgi-docs.readthedocs.io/en/latest/WSGIquickstart.html#the-first-wsgi-application

现在我想制作一个“Hello World”Flask 应用程序。当我运行时:

uwsgi --socket 127.0.0.1:3031 --wsgi-file app.py --callable app --processes 4 --threads 2 --stats 127.0.0.1:9191 

我收到这个错误:

  File "/Users/JohnsMacBook/anaconda/lib/python2.7/site-packages/werkzeug/http.py", line 28, in <module> 
    from urllib.request import parse_http_list as _parse_list_header 
ImportError: No module named request 

我尝试了 pip install -U Werkzeug 但没有成功。 Werkzeug 文档说:

“Werkzeug 至少需要 Python 2.6 才能正常工作。如果您确实需要支持旧版本,您可以下载旧版本的 Werkzeug,但我们强烈建议不要这样做。Werkzeug 目前对 Python 3 提供实验性支持。 "

我使用的是 Python 2.7.11,所以我不确定为什么 werkzeug 尝试使用 urllib.request 而不是 urllib2。


这是完整的堆栈跟踪:

*** Starting uWSGI 2.0.13.1 (64bit) on [Sun Jun  5 17:31:43 2016] *** 
compiled with version: 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31) on 05 June 2016 16:36:34 
os: Darwin-15.3.0 Darwin Kernel Version 15.3.0: Thu Dec 10 18:40:58 PST 2015; root:xnu-3248.30.4~1/RELEASE_X86_64 
nodename: Johns-MacBook-Pro.local 
machine: x86_64 
clock source: unix 
detected number of CPU cores: 8 
current working directory: /Users/JohnsMacBook/Dropbox/DEV/PyDev/flask-nginx 
detected binary path: /Users/JohnsMacBook/anaconda/bin/uwsgi 
!!! no internal routing support, rebuild with pcre support !!! 
your processes number limit is 709 
your memory page size is 4096 bytes 
detected max file descriptor number: 256 
lock engine: OSX spinlocks 
thunder lock: disabled (you can enable it with --thunder-lock) 
uwsgi socket 0 bound to TCP address 127.0.0.1:3031 fd 3 
Python version: 2.7.10 (default, Oct 23 2015, 18:05:06)  [GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] 
Python main interpreter initialized at 0x7f8531c09740 
python threads support enabled 
your server socket listen backlog is limited to 100 connections 
your mercy for graceful operations on workers is 60 seconds 
mapped 415200 bytes (405 KB) for 8 cores 
*** Operational MODE: preforking+threaded *** 
Traceback (most recent call last): 
  File "app.py", line 1, in <module> 
    from flask import Flask 
  File "/Users/JohnsMacBook/anaconda/lib/python2.7/site-packages/flask/__init__.py", line 17, in <module> 
    from werkzeug.exceptions import abort 
  File "/Users/JohnsMacBook/anaconda/lib/python2.7/site-packages/werkzeug/__init__.py", line 154, in <module> 
    __import__('werkzeug.exceptions') 
  File "/Users/JohnsMacBook/anaconda/lib/python2.7/site-packages/werkzeug/exceptions.py", line 71, in <module> 
    from werkzeug.wrappers import Response 
  File "/Users/JohnsMacBook/anaconda/lib/python2.7/site-packages/werkzeug/wrappers.py", line 26, in <module> 
    from werkzeug.http import HTTP_STATUS_CODES, \ 
  File "/Users/JohnsMacBook/anaconda/lib/python2.7/site-packages/werkzeug/http.py", line 28, in <module> 
    from urllib.request import parse_http_list as _parse_list_header 
ImportError: No module named request 
unable to load app 0 (mountpoint='') (callable not found or import error) 
*** no app loaded. going in full dynamic mode *** 
*** uWSGI is running in multiple interpreter mode *** 
spawned uWSGI master process (pid: 9870) 
spawned uWSGI worker 1 (pid: 9871, cores: 2) 
spawned uWSGI worker 2 (pid: 9872, cores: 2) 
spawned uWSGI worker 3 (pid: 9873, cores: 2) 
spawned uWSGI worker 4 (pid: 9874, cores: 2) 
*** Stats server enabled on 127.0.0.1:9191 fd: 15 *** 

请您参考如下方法:

您的 python 版本似乎有问题。这是来自 werkzeug.http.py 的源代码.如果你有 python 2.x,第一个 import 应该可以工作,第二个应该可以与 python 3 一起工作。

try: 
    from urllib2 import parse_http_list as _parse_list_header 
except ImportError:  # pragma: no cover 
    from urllib.request import parse_http_list as _parse_list_header 

出于某种原因,您的 python 版本既没有 python 2 urllib2 也没有 python 3 urllib。

我对Anaconda python不熟悉,但会不会安装有问题?