Python 3.6 Flask with mod_wsgi on Windows : No module named queue

Python 3.6 Flask with mod_wsgi on Windows : No module named queue

我在玩 Flask/Python RESTful api 一切都很好,直到我开始尝试学习如何为它服务。当然我在本地试过了。

我安装了 AMPPS,因为它默认安装并启用了 python 和 mod_wsgi。我完成了所有设置,并且能够让默认的 "Hello World!" 应用程序正常工作。万岁!对吗?

然后我尝试开始引入我的应用程序,这就是我遇到障碍的地方。

起初,我得到一个错误,没有名为 flask 的模块。经过一番阅读后,我了解到我需要像这样加载我的 virtualenv:

activate_this = 'path/to/venv/Scripts/activate_this.py'
with open(activate_this) as file_:
    exec(file_.read(), dict(__file__=activate_this))

这似乎适用于烧瓶,但后来我得到:

ModuleNotFoundError: No module named 'queue'

我搜索了互联网并阅读了有关 "queue" 与 "Queue" 的内容,但我没有直接导入它。

这是我目前拥有的代码。

activate_this = 'path/to/venv/Scripts/activate_this.py'
with open(activate_this) as file_:
    exec(file_.read(), dict(__file__=activate_this))

# this line is what causes the error
from flask import Flask

def application(environ, start_response):
    status = '200 OK'
    output = b'Hello World!'
    response_headers = [('Content-type', 'text/plain'),('Content-Length', str(len(output)))]
    start_response(status, response_headers)
    return [output]

如有任何帮助,我们将不胜感激。

您的 mod_wsgi 实际上是为 Python 2.7 而不是 3.6 编译的。错误是因为Queue模块在3.6中被重命名为queue,所以在2.7下导入queue会失败。

您将需要卸载 mod_wsgi 并安装一个为 Python 3.6 编译的版本。您不能通过将针对一个 Python 版本编译的 mod_wsgi 版本指向不同版本的 Python 虚拟环境,将其强制为 运行 作为另一个版本。这是因为 mod_wsgi 直接链接到特定版本的 Python 库。