Search code examples
djangoapachevirtualenvmod-wsgivirtualenvwrapper

Django 1.5, Apache, wsgi.py - ImportError: Could not import settings


I have seen others post on this on StackOverflow (mainly for Django 1.4), but none of those provided a successful solution for me.

I am unable to setup Django 1.5.1 correctly with Apache. I receive the following error:

"ImportError: Could not import settings 'foobar.settings' (Is it on sys.path?): No module named foobar.settings"

Using a fresh install of Xubuntu 13.04 on virtualbox.

Here is my setup script

$ sudo apt-get update
$ sudo apt-get install apache2
$ sudo apt-get install libapache2-mod-wsgi
$ sudo apt-get install python-setuptools
$ sudo apt-get install build-essential nmap python-dev

$ sudo easy_install pip
$ sudo pip install virtualenvwrapper

$ cd ~
$ vim .bashrc

export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/foobar.com
source /usr/local/bin/virtualenvwrapper.sh
# save .bashrc

$ . .bashrc

$ mkdir -p foobar.com/prod
$ cd foobar.com/prod

# start a python project with virtualenv
$ mkvirtualenv foobar --no-site-packages
$ workon foobar

$ pip install django

# this will create project root
$ django-admin.py startproject foobar

vim foobar/foobar/settings.py
# add database settings to settings.py
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': '/home/banjo/foobar.com/prod/foobar/foobar/foobar.db'
    }
}
# save settings.py

$ cd /home/banjo/foobar.com/prod/foobar
python manage.py syncdb

# create directory for static files
$ mkdir static

# create directory for apache log files
$ cd /home/banjo/foobar.com/prod
$ mkdir logs

$ pip freeze > requirements.txt

# create apache .conf file for site
$ sudo vim /etc/apache2/sites-available/foobar.conf

<VirtualHost *:80>
  ServerAdmin banjo@foobar.com
  ServerName foobar
  ServerAlias foobar

  Alias /static/ /home/banjo/foobar.com/prod/foobar/static/

  <Directory /home/banjo/foobar.com/prod/foobar/static>
    Order allow,deny
    Allow from all
  </Directory>

  LogLevel warn
  ErrorLog /home/banjo/foobar.com/prod/logs/apache_error.log
  CustomLog /home/banjo/foobar.com/prod/logs/apache_access.log combined

  WSGIDaemonProcess foobar threads=15 display-name=%{GROUP} python-path=/home/banjo/foobar.com/prod/foobar/foobar:/home/banjo/.virtualenvs/foobar/lib/python2.7/site-packages
  WSGIProcessGroup foobar

  WSGIScriptAlias / /home/banjo/foobar.com/prod/foobar/foobar/wsgi.py
</VirtualHost>
# save .conf file

# add site to /etc/hosts
$ vim /etc/hosts
127.0.0.1 foobar
# save hosts file


# configure .wsgi file and the following
$ vim /home/banjo/foobar.com/prod/foobar/foobar/wsgi.py

import sys

path = '/home/banjo/foobar.com/prod/foobar/foobar'
if path not in sys.path:
    sys.path.insert(0, '/home/banjo/foobar.com/prod/foobar/foobar')

# save wsgi.py

$ sudo a2ensite foobar.conf
$ sudo service apache2 restart

# open browser and navigate to site
http://foobar

Here is my directory tree

+-- foobar.com
¦   +-- prod
¦       +-- foobar
¦       ¦   +-- foobar
¦       ¦   ¦   +-- foobar.db
¦       ¦   ¦   +-- __init__.py
¦       ¦   ¦   +-- __init__.pyc
¦       ¦   ¦   +-- settings.py
¦       ¦   ¦   +-- settings.pyc
¦       ¦   ¦   +-- urls.py
¦       ¦   ¦   +-- wsgi.py
¦       ¦   +-- manage.py
¦       ¦   +-- static
¦       +-- logs
¦       ¦   +-- apache_access.log
¦       ¦   +-- apache_error.log
¦       +-- requirements.txt

Here is the full error log

