Flask、mod_wsgi 和 Apache:ImportError

Posted

技术标签:

【中文标题】Flask、mod_wsgi 和 Apache:ImportError【英文标题】:Flask, mod_wsgi, and Apache: ImportError 【发布时间】:2012-10-31 00:41:53 【问题描述】:

我的错误日志中出现了这个:

ImportError: 没有名为 flask 的模块

它看起来与Django + mod_wsgi + apache: ImportError at / No module named djproj.urls 中的完全一样,但我尝试了该解决方案,但它似乎不起作用...我正在将应用程序主文件夹和父文件夹正确插入系统路径,但此错误仍然出现起来。

导航到我的页面仍然会显示 500 页面。

更多信息:我正在使用Amazon EC2 免费套餐,Apache 采用 httpd 的形式。一切都安装正确,我很确定...

这是我的东西(应用名称 badassery 和应用主文件夹名称 hatemail - 我改变了很多想法):

错误日志

[Sun Nov 11 07:14:45 2012] [error] [client 18.189.71.148] mod_wsgi (pid=28143): Target WSGI script '/home/ec2-user/hatemail/badassery.wsgi' cannot be loaded as Python module.
[Sun Nov 11 07:14:45 2012] [error] [client 18.189.71.148] mod_wsgi (pid=28143): Exception occurred processing WSGI script '/home/ec2-user/hatemail/badassery.wsgi'.
[Sun Nov 11 07:14:45 2012] [error] [client 18.189.71.148] Traceback (most recent call last):
[Sun Nov 11 07:14:45 2012] [error] [client 18.189.71.148]   File "/home/ec2-user/hatemail/badassery.wsgi", line 6, in <module>
[Sun Nov 11 07:14:45 2012] [error] [client 18.189.71.148]     from badassery import app as application
[Sun Nov 11 07:14:45 2012] [error] [client 18.189.71.148]   File "/home/ec2-user/hatemail/badassery.py", line 6, in <module>
[Sun Nov 11 07:14:45 2012] [error] [client 18.189.71.148]     from flask import Flask, request, session, url_for, redirect, render_template, abort, g, flash, _app_ctx_stack
[Sun Nov 11 07:14:45 2012] [error] [client 18.189.71.148] ImportError: No module named flask
[Sun Nov 11 07:14:45 2012] [error] [client 18.189.71.148] mod_wsgi (pid=28143): Target WSGI script '/home/ec2-user/hatemail/badassery.wsgi' cannot be loaded as Python module.
[Sun Nov 11 07:14:45 2012] [error] [client 18.189.71.148] mod_wsgi (pid=28143): Exception occurred processing WSGI script '/home/ec2-user/hatemail/badassery.wsgi'.
[Sun Nov 11 07:14:45 2012] [error] [client 18.189.71.148] Traceback (most recent call last):
[Sun Nov 11 07:14:45 2012] [error] [client 18.189.71.148]   File "/home/ec2-user/hatemail/badassery.wsgi", line 6, in <module>
[Sun Nov 11 07:14:45 2012] [error] [client 18.189.71.148]     from badassery import app as application
[Sun Nov 11 07:14:45 2012] [error] [client 18.189.71.148]   File "/home/ec2-user/hatemail/badassery.py", line 6, in <module>
[Sun Nov 11 07:14:45 2012] [error] [client 18.189.71.148]     from flask import Flask, request, session, url_for, redirect, render_template, abort, g, flash, _app_ctx_stack
[Sun Nov 11 07:14:45 2012] [error] [client 18.189.71.148] ImportError: No module named flask

badassery.wsgi

import sys

sys.path.insert(0,'/home/ec2-user/hatemail')
sys.path.insert(1,'/home/ec2-user')

from badassery import app as application

对 httpd.conf 文件的补充

WSGISocketPrefix /var/run/wsgi

<VirtualHost *:80>
        ServerName 54.243.61.41
        DocumentRoot "/home/ec2-user/hatemail"

        WSGIDaemonProcess badassery user=apache group=apache processes=1 threads=5
        WSGIScriptAlias / /home/ec2-user/hatemail/badassery.wsgi
        WSGIScriptReloading On

        <Directory /home/ec2-user/hatemail>
                WSGIProcessGroup badassery
                WSGIApplicationGroup %GLOBAL
                Order deny,allow
                Allow from all
        </Directory>
</VirtualHost>

目录结构

~
    hatemail
        badassery.py
        badassery.wsgi
        Procfile
        README
        requirements.txt
        schema.sql
        static/
        templates/
        venv/bin/activate

是的,我正在执行“if name == 'main': app.run()”。

我该如何解决这个问题?

【问题讨论】:

Apache 用户可以读取这些文件吗? 你能运行一个交互式 Python shell 并输入:import flask 你看到了什么? 【参考方案1】:

如果您要部署 virtualenv,则必须确保先激活它。您应该像这样更新您的 wsgi 文件(更新值以匹配您的环境):

activate_this = '/path/to/virtualenv/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this)

import sys
sys.path.insert(0, '/path/to/flask/appname')
sys.path.insert(0,'/home/ec2-user/hatemail')
sys.path.insert(1,'/home/ec2-user')

from badassery import app as application

如果您没有部署 virtualenv,您可能只需要确保您的系统上安装了 Flask。如果您已经安装了setuptools,您可以:

easy_install flask

【讨论】:

【参考方案2】:

如果您在使用wsgi_mod 时遇到任何导入问题,请确保您尝试明确指向导致问题的导入文件:

sys.path.append('/home/foo/www/Forms')

然后进行导入!

【讨论】:

以上是关于Flask、mod_wsgi 和 Apache:ImportError的主要内容,如果未能解决你的问题,请参考以下文章

初始化使用 Apache 和 mod_wsgi 运行的 Flask 应用程序

在 linux 中使用 apache 和 mod_wsgi 配置 Python flask 应用程序

使用 mod_wsgi 和 WinSCP 在 Apache 服务器上部署 Flask 应用程序

apache+mod_wsgi+flask 环境搭建

如何部署Apache2+mod_wsgi+flask?

flask+mod_wsgi+apache在windows上的布署