Search code examples
djangoapachewsgiimporterror

Django Import errors on production environment


i have a trouble with run django project on production server with Apache and mod_wsgi. This Error happened when i'm start apache and go to site first time or go from other:

ImportError at /
Exception Value: cannot import name MyName
Exception Location /var/www/projectname/appname/somemodule.py

When i'm reload page the error disappears and site work fine. Another point is that this error happened selectively and sometime not appear.

In project i'm use imports without project name prefix (i mean 'from accounts.models import Account' instead 'from projectname.accounts.models import Account').

On development (manage.py runserver) server all work fine without any troubles.

I have used many variations of my apache and wsgi script configurations but problem is not solved.

Here my current projectname.wsgi:

#!/usr/bin/env python
import os, sys, re

sys.path.append('/var/www/projectname')
sys.path.append('/var/www')

os.environ['PYTHON_EGG_CACHE'] = '/var/www/projectname/.python-egg'
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

Here is some parts from apache config:

<VirtualHost ip:80>
    ServerAdmin [email protected]
    DocumentRoot /var/www
    ServerName www.projectname.com
    WSGIScriptAlias / "/var/www/projectname/projectname.wsgi"

    WSGIDaemonProcess projectname threads=5 maximum-requests=5000

    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>

    <Directory /var/www/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        allow from all
    </Directory>
    ....

Also i'm use a separate Virtual Host for SSL.
I hope somebody help me.
Thanks!


Solution

  • When I had this problem, this post https://stackoverflow.com/a/10601523/1782780 pointed me towards the answer: I had a bug in a module further up the chain.

    As Michal explains in that post:

    Python does not just select that one class from the administrative/models.py file. Instead the Python interpreter reads the entire file, creates the module object, executes the code from the imported file in the new modules namespace, and then copies the name Contract from the new module's namespace to the current namespace. So, although it seems that you are importing just one class from a module, an error anywhere in that module can prevent a successful import - in your case it is an error with class ContractForm. The rest of the traceback details what exactly went wrong with that class.

    So look further back in your traceback and you might find your problem.