Facing ModuleNotFoundError: No module named 'blog_project.wsgi'

Facing ModuleNotFoundError: No module named 'blog_project.wsgi'

我正在尝试在 heroku 上部署 django 应用程序。构建成功,但是当我启动应用程序时,我遇到了导入错误。 “没有名为 'blog_project.wsgi' 的模块”。该应用程序 运行 在本地环境中运行良好。

(blog) ashish@tiwari:~/Documents/Django/blog$ heroku logs --tail
 ›   Warning: heroku update available from 7.39.0 to 7.39.1.
2020-03-23T02:37:56.149348+00:00 app[web.1]: [2020-03-23 02:37:56 +0000] [4] [INFO] Listening at: http://0.0.0.0:57707 (4)
2020-03-23T02:37:56.149552+00:00 app[web.1]: [2020-03-23 02:37:56 +0000] [4] [INFO] Using worker: sync
2020-03-23T02:37:56.160538+00:00 app[web.1]: [2020-03-23 02:37:56 +0000] [10] [INFO] Booting worker with pid: 10
2020-03-23T02:37:56.170494+00:00 app[web.1]: [2020-03-23 02:37:56 +0000] [10] [ERROR] Exception in worker process
2020-03-23T02:37:56.170497+00:00 app[web.1]: Traceback (most recent call last):
2020-03-23T02:37:56.170497+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
2020-03-23T02:37:56.170498+00:00 app[web.1]: worker.init_process()
2020-03-23T02:37:56.170498+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/workers/base.py", line 129, in init_process
2020-03-23T02:37:56.170499+00:00 app[web.1]: self.load_wsgi()
2020-03-23T02:37:56.170499+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/workers/base.py", line 138, in load_wsgi
2020-03-23T02:37:56.170499+00:00 app[web.1]: self.wsgi = self.app.wsgi()
2020-03-23T02:37:56.170500+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/base.py", line 67, in wsgi
2020-03-23T02:37:56.170500+00:00 app[web.1]: self.callable = self.load()
2020-03-23T02:37:56.170501+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 52, in load
2020-03-23T02:37:56.170501+00:00 app[web.1]: return self.load_wsgiapp()
2020-03-23T02:37:56.170501+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 41, in load_wsgiapp
2020-03-23T02:37:56.170502+00:00 app[web.1]: return util.import_app(self.app_uri)
2020-03-23T02:37:56.170502+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/util.py", line 350, in import_app
2020-03-23T02:37:56.170503+00:00 app[web.1]: __import__(module)
2020-03-23T02:37:56.171265+00:00 app[web.1]: ModuleNotFoundError: No module named 'blog_project.wsgi'
2020-03-23T02:37:56.173904+00:00 app[web.1]: [2020-03-23 02:37:56 +0000] [10] [INFO] Worker exiting (pid: 10)
2020-03-23T02:37:56.231018+00:00 app[web.1]: [2020-03-23 02:37:56 +0000] [11] [INFO] Booting worker with pid: 11
2020-03-23T02:37:56.314461+00:00 app[web.1]: [2020-03-23 02:37:56 +0000] [11] [ERROR] Exception in worker process
2020-03-23T02:37:56.314463+00:00 app[web.1]: Traceback (most recent call last):
2020-03-23T02:37:56.314464+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
2020-03-23T02:37:56.314464+00:00 app[web.1]: worker.init_process()
2020-03-23T02:37:56.314465+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/workers/base.py", line 129, in init_process
2020-03-23T02:37:56.314465+00:00 app[web.1]: self.load_wsgi()
2020-03-23T02:37:56.314466+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/workers/base.py", line 138, in load_wsgi
2020-03-23T02:37:56.314466+00:00 app[web.1]: self.wsgi = self.app.wsgi()
2020-03-23T02:37:56.314467+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/base.py", line 67, in wsgi
2020-03-23T02:37:56.314467+00:00 app[web.1]: self.callable = self.load()
2020-03-23T02:37:56.314468+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 52, in load
2020-03-23T02:37:56.314468+00:00 app[web.1]: return self.load_wsgiapp()
2020-03-23T02:37:56.314468+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 41, in load_wsgiapp
2020-03-23T02:37:56.314469+00:00 app[web.1]: return util.import_app(self.app_uri)
2020-03-23T02:37:56.314469+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/util.py", line 350, in import_app
2020-03-23T02:37:56.314469+00:00 app[web.1]: __import__(module)
2020-03-23T02:37:56.396761+00:00 app[web.1]: ModuleNotFoundError: No module named 'blog_project.wsgi'
2020-03-23T02:37:56.397180+00:00 app[web.1]: [2020-03-23 02:37:56 +0000] [11] [INFO] Worker exiting (pid: 11)
2020-03-23T02:37:56.588320+00:00 app[web.1]: [2020-03-23 02:37:56 +0000] [4] [INFO] Shutting down: Master
2020-03-23T02:37:56.588364+00:00 app[web.1]: [2020-03-23 02:37:56 +0000] [4] [INFO] Reason: Worker failed to boot.
2020-03-23T02:37:56.715871+00:00 heroku[web.1]: State changed from up to crashed
2020-03-23T02:37:56.696010+00:00 heroku[web.1]: Process exited with status 3
2020-03-23T02:48:41.215519+00:00 heroku[web.1]: State changed from crashed to starting
2020-03-23T02:48:46.264529+00:00 heroku[web.1]: Starting process with command `gunicorn blog_project.wsgi --log-file -`
2020-03-23T02:48:48.715506+00:00 heroku[web.1]: State changed from starting to crashed
2020-03-23T02:48:48.699479+00:00 heroku[web.1]: Process exited with status 3
2020-03-23T02:48:48.588784+00:00 app[web.1]: [2020-03-23 02:48:48 +0000] [4] [INFO] Starting gunicorn 19.9.0
2020-03-23T02:48:48.589327+00:00 app[web.1]: [2020-03-23 02:48:48 +0000] [4] [INFO] Listening at: http://0.0.0.0:6021 (4)
2020-03-23T02:48:48.589423+00:00 app[web.1]: [2020-03-23 02:48:48 +0000] [4] [INFO] Using worker: sync
2020-03-23T02:48:48.593176+00:00 app[web.1]: [2020-03-23 02:48:48 +0000] [10] [INFO] Booting worker with pid: 10
2020-03-23T02:48:48.599968+00:00 app[web.1]: [2020-03-23 02:48:48 +0000] [10] [ERROR] Exception in worker process
2020-03-23T02:48:48.599969+00:00 app[web.1]: Traceback (most recent call last):
2020-03-23T02:48:48.599969+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
2020-03-23T02:48:48.599970+00:00 app[web.1]: worker.init_process()
2020-03-23T02:48:48.599970+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/workers/base.py", line 129, in init_process
2020-03-23T02:48:48.599970+00:00 app[web.1]: self.load_wsgi()
2020-03-23T02:48:48.599970+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/workers/base.py", line 138, in load_wsgi
2020-03-23T02:48:48.599971+00:00 app[web.1]: self.wsgi = self.app.wsgi()
2020-03-23T02:48:48.599971+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/base.py", line 67, in wsgi
2020-03-23T02:48:48.599971+00:00 app[web.1]: self.callable = self.load()
2020-03-23T02:48:48.599971+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 52, in load
2020-03-23T02:48:48.599972+00:00 app[web.1]: return self.load_wsgiapp()
2020-03-23T02:48:48.599972+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 41, in load_wsgiapp
2020-03-23T02:48:48.599972+00:00 app[web.1]: return util.import_app(self.app_uri)
2020-03-23T02:48:48.599972+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/util.py", line 350, in import_app
2020-03-23T02:48:48.599973+00:00 app[web.1]: __import__(module)
2020-03-23T02:48:48.599995+00:00 app[web.1]: ModuleNotFoundError: No module named 'blog_project.wsgi'
2020-03-23T02:48:48.600161+00:00 app[web.1]: [2020-03-23 02:48:48 +0000] [10] [INFO] Worker exiting (pid: 10)
2020-03-23T02:48:48.627009+00:00 app[web.1]: [2020-03-23 02:48:48 +0000] [4] [INFO] Shutting down: Master
2020-03-23T02:48:48.627102+00:00 app[web.1]: [2020-03-23 02:48:48 +0000] [4] [INFO] Reason: Worker failed to boot.
2020-03-23T03:10:43.545420+00:00 heroku[web.1]: State changed from crashed to starting
2020-03-23T03:10:50.068632+00:00 heroku[web.1]: Starting process with command `gunicorn blog_project.wsgi --log-file -`
2020-03-23T03:10:52.561751+00:00 heroku[web.1]: State changed from starting to crashed
2020-03-23T03:10:52.436858+00:00 app[web.1]: [2020-03-23 03:10:52 +0000] [4] [INFO] Starting gunicorn 19.9.0
2020-03-23T03:10:52.437493+00:00 app[web.1]: [2020-03-23 03:10:52 +0000] [4] [INFO] Listening at: http://0.0.0.0:54608 (4)
2020-03-23T03:10:52.437577+00:00 app[web.1]: [2020-03-23 03:10:52 +0000] [4] [INFO] Using worker: sync
2020-03-23T03:10:52.441956+00:00 app[web.1]: [2020-03-23 03:10:52 +0000] [10] [INFO] Booting worker with pid: 10
2020-03-23T03:10:52.448061+00:00 app[web.1]: [2020-03-23 03:10:52 +0000] [10] [ERROR] Exception in worker process
2020-03-23T03:10:52.448075+00:00 app[web.1]: Traceback (most recent call last):
2020-03-23T03:10:52.448079+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
2020-03-23T03:10:52.448082+00:00 app[web.1]: worker.init_process()
2020-03-23T03:10:52.448083+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/workers/base.py", line 129, in init_process
2020-03-23T03:10:52.448083+00:00 app[web.1]: self.load_wsgi()
2020-03-23T03:10:52.448083+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/workers/base.py", line 138, in load_wsgi
2020-03-23T03:10:52.448083+00:00 app[web.1]: self.wsgi = self.app.wsgi()
2020-03-23T03:10:52.448088+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/base.py", line 67, in wsgi
2020-03-23T03:10:52.448088+00:00 app[web.1]: self.callable = self.load()
2020-03-23T03:10:52.448089+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 52, in load
2020-03-23T03:10:52.448089+00:00 app[web.1]: return self.load_wsgiapp()
2020-03-23T03:10:52.448089+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 41, in load_wsgiapp
2020-03-23T03:10:52.448089+00:00 app[web.1]: return util.import_app(self.app_uri)
2020-03-23T03:10:52.448090+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/util.py", line 350, in import_app
2020-03-23T03:10:52.448090+00:00 app[web.1]: __import__(module)
2020-03-23T03:10:52.448090+00:00 app[web.1]: ModuleNotFoundError: No module named 'blog_project.wsgi'
2020-03-23T03:10:52.448190+00:00 app[web.1]: [2020-03-23 03:10:52 +0000] [10] [INFO] Worker exiting (pid: 10)
2020-03-23T03:10:52.482036+00:00 app[web.1]: [2020-03-23 03:10:52 +0000] [4] [INFO] Shutting down: Master
2020-03-23T03:10:52.482118+00:00 app[web.1]: [2020-03-23 03:10:52 +0000] [4] [INFO] Reason: Worker failed to boot.
2020-03-23T03:10:52.544498+00:00 heroku[web.1]: Process exited with status 3

