Gunicorn / Procfile - 找不到module.wsgi

Posted

技术标签:

【中文标题】Gunicorn / Procfile - 找不到module.wsgi【英文标题】:Gunicorn / Procfile - module.wsgi not found 【发布时间】:2021-07-24 18:52:23 【问题描述】:

我正在尝试将 django 应用程序部署到 heroku。

我的目录结构是:

'root'/
  Procfile
  backend/
    django_app/
      manage.py
      mainapp/
        settings.py
        wsgi.py      

我尝试按照 *** 上的一些帖子中给出的指示进行操作,但无济于事。

我在根目录中有一个 Procfile。最新版本有:

web: gunicorn backend.django_app.mainapp.wsgi --log-file -

但它仍然给我一个找不到模块的错误:

2021-05-01T20:20:33.556737+00:00 heroku[web.1]: State changed from crashed to starting
2021-05-01T20:20:58.799412+00:00 heroku[web.1]: Starting process with command `gunicorn backend.django_app.mainapp.wsgi --log-file -`
2021-05-01T20:21:01.738041+00:00 app[web.1]: [2021-05-01 20:21:01 +0000] [4] [INFO] Starting gunicorn 20.0.4
2021-05-01T20:21:01.739021+00:00 app[web.1]: [2021-05-01 20:21:01 +0000] [4] [INFO] Listening at: http://0.0.0.0:46010 (4)
2021-05-01T20:21:01.739198+00:00 app[web.1]: [2021-05-01 20:21:01 +0000] [4] [INFO] Using worker: sync
2021-05-01T20:21:01.744319+00:00 app[web.1]: [2021-05-01 20:21:01 +0000] [8] [INFO] Booting worker with pid: 8
2021-05-01T20:21:02.007630+00:00 app[web.1]: [2021-05-01 20:21:02 +0000] [8] [ERROR] Exception in worker process
2021-05-01T20:21:02.007644+00:00 app[web.1]: Traceback (most recent call last):
2021-05-01T20:21:02.007645+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
2021-05-01T20:21:02.007645+00:00 app[web.1]: worker.init_process()
2021-05-01T20:21:02.007646+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/workers/base.py", line 119, in init_process
2021-05-01T20:21:02.007646+00:00 app[web.1]: self.load_wsgi()
2021-05-01T20:21:02.007647+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/workers/base.py", line 144, in load_wsgi
2021-05-01T20:21:02.007648+00:00 app[web.1]: self.wsgi = self.app.wsgi()
2021-05-01T20:21:02.007648+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/app/base.py", line 67, in wsgi
2021-05-01T20:21:02.007648+00:00 app[web.1]: self.callable = self.load()
2021-05-01T20:21:02.007649+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/app/wsgiapp.py", line 49, in load
2021-05-01T20:21:02.007649+00:00 app[web.1]: return self.load_wsgiapp()
2021-05-01T20:21:02.007649+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/app/wsgiapp.py", line 39, in load_wsgiapp
2021-05-01T20:21:02.007650+00:00 app[web.1]: return util.import_app(self.app_uri)
2021-05-01T20:21:02.007650+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/util.py", line 358, in import_app
2021-05-01T20:21:02.007650+00:00 app[web.1]: mod = importlib.import_module(module)
2021-05-01T20:21:02.007651+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/importlib/__init__.py", line 127, in import_module
2021-05-01T20:21:02.007651+00:00 app[web.1]: return _bootstrap._gcd_import(name[level:], package, level)
2021-05-01T20:21:02.007651+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
2021-05-01T20:21:02.007652+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 991, in _find_and_load
2021-05-01T20:21:02.007652+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
2021-05-01T20:21:02.007652+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
2021-05-01T20:21:02.007652+00:00 app[web.1]: File "<frozen importlib._bootstrap_external>", line 783, in exec_module
2021-05-01T20:21:02.007653+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
2021-05-01T20:21:02.007653+00:00 app[web.1]: File "/app/backend/django_app/mainapp/wsgi.py", line 16, in <module>
2021-05-01T20:21:02.007653+00:00 app[web.1]: application = get_wsgi_application()
2021-05-01T20:21:02.007654+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
2021-05-01T20:21:02.007654+00:00 app[web.1]: django.setup(set_prefix=False)
2021-05-01T20:21:02.007655+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/django/__init__.py", line 19, in setup
2021-05-01T20:21:02.007655+00:00 app[web.1]: configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
2021-05-01T20:21:02.007655+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/django/conf/__init__.py", line 83, in __getattr__
2021-05-01T20:21:02.007656+00:00 app[web.1]: self._setup(name)
2021-05-01T20:21:02.007656+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/django/conf/__init__.py", line 70, in _setup
2021-05-01T20:21:02.007656+00:00 app[web.1]: self._wrapped = Settings(settings_module)
2021-05-01T20:21:02.007656+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/django/conf/__init__.py", line 177, in __init__
2021-05-01T20:21:02.007657+00:00 app[web.1]: mod = importlib.import_module(self.SETTINGS_MODULE)
2021-05-01T20:21:02.007657+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/importlib/__init__.py", line 127, in import_module
2021-05-01T20:21:02.007657+00:00 app[web.1]: return _bootstrap._gcd_import(name[level:], package, level)
2021-05-01T20:21:02.007658+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
2021-05-01T20:21:02.007658+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 991, in _find_and_load
2021-05-01T20:21:02.007658+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 961, in _find_and_load_unlocked
2021-05-01T20:21:02.007658+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
2021-05-01T20:21:02.007659+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
2021-05-01T20:21:02.007659+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 991, in _find_and_load
2021-05-01T20:21:02.007659+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked
2021-05-01T20:21:02.007660+00:00 app[web.1]: ModuleNotFoundError: No module named 'mainapp'
2021-05-01T20:21:02.007864+00:00 app[web.1]: [2021-05-01 20:21:02 +0000] [8] [INFO] Worker exiting (pid: 8)
2021-05-01T20:21:02.078511+00:00 app[web.1]: [2021-05-01 20:21:02 +0000] [4] [INFO] Shutting down: Master
2021-05-01T20:21:02.078591+00:00 app[web.1]: [2021-05-01 20:21:02 +0000] [4] [INFO] Reason: Worker failed to boot.
2021-05-01T20:21:02.152800+00:00 heroku[web.1]: Process exited with status 3
2021-05-01T20:21:02.289079+00:00 heroku[web.1]: State changed from starting to crashed
2021-05-01T20:21:10.000000+00:00 app[api]: Build succeeded

