简体   繁体   English

Flask Gunicorn 找不到应用程序

[英]Flask Gunicorn can't find application

I'm creating an application using Flask.我正在使用 Flask 创建一个应用程序。 Everything works fine when running it with directly with Python, but I want to host it on Heroku, so I need to figure out how to use Gunicorn.直接使用 Python 运行它时一切正常,但我想将它托管在 Heroku 上,所以我需要弄清楚如何使用 Gunicorn。 The problem I'm having is that since app.py isn't in my root folder, it can't be run by simply typing gunicorn app or something like that.我遇到的问题是,由于app.py不在我的根文件夹中,因此无法通过简单地键入gunicorn app或类似的东西来运行它。 My folder structure is the following:我的文件夹结构如下:

.
├── LICENSE
├── myproject
│   ├── app.py
│   ├── static
│   └── templates
├── Procfile
├── README.md
├── requirements.txt

where app.py looks something like this: app.py看起来像这样:

app = Flask(__name__)

@app.route("/")
def index():
    return render_template("index.html")

if __name__ == '__main__':
    app.run(debug=True)

I have tried using Gunicorn to run it in the following ways...我尝试使用 Gunicorn 通过以下方式运行它...

gunicorn myproject app.py

gunicorn myproject app:app

gunicorn myproject/app.py

I have also tried creating an __init__.py in myproject/ , but I have no idea what to put in there to make this work.我也尝试在myproject/创建一个__init__.py ,但我不知道在其中放置什么来完成这项工作。

Try adding to __init__.py the wsgi instance that you want to export. 尝试将要导出的wsgi实例添加到__init__.py

from .app import app

This way the wsgi app is available when importing myproject and you should be able to run your app with gunicorn myproject:app 这样,wsmy app在导入myproject时可用,并且您应该能够使用gunicorn myproject:app运行您的应用gunicorn myproject:app

This is a very late response but I have run in to this issue deploying a flask app.这是一个非常晚的响应,但我在部署烧瓶应用程序时遇到了这个问题。 It seems to be intermittent.好像是断断续续的。

My issue is that even though I'm in my venv gunicorn from the command line seems to be ignoring the venv however if I run gunicorn with the full path in the venv it works我的问题是,即使我在命令行中使用我的 venv gunicorn 似乎忽略了 venv 但是如果我在 venv 中使用完整路径运行 gunicorn 它可以工作

which gunicorn

/home/ubuntu/FieldWorker/venv/bin/gunicorn /home/ubuntu/FieldWorker/venv/bin/gunicorn

/home/ubuntu/FieldWorker/venv/bin/gunicorn
# correctly outputs

[2022-01-10 23:15:52 +0000] [1280] [INFO] Starting gunicorn 20.1.0 [2022-01-10 23:15:52 +0000] [1280] [INFO] Listening at: http://0.0.0.0:8080 (1280) [2022-01-10 23:15:52 +0000] [1280] [INFO] Using worker: sync [2022-01-10 23:15:52 +0000] [1282] [INFO] Booting worker with pid: 1282 [2022-01-10 23:15:52 +0000] [1283] [INFO] Booting worker with pid: 1283 DEBUG:ngoupdate:ngoupdate.py [2022-01-10 23:15:52 +0000] [1280] [INFO] 开始 gunicorn 20.1.0 [2022-01-10 23:15:52 +0000] [1280] [INFO] 收听: http: //0.0.0.0:8080 (1280) [2022-01-10 23:15:52 +0000] [1280] [INFO] 使用工作者:同步 [2022-01-10 23:15:52 +0000] [1282 ] [INFO] 使用 pid 引导工作者:1282 [2022-01-10 23:15:52 +0000] [1283] [INFO] 使用 pid 引导工作者:1283 DEBUG:ngoupdate:ngoupdate.py

Whereas然而

gunicorn

gunicorn -w 2 -b 0.0.0.0:8080 'fieldworker:create_app()' [2022-01-10 23:15:26 +0000] [1275] [INFO] Starting gunicorn 20.0.4 [2022-01-10 23:15:26 +0000] [1275] [INFO] Listening at: http://0.0.0.0:8080 (1275) [2022-01-10 23:15:26 +0000] [1275] [INFO] Using worker: sync [2022-01-10 23:15:26 +0000] [1277] [INFO] Booting worker with pid: 1277 [2022-01-10 23:15:26 +0000] [1277] [ERROR] Exception in worker process ... ModuleNotFoundError: No module named 'flask' gunicorn -w 2 -b 0.0.0.0:8080 'fieldworker:create_app()' [2022-01-10 23:15:26 +0000] [1275] [INFO] 启动 gunicorn 20.0.4 [2022-01-10 23 :15:26 0000] [1275] [INFO]在听力: http://0.0.0.0:8080 (1275)[2022年1月10日23时15分26秒0000] [1275] [INFO]使用工人:同步 [2022-01-10 23:15:26 +0000] [1277] [INFO] 使用 pid 引导工作人员:1277 [2022-01-10 23:15:26 +0000] [1277] [错误]异常工作进程... ModuleNotFoundError:没有名为“flask”的模块

(venv) ubuntu@ip-172-31-1-144:~/prod$ which python3 /home/ubuntu/FieldWorker/venv/bin/python3 (venv) ubuntu@ip-172-31-1-144:~/prod$ which python3 /home/ubuntu/FieldWorker/venv/bin/python3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM