简体   繁体   English

错误:gunicorn:无法在“app”中找到应用程序 object“app”

[英]Error: gunicorn: Failed to find application object 'app' in 'app'

Here's my code:这是我的代码:

app.py应用程序.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 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存储库.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:但是,当我执行 gunicorn 命令“gunicorn app: app”时,出现此错误:

 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.我在 ubuntu 16.04 上使用 pipenv whith pipenv shell,但我也尝试了 docker 容器并得到了同样的错误。 here's my pip file:这是我的 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"

What am i doing wrong?我究竟做错了什么?

You have a folder called app (as by the import lines in your file) and a app.py file.您有一个名为app的文件夹(如文件中的导入行)和一个app.py文件。

Gunicorn will try to find the app WSGI variable inside the app module, which in your case is identified as app/__init__.py Gunicorn 将尝试在app模块中查找app WSGI 变量,在您的情况下,该变量被标识为app/__init__.py

You need to rename your folder or your app.py file to avoid this conflict.您需要重命名文件夹或app.py文件以避免此冲突。

I found that this bug only happens on gunicorn version 20+.我发现这个错误只发生在 gunicorn 版本 20+ 上。 When I downgrade to version 19.9.0, it works fine even with the folder and app.py sharing the same name.当我降级到版本 19.9.0 时,即使文件夹和app.py共享相同的名称,它也能正常工作。

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 命令:

gunicorn app

In my case neither renaming the app.py nor downgrading worked.在我的情况下,重命名 app.py 和降级都不起作用。

Following fixed the issue:以下修复了问题:

gunicorn app:app

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:在尝试更改 init 文件和 dockerfile 中的内容的大量试验和错误之后,这就是我们在 gunicorn/conda/Docker 堆栈中的最终结果:

Dockerfile: 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在 app.py 中初始化

server = flask.Flask(__name__)
app = Dash(__name__, suppress_callback_exceptions=True, server=server)
)```

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在“ app” gunicorn中找不到应用程序对象“ app” - Failed to find application object 'app' in 'app' gunicorn Gunicorn:在“运行”中找不到应用程序 object“App” - Gunicorn : Failed to find application object 'App' in 'run' Gunicorn 20 在 'index' 中找不到应用程序 object 'app.server' - Gunicorn 20 failed to find application object 'app.server' in 'index' Heroku 找不到应用程序错误 - Heroku failed to find application app error 当名称从“应用程序”更改时,Gunicorn 无法找到应用程序 - Gunicorn can't find app when name changed from "application" 启动 Flask 应用程序时出错,出现错误“无法找到 Flask 应用程序” - Error launching Flask app with error "Failed to find Flask application" 在 Heroku 上部署 Postgres Flask 应用程序,“gunicorn.errors.AppImportError:无法在 'app' 中找到属性 'app'。” - Deploying Postgres Flask App on Heroku, "gunicorn.errors.AppImportError: Failed to find attribute 'app' in 'app'." 错误:无法在模块“app”中找到 Flask 应用程序或工厂。 使用 'FLASK_APP=app:name' 指定一个 - Error: Failed to find Flask application or factory in module 'app'. Use 'FLASK_APP=app:name' to specify one 使用gunicorn运行应用程序错误 - Using gunicorn to run app error Gunicorn:尝试启动 flask 服务器时无法在 'wsgi' 中找到属性 'app' - Gunicorn: Failed to find attribute 'app' in 'wsgi' when attempting to start flask server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM