mardi 12 août 2014

Django / Apache / mod_wsgi : aucun module ne nommé importlib - Stack Overflow


After working with django's dev server for the past two months, the time finally came to move to apache + mod_wsgi.


The problem is when I go to my site (let's call it junux), to the URL mapped to the django app, things do not seem to work. When running the dev server on the server things work properly.


The bottom-line of the error is given to me in the apache error_log:



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



I'm aware this is similar to many other questions on the matter (there are so many that I won't even quote them here), but I still haven't found the answer. I have read quite a few guides on moving to production, including django's deployment docs, mod_wsgi's guides, some pycon presentation and have been googling the issue all day...


Lots of fun and exciting details below.


Any help will be appreciated. Thanks in advance.




The configuration:



  • Apache 2.2.15 with mod_wsgi on CentOS 6

  • Python 2.7.3 compiled from source

  • The site uses a virtualenv


This is the error page apache returns:


Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Apache/2.2.15 (CentOS) Server at junux.net Port 80

The apache error_log reveals the following information:


mod_wsgi (pid=22502): Create interpreter 'junux.net|/dev'.
mod_wsgi (pid=22502): Exception occurred processing WSGI script '/var/www/junux_dev/junux_site/wsgi.py'.
Traceback (most recent call last):
File "/var/www/junux_dev/venv/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 219, in __call__
self.load_middleware()
File "/var/www/junux_dev/venv/lib/python2.7/site-packages/django/core/handlers/base.py", line 39, in load_middleware
for middleware_path in settings.MIDDLEWARE_CLASSES:
File "/var/www/junux_dev/venv/lib/python2.7/site-packages/django/utils/functional.py", line 184, in inner
self._setup()
File "/var/www/junux_dev/venv/lib/python2.7/site-packages/django/conf/__init__.py", line 42, in _setup
self._wrapped = Settings(settings_module)
File "/var/www/junux_dev/venv/lib/python2.7/site-packages/django/conf/__init__.py", line 95, in __init__
raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
ImportError: Could not import settings 'junux_site.settings' (Is it on sys.path?): No module named importlib

The relevant wsgi.py:


import os
import sys
import site

# use our virtual environment
SITE_DIR = os.path.dirname(__file__)
PROJECT_ROOT = os.path.dirname(SITE_DIR)
site_packages = os.path.join(PROJECT_ROOT, 'venv/lib/python2.7/site-packages')
site.addsitedir(os.path.abspath(site_packages))
sys.path.insert(0, SITE_DIR)
sys.path.insert(1, PROJECT_ROOT)

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

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

And httpd.conf: (more stuff here from the default apache configuration file)


<VirtualHost *:80>
ServerName junux.net
ServerAlias junux.net
ServerAdmin admin@junux.net

WSGIScriptAlias /test /var/www/test/hello.py
WSGIScriptAlias /dev /var/www/junux_dev/junux_site/wsgi.py

<Directory /var/www/test >
Order allow,deny
Allow from all
</Directory>

<Directory /var/www/junux_dev >
Options FollowSymLinks
Order allow,deny
Allow from all
</Directory>

</VirtualHost>

LogLevel info

There's a WSGIScriptAlias to /test to provide a sanity-check that mod_wsgi indeed works. It does. When opening that URL the (very simple) application works (a typical hello world).


I have set permissions to chmod o+r on my wsgi file and chmod o+rx on the entire /var/www/junux_dev dir, as instructed in the pycon-sydney-2010 presentation refered to from here.




What version of Python was mod_wsgi compiled against? Looks like you might have multiple Python installations on the system and your virtual environment is using Python 2.7, but your mod_wsgi is compiled against 2.6.


Am basing this guess on fact that importlib was only added in Python 2.7, so if mod_wsgi was compiled for 2.6 and using that base installation, then would fail to find importlib.


Run checks:


http://code.google.com/p/modwsgi/wiki/CheckingYourInstallation#Python_Shared_Library http://code.google.com/p/modwsgi/wiki/CheckingYourInstallation#Python_Installation_In_Use



