Heroku上的Flask配置导入失败

时间:2016-08-23 01:27:45

标签: python heroku flask

我的应用程序在本地运行时运行正常(使用flask run),当app.config.from_object(config.DevelopmentConfig)被注释掉时,它也适用于Heroku。但是,当在Heroku上部署时,gunicorn无法找到配置文件。 (我不能在我的Windows机器上尝试本地的gunicorn。)

__初始化__。PY

from flask import Flask

app = Flask(__name__)
app.config.from_object('config.DevelopmentConfig')

from app import views

Procfile

web: gunicorn app:app

目录结构

我的config.py是项目根目录,虽然我确实在app目录中添加了副本但无济于事。

/ProjectRoot
  config.py
  /app
    __init.py__
    views.py
    /static
    /templates

Heroku日志

删除了带有时间戳的错误日志:

: [2016-08-23 01:18:51 +0000] [9] [INFO] Worker exiting (pid: 9)
:
: Original exception:
:
: Process exited with status 3
:     self.wsgi = self.app.wsgi()
:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/app/base.py", line 67, in wsgi
:     self.callable = self.load()
:     self.load_wsgi()
:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/workers/base.py", line 136, in load_wsgi
:     worker.init_process()
: [2016-08-23 01:18:51 +0000] [10] [ERROR] Exception in worker process
: Traceback (most recent call last):
:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/arbiter.py", line 557, in spawn_worker
:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/workers/base.py", line 126, in init_process
: ImportStringError: import_string() failed for 'config.DevelopmentConfig'. Possible reasons are:
:
:   File "/app/.heroku/python/lib/python2.7/site-packages/werkzeug/utils.py", line 443, in import_string
:     obj = import_string(obj)
: State changed from starting to up
:     app.config.from_object('config.DevelopmentConfig')
:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 65, in load
:     return self.load_wsgiapp()
:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 52, in load_wsgiapp
:     return util.import_app(self.app_uri)
:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/util.py", line 357, in import_app
:     __import__(module)
:   File "/app/app/__init__.py", line 10, in <module>
:   File "/app/.heroku/python/lib/python2.7/site-packages/flask/config.py", line 163, in from_object
:   File "/app/.heroku/python/lib/python2.7/site-packages/werkzeug/utils.py", line 436, in import_string
:     raise ImportError(e)
: - missing __init__.py in a package;
: - package or module path not included in sys.path;
: - duplicated package or module name taking precedence in sys.path;
: - missing module, class, function or variable;
:
: Debugged import:
:
:
: ImportError: 'module' object has no attribute 'DevelopmentConfig'
: Original exception:
: - 'config' found in '/app/config.pyc'.
:
: [2016-08-23 01:18:51 +0000] [10] [INFO] Worker exiting (pid: 10)
: [2016-08-23 01:18:51 +0000] [3] [INFO] Shutting down: Master
: [2016-08-23 01:18:51 +0000] [3] [INFO] Reason: Worker failed to boot.
:     sys.exc_info()[2])
: - 'config.DevelopmentConfig' not found.
: State changed from up to crashed

更新我也更改了config foo,令我惊讶的是,它似乎有效。所以也许 在路径中是重复的。在写一个答案之前,我会再试一试。

1 个答案:

答案 0 :(得分:0)

我将配置文件的名称更改为config_app.py并将文件保留在项目的根目录中,并且在本地和heroku上运行正常。 (不要忘记相应地修改app.config.from_object()。)

我不确定,但似乎错误建议“在sys.path中优先使用重复的包或模块名称;”适用。