[Tue May 21 14:02:34 2013] [error] [client 127.0.0.1] mod_wsgi (pid=3424): Exception occurred processing WSGI script '/home/banjo/foobar.com/prod/foobar/foobar/wsgi.py'.
[Tue May 21 14:02:34 2013] [error] [client 127.0.0.1] Traceback (most recent call last):
[Tue May 21 14:02:34 2013] [error] [client 127.0.0.1]   File "/home/banjo/.virtualenvs/foobar/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 236, in call
[Tue May 21 14:02:34 2013] [error] [client 127.0.0.1]     self.load_middleware()
[Tue May 21 14:02:34 2013] [error] [client 127.0.0.1]   File "/home/banjo/.virtualenvs/foobar/lib/python2.7/site-packages/django/core/handlers/base.py", line 45, in load_middleware
[Tue May 21 14:02:34 2013] [error] [client 127.0.0.1]     for middleware_path in settings.MIDDLEWARE_CLASSES:
[Tue May 21 14:02:34 2013] [error] [client 127.0.0.1]   File "/home/banjo/.virtualenvs/foobar/lib/python2.7/site-packages/django/conf/init.py", line 53, in getattr
[Tue May 21 14:02:34 2013] [error] [client 127.0.0.1]     self._setup(name)
[Tue May 21 14:02:34 2013] [error] [client 127.0.0.1]   File "/home/banjo/.virtualenvs/foobar/lib/python2.7/site-packages/django/conf/init.py", line 48, in _setup
[Tue May 21 14:02:34 2013] [error] [client 127.0.0.1]     self._wrapped = Settings(settings_module)
[Tue May 21 14:02:34 2013] [error] [client 127.0.0.1]   File "/home/banjo/.virtualenvs/foobar/lib/python2.7/site-packages/django/conf/init.py", line 134, in init
[Tue May 21 14:02:34 2013] [error] [client 127.0.0.1]     raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
[Tue May 21 14:02:34 2013] [error] [client 127.0.0.1] ImportError: Could not import settings 'foobar.settings' (Is it on sys.path?): No module named foobar.settings

Here is the output of my sys path

(foobar)banjo@sandbox:~/foobar.com$ python
Python 2.7.4 (default, Apr 19 2013, 18:32:33) 
[GCC 4.7.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> for path in sys.path: print path
... 

/home/banjo/.virtualenvs/foobar/local/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg
/home/banjo/.virtualenvs/foobar/local/lib/python2.7/site-packages/pip-1.3.1-py2.7.egg
/home/banjo/.virtualenvs/foobar/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg
/home/banjo/.virtualenvs/foobar/lib/python2.7/site-packages/pip-1.3.1-py2.7.egg
/home/banjo/.virtualenvs/foobar/lib/python2.7
/home/banjo/.virtualenvs/foobar/lib/python2.7/plat-i386-linux-gnu
/home/banjo/.virtualenvs/foobar/lib/python2.7/lib-tk
/home/banjo/.virtualenvs/foobar/lib/python2.7/lib-old
/home/banjo/.virtualenvs/foobar/lib/python2.7/lib-dynload
/usr/lib/python2.7
/usr/lib/python2.7/plat-i386-linux-gnu
/usr/lib/python2.7/lib-tk
/home/banjo/.virtualenvs/foobar/local/lib/python2.7/site-packages
/home/banjo/.virtualenvs/foobar/lib/python2.7/site-packages

Solution

  • Try adding the following to your python path, making the appropriate path substitution.

    /unix/path/above/foobar/foobar.com/prod/foobar/
    

    One way add to your path is to create a file called foobar.pth (or whatever.pth) and place it in the following directory:

    /home/banjo/.virtualenvs/foobar/lib/python2.7/site-packages
    

    That file should contain a newline separated list of paths that you want to add to your virtual environment.

    /unix/path/above/foobar/foobar.com/prod/foobar/
    

    Update: actually this might be the path entry to make:

    /unix/path/above/foobar/foobar.com/prod/foobar/foobar/
    

    Adding the path here has the added benefit of exposing this path to the Python interpreter and not just through the wsgi.py script.