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

Time:05-14

I'm trying to deploy a django application but I keep getting this error:

ModuleNotFoundError: No module named 'django_project.wsgi'

In fact, here's the full log:

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

Here's my Procfile

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

On settings.py I have the following:

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

Here's my wsgi.py file:

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

application = get_wsgi_application()

Here's my project strucure:

enter image description here

I've tried moving Procfile to the same folder as the manage.py file, but then heroku can't locate it.

Here's the log when Procfile is located at the same folder as 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

Edit: With Procfile still at manage.py folder, I've tried the command heroku local web to see if I can run it locally. But I get the following error:

[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

I've tried changing Procfile to the following:

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

But nothing.

Tried running heroku local web with Procfile in the same level as my git repository (which I'm refering to as the root directory, which is one level above my manage.py file) but same error:

[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

What am I doing wrong? Any help is appreciated. Thank you.

CodePudding user response:

Add runtime.txt with your python version

runtime.txt

python-"your version"

and run

pip freeze > requirements.txt

CodePudding user response:

First off, make sure your Procfile, requirements.txt & runtime.txt files are in the same folder as the manage.py file.

Your Procfile should read as follows: web:gunicorn django_project.wsgi --log-file -

Your runtime.txt should read as follows: python-versionnumber. e.g. python-3.10.2

Your requirements.txt file is necessary as it helps heroku resolve and install packages used in your project. Run pip freeze > requirements.txt

CodePudding user response:

With Procfile at the root directory (that is, one level above manage.py), I've changed its content to the following:

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

As per enter image description here

Now everything works as it should.

I'm marking this question as solved.

  • Related