[Django]-Heroku – No module named wsgi

20👍

This is an old question but I’m responding to it nonetheless as I faced this exact problem recently and the answer by Intenex although helpful was not able to resolve my issue.

As pointed out by Intenex you’re probably missing the wsgi.py file. This could be because you’ve created your project using Django 1.3 or a previous version. Creating a project using Django 1.4 automatically creates the wsgi.py just like manage.py, settings.py.

Unlike what Intenex has pointed out, here’s what you should do. Assuming you’re on Django 1.3 or earlier version, create wsgi.py file in the project directory (same directory as manage.py etc.) and add the following to your wsgi.py file:

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")

# This application object is used by the development server
# as well as any WSGI server configured to use this file.
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

Note the difference in the last 2 statements compared to Intenex’s answer.

With this you should be able to run foreman start with the following in your Procfile:

web: gunicorn mysite.wsgi

5👍

It looks like you don’t have a wsgi.py module file in your project directory. If you started your project (with django-admin.py startproject project) with Django 1.3 or earlier, the wsgi file was not automatically created. You’ll want to create it yourself following this guide:

https://docs.djangoproject.com/en/dev/howto/deployment/wsgi/

As per the guide, put this in the file, making sure to change ‘mysite’ to your project name:

import os

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

# This application object is used by the development server
# as well as any WSGI server configured to use this file.
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

Also, as I didn’t see this in your settings, you’ll want to make sure your project directory is included in your Python path as per the last sentence here: https://docs.djangoproject.com/en/dev/howto/deployment/wsgi/gunicorn/#running-django-in-gunicorn-as-a-generic-wsgi-application

Or else you’ll run into the same problem I did:
Python app import error in Django with WSGI gunicorn

After that, everything should work without a hitch. Cheers!

Sidenote: You might already be able to run gunicorn by changing your Procfile to:

web: python manage.py run_gunicorn

as per https://docs.djangoproject.com/en/dev/howto/deployment/wsgi/gunicorn/#using-gunicorn-s-django-integration, but I haven’t tested this myself so not sure. Might still need the wsgi.py module.

1👍

Just came across this problem; occured to me that my Procfile had Windows Format EOL Encoding; switched to UNIX EOL – and voilà, worked.

1👍

Came across this error and the above question just now.

Found the settings file had a syntax error in the line for ALLOWED_HOSTS. The above descriptions help point me in that direction (at least looking at the settings).

Thought it might help somebody else out…

👤Minnow

1👍

I had this error as well, and the other solutions did not fix it.
The source of my problem was that I had installed gunicorn through apt-get. To solve, I removed gunicorn via apt and ran pip install gunicorn.

👤Brukba

0👍

I ran into this error because I manually renamed my project folder from myproject to myproject_django, but I didn’t fix all of the references in the settings file. In particular,

WSGI_APPLICATION = 'myproject.wsgi.application'

needed to be changed to

WSGI_APPLICATION = 'myproject_django.wsgi.application'

Again, there a few references in that file that need to be changed. Just do a search and a careful, one-by-one replace.

👤Seth

Leave a comment