I've been building my first Django project (just running it locally so far using runserver), and I'm taking the steps to host it on Heroku, adding gunicorn. The build succeeds, but when I try to open the app, the Heroku logs show an exception in worker process:
ModuleNotFoundError: No module named 'mysite.wsgi'
Originally my Procfile had this:
web: gunicorn mysite.wsgi
When I would try that gunicorn command locally, it would work from within the family-django/mysite directory, but when I tried from the root directory (family-django), it would give me the same 'no module named mysite.wsgi' error. According to this post, Heroku's going to try from the root directory, so I updated my Procfile as follows, to tell it to run from the mysite directory instead:
web: gunicorn --chdir mysite mysite.wsgi
This new gunicorn command does work locally when run from the root directory (family-django), hooray!! That must be the fix I need. BUT: after updating the Procfile in Heroku and trying to open the app again, it still fails with that 'no module named mysite.wsgi' error pasted above. So there's some other tweak that Heroku needs for this to run there.
My Django project structure is like this:
family-django
|-mysite
| |-familytree
| |-myauth
| |-mysite
| |-asgi.py
| |-settings.py
| |-urls.py
| |-wsgi.py
|-Procfile
|-requirements.txt
wsgi.py and asgi.py were both created at the start of the project. (Not sure if it's wrong to have both?). wsgi.py has:
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
application = get_wsgi_application()
settings.py has the following info for INSTALLED_APPS and WSGI_APPLICATION:
INSTALLED_APPS = [
'familytree.apps.FamilytreeConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.humanize',
"myauth",
]
WSGI_APPLICATION = 'mysite.wsgi.application'
Now that I'd like to get this up on Heroku, they recommend gunicorn, so I've added a requirements.txt:
django
django-heroku
gunicorn
Procfile has:
web: gunicorn --chdir mysite mysite.wsgi