【问题标题】:Flask app deployment - ImportError: No module named 'app'Flask 应用程序部署 - ImportError:没有名为“app”的模块
【发布时间】:2018-03-26 03:18:43
【问题描述】:

奇怪的一个 - 我已经尝试并试图让这个 Flask 应用程序运行 - 似乎出了点问题。

$ pwd 在 Flask 应用程序中给出:

/home/winduplordvexxos/winduplordvexxos.com/windupflaskvexxos

$ ls 在 Flask 应用程序中给出:

__init__.py  __pycache__  vexxos.py  windupflaskvexxos.wsgi

__init__.py 为空。

vexxos.py

from flask import Flask

app = Flask(__name__)

@app.route('/')
def api():
 return 'Wind Up Lord Vexxos API'

if __name__ == "__main__":
    app.run(host='0.0.0.0')

windupflaskvexxos.wsgi

activate_this = '/home/winduplordvexxos/winduplordvexxos.com/winduplordvexxos_env/bin/activate_this.py'
with open(activate_this) as file_:
    exec(file_.read(), dict(__file__=activate_this))

import os, sys

cwd = os.getcwd()
sys.path.insert(0, cwd)

try:
   from .vexxos import app as application
except Exception: #ImportError
   from vexxos import app as application

但是,我在前端看到的只是:

/etc/apache2/site-available/000-default.conf

<VirtualHost *:80>

        ServerAlias flask.winduplordvexxos.com
        ServerAdmin admin@winduplordvexxos.com

        WSGIDaemonProcess windupflaskvexxos.com user=winduplordvexxos group=winduplordvexxos threads=5
        WSGIScriptAlias / /home/winduplordvexxos/winduplordvexxos.com/windupflaskvexxos/windupflaskvexxos.wsgi

        <Directory /home/winduplordvexxos/winduplordvexxos.com/windupflaskvexxos/>
          WSGIProcessGroup windupflaskvexxos.com
          WSGIApplicationGroup %{GLOBAL}
          WSGIScriptReloading On
          Require all granted
        </Directory>

</VirtualHost>

检查 apache 错误日志给了我:

[Fri Oct 13 23:05:14.650945 2017] [wsgi:error] [pid 15561:tid 140103054321408] [remote 109.149.163.9:59859] mod_wsgi (pid=15561): Target WSGI script '/home/winduplordve$
[Fri Oct 13 23:05:14.651088 2017] [wsgi:error] [pid 15561:tid 140103054321408] [remote 109.149.163.9:59859] mod_wsgi (pid=15561): Exception occurred processing WSGI scr$
[Fri Oct 13 23:05:14.657996 2017] [wsgi:error] [pid 15561:tid 140103054321408] [remote 109.149.163.9:59859] Traceback (most recent call last):
[Fri Oct 13 23:05:14.658060 2017] [wsgi:error] [pid 15561:tid 140103054321408] [remote 109.149.163.9:59859]   File "/home/winduplordvexxos/winduplordvexxos.com/windupfl$
[Fri Oct 13 23:05:14.658071 2017] [wsgi:error] [pid 15561:tid 140103054321408] [remote 109.149.163.9:59859]     from .vexxos import app as application
[Fri Oct 13 23:05:14.658098 2017] [wsgi:error] [pid 15561:tid 140103054321408] [remote 109.149.163.9:59859] SystemError: Parent module '' not loaded, cannot perform rel$
[Fri Oct 13 23:05:14.658131 2017] [wsgi:error] [pid 15561:tid 140103054321408] [remote 109.149.163.9:59859]
[Fri Oct 13 23:05:14.658146 2017] [wsgi:error] [pid 15561:tid 140103054321408] [remote 109.149.163.9:59859] During handling of the above exception, another exception oc$
[Fri Oct 13 23:05:14.658156 2017] [wsgi:error] [pid 15561:tid 140103054321408] [remote 109.149.163.9:59859]
[Fri Oct 13 23:05:14.658180 2017] [wsgi:error] [pid 15561:tid 140103054321408] [remote 109.149.163.9:59859] Traceback (most recent call last):
[Fri Oct 13 23:05:14.658229 2017] [wsgi:error] [pid 15561:tid 140103054321408] [remote 109.149.163.9:59859]   File "/home/winduplordvexxos/winduplordvexxos.com/windupfl$
[Fri Oct 13 23:05:14.658248 2017] [wsgi:error] [pid 15561:tid 140103054321408] [remote 109.149.163.9:59859]     from vexxos import app as application
[Fri Oct 13 23:05:14.658286 2017] [wsgi:error] [pid 15561:tid 140103054321408] [remote 109.149.163.9:59859] ImportError: No module named 'vexxos'
[Fri Oct 13 23:05:14.690252 2017] [wsgi:error] [pid 15561:tid 140103087892224] [remote 109.149.163.9:59858] mod_wsgi (pid=15561): Target WSGI script '/home/winduplordve$
[Fri Oct 13 23:05:14.690318 2017] [wsgi:error] [pid 15561:tid 140103087892224] [remote 109.149.163.9:59858] mod_wsgi (pid=15561): Exception occurred processing WSGI scr$
[Fri Oct 13 23:05:14.690468 2017] [wsgi:error] [pid 15561:tid 140103087892224] [remote 109.149.163.9:59858] Traceback (most recent call last):
[Fri Oct 13 23:05:14.690515 2017] [wsgi:error] [pid 15561:tid 140103087892224] [remote 109.149.163.9:59858]   File "/home/winduplordvexxos/winduplordvexxos.com/windupfl$
[Fri Oct 13 23:05:14.690525 2017] [wsgi:error] [pid 15561:tid 140103087892224] [remote 109.149.163.9:59858]     from .vexxos import app as application
[Fri Oct 13 23:05:14.690549 2017] [wsgi:error] [pid 15561:tid 140103087892224] [remote 109.149.163.9:59858] SystemError: Parent module '' not loaded, cannot perform rel$
[Fri Oct 13 23:05:14.690566 2017] [wsgi:error] [pid 15561:tid 140103087892224] [remote 109.149.163.9:59858]
[Fri Oct 13 23:05:14.690573 2017] [wsgi:error] [pid 15561:tid 140103087892224] [remote 109.149.163.9:59858] During handling of the above exception, another exception oc$
[Fri Oct 13 23:05:14.690577 2017] [wsgi:error] [pid 15561:tid 140103087892224] [remote 109.149.163.9:59858]
[Fri Oct 13 23:05:14.690589 2017] [wsgi:error] [pid 15561:tid 140103087892224] [remote 109.149.163.9:59858] Traceback (most recent call last):
[Fri Oct 13 23:05:14.690610 2017] [wsgi:error] [pid 15561:tid 140103087892224] [remote 109.149.163.9:59858]   File "/home/winduplordvexxos/winduplordvexxos.com/windupfl$
[Fri Oct 13 23:05:14.690619 2017] [wsgi:error] [pid 15561:tid 140103087892224] [remote 109.149.163.9:59858]     from vexxos import app as application
[Fri Oct 13 23:05:14.690637 2017] [wsgi:error] [pid 15561:tid 140103087892224] [remote 109.149.163.9:59858] ImportError: No module named 'vexxos'

我已经尝试了很多东西,除了模块导入等之外尝试,但似乎没有任何工作......有人可以帮忙吗?

【问题讨论】:

    标签: python flask apache2 mod-wsgi apache2.4


    【解决方案1】:

    您可能需要告诉 mod_wsgi 您的代码在哪里使用python-path 选项到WSGIDaemonProcess。您在 WSGI 脚本文件中的导入看起来也有问题。

    对于python-path,您可能想要:

    python-path=/home/winduplordvexxos/winduplordvexxos.com
    

    那么导入应该是:

    from windupflaskvexxos import app as application
    

    或者如果app 没有被__init__.py 拉入,则使用:

    from windupflaskvexxos.vexxos import app as application
    

    还阅读了使用 mod_wsgi 设置虚拟环境的首选方法。您使用的方式不是推荐的方式。

    【讨论】:

    • 天才!这让它工作了 - 供参考:我使用了添加 python-path 的组合(我认为使用来自烧瓶 pocco 文档的虚拟环境:flask.pocoo.org/docs/0.12/deploying/mod_wsgi)就足够了)和from windupflaskvexxos.vexos import app as application。我想我失去了它,因为主网站是一个 Django 应用程序。
    猜你喜欢
    • 2014-05-07
    • 2019-07-20
    • 1970-01-01
    • 1970-01-01
    • 2015-08-20
    • 1970-01-01
    • 2014-02-20
    • 2021-01-08
    • 1970-01-01
    相关资源
    最近更新 更多