错误:gunicorn:找不到应用程序对象' app'在' app'

时间:2018-05-03 14:15:22

标签: python python-3.x gunicorn pipenv

这是我的代码:

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)

然而,当我执行gunicorn命令" gunicorn app:app"我收到这个错误:

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

我在ubuntu 16.04上使用了pipenv whith pipenv shell,但是我也试过了一个docker容器并得到了同样的错误。 这是我的pip文件:

[[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"

我做错了什么?

2 个答案:

答案 0 :(得分:6)

您有一个名为app的文件夹(与文件中的导入行相同)和app.py文件。

Gunicorn将尝试在app模块中找到app WSGI变量,在您的情况下,该变量被标识为app/__init__.py

您需要重命名文件夹或app.py文件以避免此冲突。

答案 1 :(得分:3)

我发现此错误仅发生在20或以上版本的gunicorn。当我降级到19.9.0版时,即使文件夹和app.py共享相同的名称,它也可以正常工作。