我的项目结构如下:

blog/
├── blog_project
│   ├── blog
│   │   ├── admin.py
│   │   ├── apps.py
│   │   ├── __init__.py
│   │   ├── migrations
│   │   │   ├── 0001_initial.py
│   │   │   ├── __init__.py
│   │   ├── models.py
│   │   ├── tests.py
│   │   ├── urls.py
│   │   └── views.py
│   ├── blog_project
│   │   ├── asgi.py
│   │   ├── __init__.py
│   │   ├── settings.py
│   │   ├── urls.py
│   │   └── wsgi.py
│   ├── db.sqlite3
│   ├── manage.py
├── Pipfile
├── Pipfile.lock
└── Procfile

以下是/blog/blog_project/blog_project/wsgi.py文件的内容

import os
import sys

from django.core.wsgi import get_wsgi_application

path = "/home/ashish/Documents/Django/blog/blog_project/"
if path not in sys.path:
    sys.path.insert(0, path)

os.environ['DJANGO_SETTINGS_MODULE'] = 'blog_project.settings'

application = get_wsgi_application()

怎么办? 提前致谢。

2020-03-23T02:48:46.264529+00:00 heroku[web.1]: Starting process with command `gunicorn blog_project.wsgi --log-file -`

这告诉我你的 Procfile 看起来像这样:

web: gunicorn blog_project.wsgi --log-file -

根目录中没有名称为 blog_project 且文件结尾为 .wsgi 的文件。

应该是这样的:

web: gunicorn blog_project/blog_project/wsgi:app

您可能希望将代码包装在根目录的 main.py 中,这样您就可以:

web: gunicorn main:app