我正在嘗試部署 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 找不到它。
這是Procfile與manage.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 [email protected]
2022-05-13T19:57:21.664049 00:00 app[api]: Release v11 created by user [email protected]
2022-05-13T19:57:21.664049 00:00 app[api]: Deploy 699afe79 by user [email protected]
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 -
但什么都沒有。
heroku local web嘗試在與我的 git 存盤庫相同級別的Procfile中運行(我將其稱為根目錄,它比我的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
我究竟做錯了什么?任何幫助表示贊賞。謝謝你。
uj5u.com熱心網友回復:
使用您的 python 版本添加 runtime.txt
運行時.txt
python-"your version"
并運行
pip freeze > requirements.txt
uj5u.com熱心網友回復:
首先,確保您的Procfile, requirements.txt&runtime.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
uj5u.com熱心網友回復:
使用根目錄中的Procfile(即,上一級manage.py),我已將其內容更改為以下內容:
web: gunicorn --pythonpath django_project django_project.wsgi --log-file -
按照
現在一切正常。
我將此問題標記為已解決。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/473747.html
