将 Django 应用程序部署到 Heroku - 没有名为“django_project.wsgi 的模块

Deploying a Django application to Heroku - No module named 'django_project.wsgi

我正在尝试部署 Django 应用程序,但我一直收到此错误:

ModuleNotFoundError: No module named 'django_project.wsgi'

事实上,这是完整的日志:

2022-05-13T14:52:06.436735+00:00 app[web.1]: Traceback (most recent call last):
2022-05-13T14:52:06.436735+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/arbiter.py", line 589, in spawn_worker
2022-05-13T14:52:06.436736+00:00 app[web.1]: worker.init_process()
2022-05-13T14:52:06.436736+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/workers/base.py", line 134, in init_process
2022-05-13T14:52:06.436736+00:00 app[web.1]: self.load_wsgi()
2022-05-13T14:52:06.436737+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/workers/base.py", line 146, in load_wsgi
2022-05-13T14:52:06.436737+00:00 app[web.1]: self.wsgi = self.app.wsgi()
2022-05-13T14:52:06.436738+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/app/base.py", line 67, in wsgi
2022-05-13T14:52:06.436738+00:00 app[web.1]: self.callable = self.load()
2022-05-13T14:52:06.436738+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/app/wsgiapp.py", line 58, in load
2022-05-13T14:52:06.436740+00:00 app[web.1]: return self.load_wsgiapp()
2022-05-13T14:52:06.436740+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp
2022-05-13T14:52:06.436740+00:00 app[web.1]: return util.import_app(self.app_uri)
2022-05-13T14:52:06.436741+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/util.py", line 359, in import_app
2022-05-13T14:52:06.436741+00:00 app[web.1]: mod = importlib.import_module(module)
2022-05-13T14:52:06.436741+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/importlib/__init__.py", line 127, in import_module
2022-05-13T14:52:06.436742+00:00 app[web.1]: return _bootstrap._gcd_import(name[level:], package, level)
2022-05-13T14:52:06.436742+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
2022-05-13T14:52:06.436742+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
2022-05-13T14:52:06.436742+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 984, in _find_and_load_unlocked
2022-05-13T14:52:06.436742+00:00 app[web.1]: ModuleNotFoundError: No module named 'django_project.wsgi'
2022-05-13T14:52:06.436801+00:00 app[web.1]: [2022-05-13 14:52:06 +0000] [10] [INFO] Worker exiting (pid: 10)
2022-05-13T14:52:06.442147+00:00 app[web.1]: [2022-05-13 14:52:06 +0000] [4] [WARNING] Worker with pid 10 was terminated due to signal 15
2022-05-13T14:52:06.541420+00:00 app[web.1]: [2022-05-13 14:52:06 +0000] [4] [INFO] Shutting down: Master
2022-05-13T14:52:06.541451+00:00 app[web.1]: [2022-05-13 14:52:06 +0000] [4] [INFO] Reason: Worker failed to boot.
2022-05-13T14:52:06.664571+00:00 heroku[web.1]: Process exited with status 3
2022-05-13T14:52:06.894647+00:00 heroku[web.1]: State changed from starting to crashed
2022-05-13T14:53:23.778808+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=django-blog-project-laila.herokuapp.com request_id=4a9f0381-627b-4770-9757-93a4faccfe7b fwd="168.195.253.168" dyno= connect=
 service= status=503 bytes= protocol=https
2022-05-13T14:53:23.928270+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=django-blog-project-laila.herokuapp.com request_id=d296d612-ff9c-4ac7-9abd-5742f13cbe8e fwd="168.195.253.168" dyn
o= connect= service= status=503 bytes= protocol=https
2022-05-13T14:53:24.335534+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=django-blog-project-laila.herokuapp.com request_id=a5ea86d2-d172-46cd-b77f-978bd1d46d46 fwd="168.195.253.168" dyn
o= connect= service= status=503 bytes= protocol=https
2022-05-13T14:53:25.238063+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=django-blog-project-laila.herokuapp.com request_id=1153847b-b0d6-4855-98a8-e4722958b03b fwd="168.195.253.168" dyn
o= connect= service= status=503 bytes= protocol=https
2022-05-13T14:52:06.000000+00:00 app[api]: Build succeeded

这是我的 Procfile

web: gunicorn django_project.wsgi:application --log-file - --log-level debug
python manage.py collectstatic --noinput
manage.py migrate

settings.py 我有以下内容:

ALLOWED_HOSTS = [
    '127.0.0.1',
    'django-blog-project-laila.herokuapp.com'
]
MIDDLEWARE = [
    ...
    'whitenoise.middleware.WhiteNoiseMiddleware',
    ...
]

这是我的 wsgi.py 文件:

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django_project.settings')

application = get_wsgi_application()

这是我的项目结构:

我尝试将 Procfile 移动到与 manage.py 文件相同的文件夹,但是 heroku 不能找到它。

这是当 Procfilemanage.py:

