2

If I run python manage.py runserver, it works fine. But when I try to have my site run through apache, I keep getting the error:

 mod_wsgi (pid=25005): Exception occurred processing WSGI script '/apps/peer-web/peer_web/wsgi.py'.
 Traceback (most recent call last):
   File "/apps/peer-web-env/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 236, in __call__
     self.load_middleware()
   File "/apps/peer-web-env/lib/python2.7/site-packages/django/core/handlers/base.py", line 53, in load_middleware
     raise exceptions.ImproperlyConfigured('Error importing middleware %s: "%s"' % (mw_module, e))
 ImproperlyConfigured: Error importing middleware django.contrib.auth.middleware: "datetime initialization failed"
 mod_wsgi (pid=25005): Exception occurred processing WSGI script '/apps/peer-web/peer_web/wsgi.py'.
 Traceback (most recent call last):
   File "/apps/peer-web-env/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 236, in __call__
     self.load_middleware()
   File "/apps/peer-web-env/lib/python2.7/site-packages/django/core/handlers/base.py", line 53, in load_middleware
     raise exceptions.ImproperlyConfigured('Error importing middleware %s: "%s"' % (mw_module, e))
 ImproperlyConfigured: Error importing middleware django.contrib.auth.middleware: "cannot import name utils"

EDIT: new traceback after re-installing Django

 mod_wsgi (pid=25005): Exception occurred processing WSGI script '/apps/peer-web/peer_web/wsgi.py'.
 Traceback (most recent call last):
   File "/apps/peer-web-env/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 236, in __call__
     self.load_middleware()
   File "/apps/peer-web-env/lib/python2.7/site-packages/django/core/handlers/base.py", line 53, in load_middleware
     raise exceptions.ImproperlyConfigured('Error importing middleware %s: "%s"' % (mw_module, e))
 ImproperlyConfigured: Error importing middleware django.contrib.auth.middleware: "cannot import name utils"
 mod_wsgi (pid=25005): Exception occurred processing WSGI script '/apps/peer-web/peer_web/wsgi.py'.
 Traceback (most recent call last):
   File "/apps/peer-web-env/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 236, in __call__
     self.load_middleware()
   File "/apps/peer-web-env/lib/python2.7/site-packages/django/core/handlers/base.py", line 53, in load_middleware
     raise exceptions.ImproperlyConfigured('Error importing middleware %s: "%s"' % (mw_module, e))
 ImproperlyConfigured: Error importing middleware django.contrib.auth.middleware: "cannot import name utils"

How do I fix this?

Hank
  • 3,367
  • 10
  • 48
  • 86
  • See https://code.djangoproject.com/ticket/16136 and http://stackoverflow.com/questions/7866256/django-circular-import-error – alecxe Aug 04 '13 at 05:02
  • Both of them relate to a import of a db, which is different from mine. – Hank Aug 04 '13 at 05:57
  • 1
    You setup is not using the same version of django as what you have developed. Check virtual environment setup and Python paths. – Burhan Khalid Aug 04 '13 at 06:35
  • 1
    Both environments are using Django 1.5.1 – Hank Aug 04 '13 at 06:55
  • 1
    You need to reinstall django. Something is corrupted in your environment. – Burhan Khalid Aug 04 '13 at 07:08
  • 2
    After reinstalling the `datetime initialization failed` disappeared, but the `cannot import name utils` is still present. And is repeated twice – Hank Aug 04 '13 at 14:42
  • Had the same error "cannot import name utils" intermittantly when I had this in middleware: 'privateviews.middleware.LoginRequiredMiddleware', and system had logged me out. Logging in and out fixed it. – PhoebeB May 30 '14 at 12:34

2 Answers2

4

Since I'm using an Amazon Linux EC2 Instance, I forgot that installing mod_wsgi from the package installer will be configured using python2.6. So after reinstalling mod_wsgi using python2.7, it fixed my problem.

EDIT:

To configure it with python2.7, inside the mod_wsgi source folder I did the following:

./configure --with-python=/usr/bin/python2.7
make install
Hank
  • 3,367
  • 10
  • 48
  • 86
0

Please ensure both the project path and site-packages for your virtual environment are added to your mod_wsgi configuration.

WSGIPythonPath /path/to/mysite.com:/path/to/your/venv/lib/python2.X/site-packages

Django docs for deploying using mod_wsgi and a virtualenv

Community
  • 1
  • 1
AndrewS
  • 1,666
  • 11
  • 15