当我在Heroku中部署django应用程序时,我如何在gunicorn中修复erro?

时间:2016-11-17 19:51:11

标签: python django heroku

当我运行git push heroku master时,构建工作成功,但是,应用程序无效...寻找一些问题我打开日志heroku logs并拥有它:

> heroku logs
2016-11-17T16:53:48.005794+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.5/site-packages/gunicorn/app/wsgiapp.py", line 52, in load_wsgiapp
2016-11-17T16:53:48.005794+00:00 app[web.1]:     return util.import_app(self.app_uri)
2016-11-17T16:53:48.005794+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.5/site-packages/gunicorn/util.py", line 357, in import_app
2016-11-17T16:53:48.005795+00:00 app[web.1]:     __import__(module)
2016-11-17T16:53:48.005795+00:00 app[web.1]:   File "/app/myapp/wsgi.py", line 22, in <module>
2016-11-17T16:53:48.005796+00:00 app[web.1]:     application = get_wsgi_application()
2016-11-17T16:53:48.005796+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.5/site-packages/django/core/wsgi.py", line 13, in get_wsgi_application
2016-11-17T16:53:48.005797+00:00 app[web.1]:     django.setup()
2016-11-17T16:53:48.005797+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.5/site-packages/django/__init__.py", line 18, in setup
2016-11-17T16:53:48.005798+00:00 app[web.1]:     apps.populate(settings.INSTALLED_APPS)
2016-11-17T16:53:48.005798+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.5/site-packages/django/apps/registry.py", line 85, in populate
2016-11-17T16:53:48.005798+00:00 app[web.1]:     app_config = AppConfig.create(entry)
2016-11-17T16:53:48.005799+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.5/site-packages/django/apps/config.py", line 90, in create
2016-11-17T16:53:48.005799+00:00 app[web.1]:     module = import_module(entry)
2016-11-17T16:53:48.005800+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.5/importlib/__init__.py", line 126, in import_module
2016-11-17T16:53:48.005800+00:00 app[web.1]:     return _bootstrap._gcd_import(name[level:], package, level)
2016-11-17T16:53:48.005803+00:00 app[web.1]: ImportError: No module named 'base'
2016-11-17T16:53:48.006060+00:00 app[web.1]: [2016-11-17 13:53:48 -0300] [8] [INFO] Worker exiting (pid: 8)
2016-11-17T16:53:48.121161+00:00 app[web.1]: [2016-11-17 13:53:48 -0300] [9] [ERROR] Exception in worker process
2016-11-17T16:53:48.121165+00:00 app[web.1]: Traceback (most recent call last):
2016-11-17T16:53:48.121166+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.5/site-packages/gunicorn/arbiter.py", line 557, in spawn_worker
2016-11-17T16:53:48.121167+00:00 app[web.1]:     worker.init_process()
2016-11-17T16:53:48.121168+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.5/site-packages/gunicorn/workers/base.py", line 126, in init_process
2016-11-17T16:53:48.121169+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.5/site-packages/gunicorn/workers/base.py", line 136, in load_wsgi
2016-11-17T16:53:48.121168+00:00 app[web.1]:     self.load_wsgi()
2016-11-17T16:53:48.121176+00:00 app[web.1]:     self.wsgi = self.app.wsgi()
2016-11-17T16:53:48.121177+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.5/site-packages/gunicorn/app/base.py", line 67, in wsgi
2016-11-17T16:53:48.121189+00:00 app[web.1]:     self.callable = self.load()
2016-11-17T16:53:48.121190+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.5/site-packages/gunicorn/app/wsgiapp.py", line 65, in load
2016-11-17T16:53:48.121191+00:00 app[web.1]:     return self.load_wsgiapp()
2016-11-17T16:53:48.121192+00:00 app[web.1]:     return util.import_app(self.app_uri)
2016-11-17T16:53:48.121192+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.5/site-packages/gunicorn/app/wsgiapp.py", line 52, in load_wsgiapp
2016-11-17T16:53:48.121193+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.5/site-packages/gunicorn/util.py", line 357, in import_app
2016-11-17T16:53:48.121193+00:00 app[web.1]:     __import__(module)
2016-11-17T16:53:48.121194+00:00 app[web.1]:   File "/app/myapp/wsgi.py", line 22, in <module>
2016-11-17T16:53:48.121195+00:00 app[web.1]:     application = get_wsgi_application()
2016-11-17T16:53:48.121196+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.5/site-packages/django/core/wsgi.py", line 13, in get_wsgi_application
2016-11-17T16:53:48.121196+00:00 app[web.1]:     django.setup()
2016-11-17T16:53:48.121197+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.5/site-packages/django/__init__.py", line 18, in setup
2016-11-17T16:53:48.121198+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.5/site-packages/django/apps/registry.py", line 85, in populate
2016-11-17T16:53:48.121197+00:00 app[web.1]:     apps.populate(settings.INSTALLED_APPS)
2016-11-17T16:53:48.121199+00:00 app[web.1]:     app_config = AppConfig.create(entry)
2016-11-17T16:53:48.121200+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.5/site-packages/django/apps/config.py", line 90, in create
2016-11-17T16:53:48.121200+00:00 app[web.1]:     module = import_module(entry)
2016-11-17T16:53:48.121201+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.5/importlib/__init__.py", line 126, in import_module
2016-11-17T16:53:48.121201+00:00 app[web.1]:     return _bootstrap._gcd_import(name[level:], package, level)
2016-11-17T16:53:48.121207+00:00 app[web.1]: ImportError: No module named 'base'
2016-11-17T16:53:48.121584+00:00 app[web.1]: [2016-11-17 13:53:48 -0300] [9] [INFO] Worker exiting (pid: 9)
2016-11-17T16:53:48.192257+00:00 app[web.1]: Traceback (most recent call last):
2016-11-17T16:53:48.192263+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.5/site-packages/gunicorn/arbiter.py", line 196, in run
2016-11-17T16:53:48.192604+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.5/site-packages/gunicorn/arbiter.py", line 346, in sleep
2016-11-17T16:53:48.192600+00:00 app[web.1]:     self.sleep()
2016-11-17T16:53:48.192790+00:00 app[web.1]:     ready = select.select([self.PIPE[0]], [], [], 1.0)
2016-11-17T16:53:48.192792+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.5/site-packages/gunicorn/arbiter.py", line 231, in handle_chld
2016-11-17T16:53:48.192947+00:00 app[web.1]:     self.reap_workers()
2016-11-17T16:53:48.192950+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.5/site-packages/gunicorn/arbiter.py", line 506, in reap_workers
2016-11-17T16:53:48.193196+00:00 app[web.1]:     raise HaltServer(reason, self.WORKER_BOOT_ERROR)
2016-11-17T16:53:48.193244+00:00 app[web.1]: gunicorn.errors.HaltServer: <HaltServer 'Worker failed to boot.' 3>
2016-11-17T16:53:48.193247+00:00 app[web.1]:
2016-11-17T16:53:48.193248+00:00 app[web.1]: During handling of the above exception, another exception occurred:
2016-11-17T16:53:48.193249+00:00 app[web.1]:
2016-11-17T16:53:48.193251+00:00 app[web.1]: Traceback (most recent call last):
2016-11-17T16:53:48.193273+00:00 app[web.1]:   File "/app/.heroku/python/bin/gunicorn", line 11, in <module>
2016-11-17T16:53:48.193397+00:00 app[web.1]:     sys.exit(run())
2016-11-17T16:53:48.193401+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.5/site-packages/gunicorn/app/wsgiapp.py", line 74, in run
2016-11-17T16:53:48.193540+00:00 app[web.1]:     WSGIApplication("%(prog)s [OPTIONS] [APP_MODULE]").run()
2016-11-17T16:53:48.193544+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.5/site-packages/gunicorn/app/base.py", line 192, in run
2016-11-17T16:53:48.193706+00:00 app[web.1]:     super(Application, self).run()
2016-11-17T16:53:48.193710+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.5/site-packages/gunicorn/app/base.py", line 72, in run
2016-11-17T16:53:48.193831+00:00 app[web.1]:     Arbiter(self).run()
2016-11-17T16:53:48.194037+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.5/site-packages/gunicorn/arbiter.py", line 331, in halt
2016-11-17T16:53:48.194003+00:00 app[web.1]:     self.halt(reason=inst.reason, exit_status=inst.exit_status)
2016-11-17T16:53:48.193848+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.5/site-packages/gunicorn/arbiter.py", line 218, in run
2016-11-17T16:53:48.194242+00:00 app[web.1]:     self.stop()
2016-11-17T16:53:48.194266+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.5/site-packages/gunicorn/arbiter.py", line 381, in stop
2016-11-17T16:53:48.194442+00:00 app[web.1]:     time.sleep(0.1)
2016-11-17T16:53:48.194446+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.5/site-packages/gunicorn/arbiter.py", line 231, in handle_chld
2016-11-17T16:53:48.194594+00:00 app[web.1]:     self.reap_workers()
2016-11-17T16:53:48.194598+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.5/site-packages/gunicorn/arbiter.py", line 506, in reap_workers
2016-11-17T16:53:48.194811+00:00 app[web.1]:     raise HaltServer(reason, self.WORKER_BOOT_ERROR)
2016-11-17T16:53:48.194841+00:00 app[web.1]: gunicorn.errors.HaltServer: <HaltServer 'Worker failed to boot.' 3>
2016-11-17T19:16:42.778427+00:00 heroku[slug-compiler]: Slug compilation started
2016-11-17T19:16:42.778433+00:00 heroku[slug-compiler]: Slug compilation finished
2016-11-17T19:16:42.979828+00:00 heroku[web.1]: State changed from crashed to starting
2016-11-17T19:16:50.202921+00:00 heroku[web.1]: Starting process with command `gunicorn myapp.wsgi --log-file -`
2016-11-17T19:16:52.256740+00:00 heroku[web.1]: Process exited with status 127
2016-11-17T19:16:52.245591+00:00 heroku[web.1]: State changed from starting to crashed
2016-11-17T19:16:52.246568+00:00 heroku[web.1]: State changed from crashed to starting
2016-11-17T19:16:52.170203+00:00 app[web.1]: bash: gunicorn: command not found
2016-11-17T19:17:00.048099+00:00 heroku[web.1]: Starting process with command `gunicorn myapp.wsgi --log-file -`
2016-11-17T19:17:02.005321+00:00 heroku[web.1]: Process exited with status 127
2016-11-17T19:17:02.025530+00:00 heroku[web.1]: State changed from starting to crashed
2016-11-17T19:17:01.929591+00:00 app[web.1]: bash: gunicorn: command not found
2016-11-17T19:17:02.796563+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=myapp.herokuapp.com request_id=5a35818e-0f8d-4343-9b86-18b07bd14f59 fwd="186.249.59.170" dyno= connect= service= status=503 bytes=
2016-11-17T19:17:15.898320+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=myapp.herokuapp.com request_id=1354b49b-b9c9-48a1-9917-5d4b5241fdda fwd="186.249.59.170" dyno= connect= service= status=503 bytes=
2016-11-17T19:18:06.649990+00:00 heroku[run.2110]: Awaiting client
2016-11-17T19:18:06.673878+00:00 heroku[run.2110]: Starting process with command `bash`
2016-11-17T19:18:06.905693+00:00 heroku[run.2110]: State changed from starting to up
2016-11-17T19:19:37.129610+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=myapp.herokuapp.com request_id=5922ceca-9f79-46c0-8b05-3fdc86989081 fwd="186.249.59.170" dyno= connect= service= status=503 bytes=
2016-11-17T19:20:00.589411+00:00 heroku[run.2110]: Process exited with status 148
2016-11-17T19:20:00.600754+00:00 heroku[run.2110]: State changed from up to complete