位于同一文件夹时的日志
2022-05-13T19:06:18.510757+00:00 app[web.1]: ModuleNotFoundError: No module named 'django_project.wsgi'
2022-05-13T19:06:18.510820+00:00 app[web.1]: [2022-05-13 19:06:18 +0000] [9] [INFO] Worker exiting (pid: 9)
2022-05-13T19:06:18.537201+00:00 app[web.1]: [2022-05-13 19:06:18 +0000] [4] [INFO] Shutting down: Master
2022-05-13T19:06:18.537230+00:00 app[web.1]: [2022-05-13 19:06:18 +0000] [4] [INFO] Reason: Worker failed to boot.
2022-05-13T19:06:18.730859+00:00 heroku[web.1]: Process exited with status 3
2022-05-13T19:06:18.869100+00:00 heroku[web.1]: State changed from starting to crashed
2022-05-13T19:56:41.000000+00:00 app[api]: Build started by user enders.game1990@gmail.com
2022-05-13T19:57:21.664049+00:00 app[api]: Release v11 created by user enders.game1990@gmail.com
2022-05-13T19:57:21.664049+00:00 app[api]: Deploy 699afe79 by user enders.game1990@gmail.com
2022-05-13T19:57:21.837420+00:00 heroku[web.1]: State changed from crashed to down
2022-05-13T19:57:24.119910+00:00 heroku[router]: at=error code=H14 desc="No web processes running" method=GET path="/" host=django-blog-project-laila.herokuapp.com request_id=6724bdc4-1598-4fb9-a657-59237254b687 fwd="168.195.253.168" d
yno= connect= service= status=503 bytes= protocol=https
2022-05-13T19:57:24.994965+00:00 heroku[router]: at=error code=H14 desc="No web processes running" method=GET path="/favicon.ico" host=django-blog-project-laila.herokuapp.com request_id=0fb84b80-c136-409f-bb4d-7cf6b5f1d84f fwd="168.195
.253.168" dyno= connect= service= status=503 bytes= protocol=https
2022-05-13T19:57:33.000000+00:00 app[api]: Build succeeded
2022-05-13T19:57:40.582602+00:00 heroku[router]: at=error code=H14 desc="No web processes running" method=GET path="/" host=django-blog-project-laila.herokuapp.com request_id=e5964c10-21ec-44ad-9440-c00503ac7752 fwd="168.195.253.168" d
yno= connect= service= status=503 bytes= protocol=https
2022-05-13T19:57:40.834565+00:00 heroku[router]: at=error code=H14 desc="No web processes running" method=GET path="/favicon.ico" host=django-blog-project-laila.herokuapp.com request_id=9f8b5a5c-6dbb-49ba-bfc6-108121e48d91 fwd="168.195
.253.168" dyno= connect= service= status=503 bytes= protocol=https
2022-05-13T19:57:41.203156+00:00 heroku[router]: at=error code=H14 desc="No web processes running" method=GET path="/favicon.ico" host=django-blog-project-laila.herokuapp.com request_id=bca40057-6e76-446b-8b84-a4c4a5bef659 fwd="168.195
.253.168" dyno= connect= service= status=503 bytes= protocol=https
2022-05-13T19:57:42.103394+00:00 heroku[router]: at=error code=H14 desc="No web processes running" method=GET path="/favicon.ico" host=django-blog-project-laila.herokuapp.com request_id=04f4c4b0-940e-4c36-a62f-00d874f7f57a fwd="168.195
.253.168" dyno= connect= service= status=503 bytes= protocol=https

编辑:Procfile 仍在 manage.py 文件夹中,我已尝试使用命令 heroku local web看看我是否可以在本地 运行。但是我收到以下错误:

[WARN] ENOENT: no such file or directory, open 'Procfile'
[FAIL] No Procfile and no package.json file found in Current Directory - See run-foreman.js --help

我尝试将 Procfile 更改为以下内容:

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

但是什么都没有。

尝试 运行ning heroku local webProcfile 在与我的 git 存储库(我指的是根目录,比我的 manage.py 文件高一级)但同样的错误:

[WARN] ENOENT: no such file or directory, open 'Procfile'
[FAIL] No Procfile and no package.json file found in Current Directory - See run-foreman.js --help

我做错了什么?任何帮助表示赞赏。谢谢。

将 runtime.txt 添加到您的 python 版本

runtime.txt

python-"your version"

和运行

pip freeze > requirements.txt

首先,确保您的 Procfilerequirements.txtruntime.txt 文件与 manage.py 文件位于同一文件夹中。

您的 Procfile 应如下所示: web:gunicorn django_project.wsgi --log-file -

您的 runtime.txt 应如下所示: python-versionnumber。例如python-3.10.2

您的 requirements.txt 文件是必需的,因为它可以帮助 heroku 解析和安装项目中使用的包。 运行pip freeze > requirements.txt

With Procfile at the root directory (that a level above manage.py),我改变了它内容如下:

web: gunicorn --pythonpath django_project django_project.wsgi --log-file -

根据 this 回答。

现在这是我的项目结构,以防万一不清楚 Procfile 的位置:

现在一切正常。

我将此问题标记为已解决。