Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Heroku - No module named wsgi

Tags:

I followed the getting started Django guide on the Heroku support center but I am getting the following error when I try to start it on either Heroku or with foreman:

ImportError: No module named wsgi 

Here is my procfile:

web: gunicorn testproject.wsgi -b 0.0.0.0:$PORT 

Here is my django project settings file:

from datetime import date, timedelta import os oneyr = date.today() + timedelta(days=365) PROJECT_DIR = os.path.dirname(__file__)  DEBUG = os.environ.get('DEBUG') TEMPLATE_DEBUG = DEBUG  ADMINS = (     # ('Your Name', '[email protected]'), )  MANAGERS = ADMINS  TIME_ZONE = 'Europe/London' LANGUAGE_CODE = 'en-gb' USE_I18N = False USE_L10N = True  MEDIA_ROOT = os.path.join(PROJECT_DIR, 'static/') MEDIA_URL = '/static/'  STATICFILES_DIRS = (     os.path.join(PROJECT_DIR, 'static/'), )  STATICFILES_FINDERS = (     'django.contrib.staticfiles.finders.FileSystemFinder',     'django.contrib.staticfiles.finders.AppDirectoriesFinder', )  TEMPLATE_LOADERS = (     'django.template.loaders.filesystem.Loader',     'django.template.loaders.app_directories.Loader', )  MIDDLEWARE_CLASSES = (     'django.middleware.common.CommonMiddleware',     'django.contrib.sessions.middleware.SessionMiddleware',     'django.middleware.csrf.CsrfViewMiddleware',     'django.contrib.auth.middleware.AuthenticationMiddleware',     'django.contrib.messages.middleware.MessageMiddleware', )  ROOT_URLCONF = 'testproject.urls'  TEMPLATE_DIRS = (     os.path.join(PROJECT_DIR, 'templates') )  INSTALLED_APPS = (     'django.contrib.auth',     'django.contrib.contenttypes',     'django.contrib.sessions',     'django.contrib.messages',     'django.contrib.staticfiles',     'gunicorn',     'storages', )  STATICFILES_STORAGE = 'storages.backends.s3boto.S3BotoStorage' 

Does anyone have any idea why this app won't start?

like image 525
Hanpan Avatar asked Jul 17 '12 23:07

Hanpan


2 Answers

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

like image 102
Arpit Rai Avatar answered Oct 26 '22 23:10

Arpit Rai


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.

like image 39
Intenex Avatar answered Oct 27 '22 00:10

Intenex