Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
580 views
in Technique[技术] by (71.8m points)

python - Error: gunicorn: Failed to find application object 'app' in 'app'

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...