简体   繁体   中英

Flask Gunicorn can't find application

I'm creating an application using 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. 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. 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 = 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 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.

Try adding to __init__.py the wsgi instance that you want to export.

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

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

which 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

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'

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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