Gunicorn/Django, ImportError: No module named application.wsgi
Asked Answered
P

4

8

I'm trying to deploy a Django app using Heroku, but I'm running into the following error: "ImportError: No module named myproject.wsgi".

My project is configured as such:

my-project
│   Procfile
│   requirements.txt
│   runtime.txt
│   README.md
│
├───myproject
│   │   db.sqlite3
│   │   django
│   │   django._file_
│   │   import
│   │   manage.py
|   |
│   ├───myproject
|   |   |    wsgi.py
|   |   |    settings.py
|   |   |    urls.py
|   |   |    _init_.py
|   |   |
|   |   ├───_pycache_
|   | 
│   ├───venv
...

My wgsi.py file is configured as such:

import os
import signal
import sys
import traceback
import time

from django.core.wsgi import get_wsgi_application
from whitenoise.django import DjangoWhiteNoise

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")

application = get_wsgi_application()
application = DjangoWhiteNoise(application)

My Procfile contains the following:

web: gunicorn myproject.wsgi:application --log-file -

Why is this producing an error?

Panne answered 3/12, 2017 at 8:21 Comment(0)
B
7

It seems your running directory is the outermost my-project. Try to change your WSGI application path like gunicorn myproject.myproject.wsgi:application --log-file - and see if the error changes.

I think putting your project in the root directory (i.e. removing the first myproject directory and putting your manage.py in my-project directory) is a requirement for Heroku and will fix your problem.

Bluebonnet answered 3/12, 2017 at 9:11 Comment(4)
I tried both options, but when trying to load the application online, I get an Application Error and the heroku logs command outputs "at=error code=H10 desc="App crashed" method=GET path="/"..."Panne
__import__(module) File "/app/myproject/wsgi.py", line 20, in <module> from django.core.wsgi import get_wsgi_application ImportError: No module named django.core.wsgi Worker exiting (pid: 9) Shutting down: Master Reason: Worker failed to boot. Process exited with status 3 State changed from starting to crashedPanne
@Panne Seems Django is not installed (or maybe a wrong version). Whats installed on your Heroku virtual env?Bluebonnet
"django-admin --version" and "./manage.py --version" both output "1.10.6" in the venv.Panne
S
5

I eventually fixed this using gunicorn's chdir flag which, as far as I understand it, essentially lets you pretend you're running gunicorn from another directory. Useful e.g. here where heroku tries to run gunicorn from one directory 'too low'.

To use it here you'd use in your Procfile:

web: gunicorn --chdir myproject myproject.wsgi:application --log-file -

i.e. you need the new:

--chdir myproject
Swami answered 11/5, 2020 at 17:53 Comment(1)
Thank you for this suggestion! I had a similar issue, but mine was a ModuleNotFoundError, both in the Heroku logs and when running locally. This solution got gunicorn to run the server locally from the root directory when I run the updated command with --chdir, hooray! Looks like it's fixed. BUT THEN: when I update that command in my Procfile and push it to Heroku and try opening the app again, it's still crashing with my original error: ModuleNotFoundError: No module named 'mysite.wsgi'. Am I right that it's a different flavor of the same issue running from root, or something else altogether?Tweezers
S
1

Extending above answer I tried my WSGI path to gunicorn myproject.myproject.wsgi:application --log-file - and yes error changed now it says ImportError: No module named myproject.settings. To solve this I changed my wsgi file.

FROM:

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

TO:

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

It woked like a charm for me!

Systematist answered 9/12, 2018 at 9:35 Comment(0)
C
0

I added gunicorn==20.1.0 to requirements.txt and it fixed the issue.

Cas answered 26/1, 2023 at 22:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.