【问题标题】:Cannot import name 'app' with uWSGI and NGINX无法使用 uWSGI 和 NGINX 导入名称“app”
【发布时间】:2019-05-01 20:36:09
【问题描述】:

我正在尝试在 NGINX 上获取一个 Flask 站点,但在使 Flask 服务器联机时遇到问题。

我遵循了this DigitalOcean 教程,并且我也使用了多个 SO 问题,但没有任何帮助。 如果我运行uwsgi --socket 127.0.0.1:8080 --protocol=http --module wsgi --callable app, 我能得到的唯一有用的回溯是:

Traceback (most recent call last):
  File "./wsgi.py", line 1, in <module>
    from app import app
ImportError: cannot import name 'app'
unable to load app 0 (mountpoint='') (callable not found or import error)

我的目录结构如下:

Xerix/
  __pycache__/
  app/
    __init.py__
    static
    templates
    xerix.db
  cert/
    xerix_me.crt
  flask/ (virtualenv)
  wsgi.py
  xerix.ini

如果我运行journalctl -u xerix,我会得到:

Nov 29 17:43:18 xerix systemd[1]: Started uWSGI instance to serve xerix.
Nov 29 17:43:18 xerix systemd[1]: xerix.service: Main process exited, code=exited, status=203/EXEC
Nov 29 17:43:18 xerix systemd[1]: xerix.service: Failed with result 'exit-code'.

(那是最近的日志)

我该如何解决这个问题,我该怎么办?

我们不胜感激。谢谢!

【问题讨论】:

  • app/__init__.py的内容是什么?
  • @KamilNiski 整个应用程序/网站
  • xerix.ini 和你的 nginx 服务器块的内容是什么?

标签: python flask uwsgi wsgi


【解决方案1】:

你的问题与nginx无关。你必须告诉 uwsgi 在哪里可以找到你的库和依赖项。在 Xerix 文件夹下创建一个myapp.wsgi 文件,内容如下:

# myapp.wsgi
from app import app as application

保存并像这样运行uwsgi:

uwsgi --http :8080 --wsgi-file myapp.wsgi --chdir /path/to/Xerix/ --pythonpath flask/python-3.7.0/lib/python3.7/site-packages

您可能需要根据您的 python 版本稍微调整路径。

【讨论】:

  • 当我运行该命令时得到ImportError: cannot import name 'app'myapp.wsgi 中的所有内容都是您提供的。
  • 在 Xerix 文件夹中运行 python -c 'from app import app' 的输出是什么?
猜你喜欢
  • 2018-04-23
  • 2015-12-24
  • 2019-05-01
  • 1970-01-01
  • 2014-02-01
  • 2014-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多