一尘不染

Flask,mod_wsgi和Apache:ImportError

flask

我在我的错误日志中得到了这个:

ImportError:没有名为flask的模块

它看起来完全像在Django + mod_wsgi + apache中:ImportError at /没有名为djproj.urls的模块,但是我尝试了该解决方案,但它似乎不起作用…我正确地将应用程序home和parent文件夹插入到系统路径,但此错误仍然出现。

导航到我的页面仍会弹出500页。

更多信息:我正在使用Amazon EC2免费套餐,并以httpd的形式使用Apache。一切都已正确安装,我敢肯定…

这是我的东西(应用程序名称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

是的,我正在做“如果name ==’main’:app.run()”。

我该如何解决这个问题?


阅读 552

收藏
2020-04-08

共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
2020-04-08