【问题标题】:Flask uWSGI - ImportError: No module named requestFlask uWSGI - ImportError:没有名为请求的模块
【发布时间】:2016-06-05 22:00:32
【问题描述】:

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

我正在努力学习uWSGI。我可以从本教程开始获得“Hello World”PythonuWSGI 应用程序: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 ***

【问题讨论】:

  • 也许你可以给我们看一些代码?发布一个可验证的例子?而不是将我们指向教程。谢谢
  • 运行pip -V 以检查您使用的python 和pip 版本。我相信urllib.request 只存在于 python 3 中。所以这里似乎有些混乱。如果您的版本错误,您可能需要卸载一些 pip 包并重新安装它们。
  • 你的回溯说Python version: 2.7.10,但在其他地方你说python 2.7.11。好像哪里搞混了?

标签: python flask uwsgi werkzeug


【解决方案1】:

您的 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 不熟悉,但会不会是安装被搞砸了?

【讨论】:

  • 你说得对,一定是在某个地方搞混了。当我运行python --verision 时,我得到Python 2.7.11 :: Anaconda 2.4.0 (x86_64)pip -V 给了我pip 8.1.1 from /Users/JohnsMacBook/anaconda/lib/python2.7/site-packages (python 2.7)。当我进入 Python shell 时,我得到Python 2.7.11 |Anaconda 2.4.0 (x86_64)| (default, Dec 6 2015, 18:57:58)。我不知道 2.7.10 可能来自哪里。
  • @jdesilvio 你找到解决这个问题的方法了吗?我的 Anaconda python 遇到了类似的问题
猜你喜欢
  • 1970-01-01
  • 2014-09-18
  • 2013-06-22
  • 2013-04-22
  • 1970-01-01
  • 2018-09-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多