Flask看不到pip安装的模块

时间:2017-01-29 04:09:37

标签: python apache flask mod-wsgi

我在Python 2.7.12 / Apache 2.4 / Ubuntu 16.04.1 LTS上安装了Flask。我一直在使用these two教程进行设置。我没有运行venv或任何其他虚拟环境(与this post不同)。运行一个简单的Flask应用程序工作正常(一个“hello world”)。我在同一目录(/ var / www / html)中有 flaskapp.wsgi flaskapp.py

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模块集之外导入模块时,例如。 httplib2(我通过pip安装),我收到500 HTTP错误。只需将import httplib2添加到 flaskapp.py 就会抛出此错误。

查看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的导入。

以下是我到目前为止排除故障的方法:

我使用httplib2检查了__file__所在的位置:

>>> import httplib2
>>> httplib2.__file__
'/home/andrew/.local/lib/python2.7/site-packages/httplib2/__init__.pyc'

然后我在Flask app中检查了sys.pathpip.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不可见。 Some posts似乎很接近,但还没有解决我的问题。我真的很感激任何建议!

注意:我对Python非常熟悉,熟悉Flask,但是非常熟悉Apache和WSGI。关于后两个主题(如果适用)的答案的详细解释/链接确实会有所帮助。

1 个答案:

答案 0 :(得分:1)

您应该在Apache配置中将~/.local/添加到python-path。

WSGIDaemonProcess flaskapp threads=5 python-path=/var/www/html/flaskapp/flaskapp:/home/andrew/.local

您还应尝试使用虚拟环境,并将WSGIDaemonProcess上的python-home设置为环境根目录。

来源:http://modwsgi.readthedocs.io/en/develop/configuration-directives/WSGIDaemonProcess.html

修改

您也可以使用httplib2安装sudo,就像安装flask软件包一样(如one of your links所示)