我在 Python 2.7.12 / Apache 2.4 / Ubuntu 16.04.1 LTS 上安装了 Flask。我一直在使用这两个教程 (链接1) (链接2) 进行设置。与 (此帖子) 不同,我没有运行 venv 或任何其他虚拟环境。运行简单的 Flask 应用程序正常工作(例如“hello world”)。我在同一个目录(/var/www/html)中有一个 flaskapp.wsgi 和一个 flaskapp.py。
但是当我尝试导入 Flask 基本模块集之外的模块,例如通过 pip 安装的 httplib2,就会出现 500 HTTP 错误。只需在 flaskapp.py 中添加 import httplib2 就会引发此错误。
查看 Apache 错误日志,我看到以下内容:
当我运行Python(不使用Flask)时,我可以轻松地导入httplib2。因此,似乎由于某种原因,我的Flask应用程序无法访问pip的导入。
到目前为止,我已经做了以下排除故障步骤:
我使用__file__检查了httplib2的位置:
因此,它可以看到
我不确定这是否是我的Apache配置问题-我已经查阅了许多教程和文档,并且这是我一直使用的设置:
我浏览了许多SO贴子,但大多数似乎关注的是
flaskapp.wsgi
import sys
sys.path.insert(0, "/var/www/html/flaskapp")
from flaskapp import app as application
flaskapp.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def main():
return "hello world"
if __name__ == '__main__':
app.run()
但是当我尝试导入 Flask 基本模块集之外的模块,例如通过 pip 安装的 httplib2,就会出现 500 HTTP 错误。只需在 flaskapp.py 中添加 import httplib2 就会引发此错误。
查看 Apache 错误日志,我看到以下内容:
[wsgi:error] mod_wsgi (pid=22739): Target WSGI script '/var/www/html/flaskapp/flaskapp.wsgi' cannot be loaded as Python module.
[wsgi:error] mod_wsgi (pid=22739): Exception occurred processing WSGI script '/var/www/html/flaskapp/flaskapp.wsgi'.
[wsgi:error] Traceback (most recent call last):
[wsgi:error] File "/var/www/html/flaskapp/flaskapp.wsgi", line 7, in <module>
[wsgi:error] from flaskapp import app as application
[wsgi:error] File "/var/www/html/flaskapp/flaskapp.py", line 7, in <module>
[wsgi:error] import httplib2
[wsgi:error] ImportError: No module named httplib2
当我运行Python(不使用Flask)时,我可以轻松地导入httplib2。因此,似乎由于某种原因,我的Flask应用程序无法访问pip的导入。
到目前为止,我已经做了以下排除故障步骤:
我使用__file__检查了httplib2的位置:
>>> import httplib2
>>> httplib2.__file__
'/home/andrew/.local/lib/python2.7/site-packages/httplib2/__init__.pyc'
然后我从Flask应用程序内部检查了sys.path
和pip.get_installed_distributions()
:
flaskapp.py(修订版)
from flask import Flask, jsonify
import pip, sys
modules = ""
for i in pip.get_installed_distributions():
modules = modules + " " + str(i)
app = Flask(__name__)
@app.route('/')
def main():
return jsonify({ "modules":modules, "sys.path":sys.path })
if __name__ == '__main__':
app.run()
输出:
{
"available_modules": " Flask 0.12 Jinja2 2.9.5 itsdangerous 0.24 Werkzeug 0.11.15 MarkupSafe 0.23 click 6.7",
"sys.path": [
"/usr/share/python-wheels/lockfile-0.12.2-py2.py3-none-any.whl",
"/usr/share/python-wheels/pkg_resources-0.0.0-py2.py3-none-any.whl",
"/usr/share/python-wheels/retrying-1.3.3-py2.py3-none-any.whl",
"/usr/share/python-wheels/requests-2.9.1-py2.py3-none-any.whl",
"/usr/share/python-wheels/wheel-0.29.0-py2.py3-none-any.whl",
"/usr/share/python-wheels/urllib3-1.13.1-py2.py3-none-any.whl",
"/usr/share/python-wheels/ipaddress-0.0.0-py2.py3-none-any.whl",
"/usr/share/python-wheels/colorama-0.3.7-py2.py3-none-any.whl",
"/usr/share/python-wheels/distlib-0.2.2-py2.py3-none-any.whl",
"/usr/share/python-wheels/pyparsing-2.0.3-py2.py3-none-any.whl",
"/usr/share/python-wheels/packaging-16.6-py2.py3-none-any.whl",
"/usr/share/python-wheels/html5lib-0.999-py2.py3-none-any.whl",
"/usr/share/python-wheels/CacheControl-0.11.5-py2.py3-none-any.whl",
"/usr/share/python-wheels/six-1.10.0-py2.py3-none-any.whl",
"/usr/share/python-wheels/chardet-2.3.0-py2.py3-none-any.whl",
"/usr/share/python-wheels/progress-1.2-py2.py3-none-any.whl",
"/usr/share/python-wheels/setuptools-20.7.0-py2.py3-none-any.whl",
"/usr/share/python-wheels/pip-8.1.1-py2.py3-none-any.whl",
"/var/www/html/flaskapp",
"/usr/lib/python2.7",
"/usr/lib/python2.7/plat-x86_64-linux-gnu",
"/usr/lib/python2.7/lib-tk",
"/usr/lib/python2.7/lib-old",
"/usr/lib/python2.7/lib-dynload",
"/usr/local/lib/python2.7/dist-packages",
"/usr/lib/python2.7/dist-packages"
]
}
因此,它可以看到
httplib2
所在的路径以及flask
的路径,但可用的模块仅为Flask模块。我不确定这是否是我的Apache配置问题-我已经查阅了许多教程和文档,并且这是我一直使用的设置:
/etc/apache2/sites-enabled/000-default.conf
<VirtualHost *:80>
WSGIDaemonProcess flaskapp threads=5 python-path=/var/www/html/flaskapp/flaskapp
WSGIScriptAlias / /var/www/html/flaskapp/flaskapp.wsgi
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory /var/www/html/flaskapp>
WSGIProcessGroup flaskapp
WSGIApplicationGroup %{GLOBAL}
Require all granted
WSGIScriptReloading On
</Directory>
</VirtualHost>
我浏览了许多SO贴子,但大多数似乎关注的是
app
对WSGI不可见。 一些 贴子看起来很接近,但没有解决我的问题。我真的很感谢任何建议!
注意:我对Python非常熟悉,对Flask有所了解,但对Apache和WSGI知之甚少。对于涉及后两个主题的答案的详细说明/链接将非常有帮助。