我的Procfile是标准web: gunicorn opportunis.wsgi --log-file -,我的设置是:

settings
|_ __init__.py
|_ locals_settings.py
|_ sever_settings.py

__init__.py我有:

# -*- coding: utf-8 -*-
import os

var = os.getenv('IS_SERVER', 0)

if bool(var):
    try:
        from .server_settings import *
    except ImportError:
        from .locals_settings import *

locals_settingsserver_settings之间的差异是debug offDATABASESdj_database_url的一些配置。在wsgi.py我有这段代码:

import os
var = os.getenv('IS_SERVER', 0)

from django.core.wsgi import get_wsgi_application
from whitenoise.django import DjangoWhiteNoise

if bool(var):
    try:
        os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings.server_settings")
    except ImportError:
        os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings.locals_settings")

application = get_wsgi_application()
application = DjangoWhiteNoise(application)

我做了什么或没有做过失败?

1 个答案:

答案 0 :(得分:1)

看起来你错过了一些gunicorn试图导入的模块。这通常是因为您没有安装所有必要的要求。通常,如果您已有requirements.txt文件,则可以使用以下命令来安装项目的要求:

pip install -r requirements.txt

如果您使用的是虚拟环境,您必须确保gunicorn正在使用您已设置的虚拟环境,这会变得稍微复杂一些。

我刚刚注意到原帖的一些更新。作为一个FYI-永远不会达到这里的除外条款:

if bool(var):
    try:
        os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings.server_settings")
    except ImportError:
        os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings.locals_settings")

setdefault只会设置环境变量,在使用之前不会触发任何导入。