Flask uWSGI - ImportError:没有名为request的模块

时间:2016-06-05 22:00:33

标签: python flask uwsgi werkzeug

我正在使用 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 ***

1 个答案:

答案 0 :(得分:2)

似乎有些东西搞砸了你的python版本。这是werkzeug.http.py的源代码。第一个import应该有效,如果你有python 2.x,第二个应该使用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并不熟悉,但可能是因为安装已经搞砸了?