Error: gunicorn: Failed to find application object 'app' in 'app'
Asked Answered
H

4

16

Here's my code:

app.py

from flask_graphql import GraphQLView
from app.infrastructure.graphql import schema
from app.infrastructure.api_resource import app

app.add_url_rule('/graphql', view_func=GraphQLView.as_view('graphql', schema=schema, graphiql=True))

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

api_resource.py

import app.infrastructure.repository as repository
from flask import request, url_for
from flask_restplus import Api, Resource, fields
from sqlalchemy_pagination import paginate
from sqlalchemy_fulltext import FullTextSearch

app = repository.app
api = Api(app, version='0.1', title='xxxxx',
          description='xxxxx')
...

repository.py

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from app.domain.model import Base

connection_string = 'xxxxxx'

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = connection_string
app.config['SQLALCHEMY_ECHO'] = True
db = SQLAlchemy(app, metadata=Base.metadata)

However, when i execute the gunicorn command "gunicorn app: app" i get this error:

Failed to find application object 'app' in 'app'

I'm using pipenv whith pipenv shell on ubuntu 16.04, but i've also tried on a docker container and got the same error. here's my pip file:

[[source]]
url = "https://pypi.python.org/simple"
verify_ssl = true
name = "pypi"

[dev-packages]

[packages]
flask-graphql = "*"
flask-sqlalchemy = "*"
sqlalchemy-fulltext-search = "*"
graphene-sqlalchemy = ">=2.0"
flask-marshmallow = "*"
sqlalchemy-pagination = "*"
flask-restplus = "*"
requests = "*"
mysqlclient = "*"
gunicorn = "*"

[requires]
python_version = "3.6"

What am i doing wrong?

Hindrance answered 3/5, 2018 at 14:15 Comment(1)
from app.infrastructure.api_resource import app needs to be named differently if you are trying to run that within app.pyMerwin
P
16

You have a folder called app (as by the import lines in your file) and a app.py file.

Gunicorn will try to find the app WSGI variable inside the app module, which in your case is identified as app/__init__.py

You need to rename your folder or your app.py file to avoid this conflict.

Poor answered 3/5, 2018 at 14:23 Comment(0)
T
4

I found that this bug only happens on gunicorn version 20+. When I downgrade to version 19.9.0, it works fine even with the folder and app.py sharing the same name.

Tollgate answered 11/11, 2019 at 22:44 Comment(1)
Thanks, man. I found the report for the issue but you saved me a lot of time :)Vastha
C
0

Accepted answers might be spot on for the original question , however I got the same error and I was running the gunicorn command as :

gunicorn app

In my case neither renaming the app.py nor downgrading worked.

Following fixed the issue:

gunicorn app:app
Craftsman answered 14/6, 2022 at 21:19 Comment(0)
V
0

After a lot of trial and error trying to change things in init files and the dockerfile, this is what we ended up with, in our gunicorn/conda/Docker stack:

Dockerfile:

FROM continuumio/miniconda3

WORKDIR /app

# create environment
COPY environment.yml /app
RUN conda env create --name my_app_env --file environment.yml

# ensure RUN commands use the new environment
SHELL ["conda", "run", "-n", "my_app_env ", "/bin/bash", "-c"]

COPY . .

EXPOSE 8123

ENTRYPOINT [\
    "conda", "run", "--no-capture-output", "-n", "my_app_env ",\
    "gunicorn", "-b", "0.0.0.0:8123",\
    "--pythonpath", "src/MyApp",\
    "src.MyApp.app:server"\
]

Dir structure:

MyApp
|- src
   |- MyApp
      - __init__.py  (file is empty for now)
      - app.py
- Dockerfile
- environment.yml
- setup.py

initialisation in app.py

server = flask.Flask(__name__)
app = Dash(__name__, suppress_callback_exceptions=True, server=server)
)```
Vomitory answered 11/10, 2022 at 7:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.