After working with django's dev server for the past two months, the time finally came to move to apache + mod_wsgi.


The problem is when I go to my site (let's call it junux), to the URL mapped to the django app, things do not seem to work. When running the dev server on the server things work properly.


The bottom-line of the error is given to me in the apache error_log:



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



I'm aware this is similar to many other questions on the matter (there are so many that I won't even quote them here), but I still haven't found the answer. I have read quite a few guides on moving to production, including django's deployment docs, mod_wsgi's guides, some pycon presentation and have been googling the issue all day...


Lots of fun and exciting details below.


Any help will be appreciated. Thanks in advance.




The configuration:



  • Apache 2.2.15 with mod_wsgi on CentOS 6

  • Python 2.7.3 compiled from source

  • The site uses a virtualenv


This is the error page apache returns:


Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Apache/2.2.15 (CentOS) Server at junux.net Port 80

The apache error_log reveals the following information:


mod_wsgi (pid=22502): Create interpreter 'junux.net|/dev'.
mod_wsgi (pid=22502): Exception occurred processing WSGI script '/var/www/junux_dev/junux_site/wsgi.py'.
Traceback (most recent call last):
File "/var/www/junux_dev/venv/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 219, in __call__
self.load_middleware()
File "/var/www/junux_dev/venv/lib/python2.7/site-packages/django/core/handlers/base.py", line 39, in load_middleware
for middleware_path in settings.MIDDLEWARE_CLASSES:
File "/var/www/junux_dev/venv/lib/python2.7/site-packages/django/utils/functional.py", line 184, in inner
self._setup()
File "/var/www/junux_dev/venv/lib/python2.7/site-packages/django/conf/__init__.py", line 42, in _setup
self._wrapped = Settings(settings_module)
File "/var/www/junux_dev/venv/lib/python2.7/site-packages/django/conf/__init__.py", line 95, in __init__
raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
ImportError: Could not import settings 'junux_site.settings' (Is it on sys.path?): No module named importlib

The relevant wsgi.py:


import os
import sys
import site

# use our virtual environment
SITE_DIR = os.path.dirname(__file__)
PROJECT_ROOT = os.path.dirname(SITE_DIR)
site_packages = os.path.join(PROJECT_ROOT, 'venv/lib/python2.7/site-packages')
site.addsitedir(os.path.abspath(site_packages))
sys.path.insert(0, SITE_DIR)
sys.path.insert(1, PROJECT_ROOT)

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

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

And httpd.conf: (more stuff here from the default apache configuration file)


<VirtualHost *:80>
ServerName junux.net
ServerAlias junux.net
ServerAdmin admin@junux.net

WSGIScriptAlias /test /var/www/test/hello.py
WSGIScriptAlias /dev /var/www/junux_dev/junux_site/wsgi.py

<Directory /var/www/test >
Order allow,deny
Allow from all
</Directory>

<Directory /var/www/junux_dev >
Options FollowSymLinks
Order allow,deny
Allow from all
</Directory>

</VirtualHost>

LogLevel info

There's a WSGIScriptAlias to /test to provide a sanity-check that mod_wsgi indeed works. It does. When opening that URL the (very simple) application works (a typical hello world).


I have set permissions to chmod o+r on my wsgi file and chmod o+rx on the entire /var/www/junux_dev dir, as instructed in the pycon-sydney-2010 presentation refered to from here.



What version of Python was mod_wsgi compiled against? Looks like you might have multiple Python installations on the system and your virtual environment is using Python 2.7, but your mod_wsgi is compiled against 2.6.


Am basing this guess on fact that importlib was only added in Python 2.7, so if mod_wsgi was compiled for 2.6 and using that base installation, then would fail to find importlib.


Run checks:


http://code.google.com/p/modwsgi/wiki/CheckingYourInstallation#Python_Shared_Library http://code.google.com/p/modwsgi/wiki/CheckingYourInstallation#Python_Installation_In_Use


0 commentaires:

Enregistrer un commentaire