Gunicorn: Failed to find attribute 'app' in 'wsgi' when attempting to start flask server
Asked Answered
C

2

21

Gunicorn fails to start flask server with the error: Failed to find attribute 'app' in 'wsgi'..

The 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__)
...

Project structure:

(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

I checked (Gunicorn can't find app when name changed from "application") but it doesn't relate to mine as I already use the application.

Any ideas what could be the problem?

Clovah answered 8/7, 2020 at 16:17 Comment(2)
What is your shell command to run gunicorn? It's important so as to debug your error. For my case it is the param gunicorn your.wsgi:yourappname where you put :yourappname to be the app instance you create in your codePr
Your gunicorn command to run should be gunicorn wsgi:app instead of gunicorn wsgi I guess - the later one will use default instance name as applicationPr
D
23

Gunicorn is looking for app in wsgi so change

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

to

from app import app as application
app = application

Then you can run do...which is what i am guessing you are doing

gunicorn <all the options here> wsgi:app
Danu answered 29/9, 2020 at 4:9 Comment(0)
C
3

Start gunicorn like this:

gunicorn app:app

The :app tells gunicorn to look for the app variable within app.py

Clew answered 8/7, 2023 at 10:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.