Gunicorn:尝试启动Flask服务器时在'wsgi'中找不到属性'app'。

20
Gunicorn 在启动 Flask 服务器时出现错误:无法在 'wsgi' 中找到属性'app'。 wsgi.py 文件内容:
#!/usr/bin/python3
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/root/customer-account-automation/")

from app import app as application
if __name__ == "__main__":
    application.run()

app.py

#!/usr/bin/python3
from flask import Flask
app = Flask(__name__)
...

项目结构:

(customer-account-automation) root@jsd-user-management:~/customer-account-automation# tree
.
├── Pipfile
├── Pipfile.lock
├── README.md
├── __pycache__
│   ├── app.cpython-37.pyc
│   └── wsgi.cpython-37.pyc
├── app.py
├── customerAccountMgmt.py
├── get-pip.py
├── static
│   ├── app.js
│   ├── bulma.min.css
│   ├── highlight.min.css
│   ├── highlight.min.js
│   └── styles.css
├── templates
│   ├── 404.html
│   ├── base.html
│   ├── change_password.html
│   ├── create_user.html
│   ├── deactivate_user.html
│   └── login.html
└── wsgi.py

我查看了(当应用程序名称从"application"更改后,Gunicorn找不到应用程序),但与我的情况无关,因为我已经使用了application

有什么想法是什么问题?


1
你的shell命令是什么来运行gunicorn?这很重要,以便调试错误。对于我的情况,参数是gunicorn your.wsgi:yourappname,其中你需要将:yourappname替换为你在代码中创建的应用程序实例名称。 - Nam G VU
1
你应该使用 gunicorn wsgi:app 命令来运行,而不是 gunicorn wsgi。我猜测后者会使用默认实例名称作为应用程序。 - Nam G VU
2个回答

22

Gunicorn正在查找WSGI中的app,请进行更改。

from app import app as application
if __name__ == "__main__":
    application.run()

from app import app as application
app = application

那么你可以运行做...我猜这就是你正在做的事情

gunicorn <all the options here> wsgi:app

1

像这样启动gunicorn:

gunicorn app:app
:app 告诉 gunicorn 在 app.py 中寻找 app 变量。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接