任何帮助将不胜感激。

提前致谢。

【问题讨论】:

您是否尝试过修改您的 Procfile,如 here 所示? 是的。 gunicorn --pythonpath django_app mainapp.wsgi --log-file - 但结果是一样的 添加后端有什么改变吗? gunicorn --pythonpath backend django_app mainapp.wsgi --log-file - 显然它适用于gunicorn backend.django_app.mainapp.wsgi --log-file -。但现在我得到另一个错误。我将编辑问题。 【参考方案1】:

错误消息指出:ModuleNotFoundError: No module named 'mainapp'。它试图使用mainapp 作为一个模块。为此,您需要在目录中添加 __init__.py 文件。

'root'/
  Procfile
  backend/
    django_app/
      __init__.py
      manage.py
      mainapp/
        __init__.py
        settings.py
        wsgi.py      

您可以阅读有关__init__.py 文件及其内容here 的更多信息。

【讨论】:

我在每个模块上都有__init__.pys。我只是忽略了它(以及所有模型、视图、url、管理员、应用程序、迁移/ 显然它适用于gunicorn backend.django_app.mainapp.wsgi --log-file -。但现在我得到另一个错误。我将编辑问题。【参考方案2】:

我终于解决了。 感谢所有输入。

Procfile的最终版本是这样的:

web: gunicorn -b 0.0.0.0:8000 backend.django_app.mainapp:application --log-file -

【讨论】:

以上是关于Gunicorn / Procfile - 找不到module.wsgi的主要内容,如果未能解决你的问题,请参考以下文章

Heroku Gunicorn Procfile

通过 Gunicorn 在 Heroku 上的 Django-twoscoops-project(骨架)。如何设置Procfile?

部署到 Heroku 时,如何在 Procfile 中导入带有 Gunicorn 的自定义模块?

Heroku,Java,Procfile,找不到或加载主类

Foreman start 找不到 Procfile,将 Django 应用程序部署到 Heroku

在当前目录中找不到 Procfile 和 package.json 文件 - 请参阅 run-foreman.js