I upgraded Python 2.5 to 2.6 but now get "ImportError: No module named tagging.views"
THE ISSUE
The problem: it seems like the tagging
module which I have in my /srv/python-environments/saltycrane/lib/python2.5/site-packages/tagging
is not being 开发者_如何学Pythonincluded because I upgraded the system to 2.6, despite my assumption of relying on 2.5 in virtualenv mode.
Here's the error in /var/log/apache2/error.log
:
[Thu Mar 24 00:08:35 2011] [error] [client 127.0.0.1] mod_wsgi (pid=10470): Exception occurred processing WSGI script '/srv/workarounds/apache/django.wsgi'.
[Thu Mar 24 00:08:35 2011] [error] [client 127.0.0.1] Traceback (most recent call last):
[Thu Mar 24 00:08:35 2011] [error] [client 127.0.0.1] File "/usr/lib/pymodules/python2.6/django/core/handlers/wsgi.py", line 241, in __call__
[Thu Mar 24 00:08:35 2011] [error] [client 127.0.0.1] response = self.get_response(request)
[Thu Mar 24 00:08:35 2011] [error] [client 127.0.0.1] File "/usr/lib/pymodules/python2.6/django/core/handlers/base.py", line 141, in get_response
[Thu Mar 24 00:08:35 2011] [error] [client 127.0.0.1] return self.handle_uncaught_exception(request, resolver, sys.exc_info())
[Thu Mar 24 00:08:35 2011] [error] [client 127.0.0.1] File "/usr/lib/pymodules/python2.6/django/core/handlers/base.py", line 176, in handle_uncaught_exception
[Thu Mar 24 00:08:35 2011] [error] [client 127.0.0.1] if resolver.urlconf_module is None:
[Thu Mar 24 00:08:35 2011] [error] [client 127.0.0.1] File "/usr/lib/pymodules/python2.6/django/core/urlresolvers.py", line 239, in _get_urlconf_module
[Thu Mar 24 00:08:35 2011] [error] [client 127.0.0.1] self._urlconf_module = import_module(self.urlconf_name)
[Thu Mar 24 00:08:35 2011] [error] [client 127.0.0.1] File "/usr/lib/pymodules/python2.6/django/utils/importlib.py", line 35, in import_module
[Thu Mar 24 00:08:35 2011] [error] [client 127.0.0.1] __import__(name)
[Thu Mar 24 00:08:35 2011] [error] [client 127.0.0.1] File "/srv/workarounds/urls.py", line 3, in <module>
[Thu Mar 24 00:08:35 2011] [error] [client 127.0.0.1] from tagging.views import tagged_object_list
[Thu Mar 24 00:08:35 2011] [error] [client 127.0.0.1] ImportError: No module named tagging.views
MY SETUP
Firstly, here's my setup:
$ python --version
Python 2.6.6
$ python
>>> import django
django.VERSION
(1, 2, 3, 'final', 0)
My django projects live in /srv
. My virtual envs live in /srv/python-environments
If I cd
into /srv/
and do:
source python-environments/saltycrane/bin/activate
The python version becomes:
$ python --version
Python 2.5.2
My project, /srv/workarounds
uses /srv/workarounds/apache/django.wsgi
, which is composed of:
import os, sys, site
virtualenv = '/srv/python-environments/saltycrane/'
ALLDIRS = [os.path.join(virtualenv, 'lib', 'python%s' % sys.version[:3], 'site-packages')]
# Remember original sys.path.
prev_sys_path = list(sys.path)
# Add project directory
sys.path.append( '/srv/' )
sys.path.append( '/srv/workarounds/' )
for directory in ALLDIRS:
site.addsitedir( directory )
new_sys_path = []
for item in list(sys.path):
if item not in prev_sys_path:
new_sys_path.append(item)
sys.path.remove(item)
sys.path[:0] = new_sys_path
os.environ['DJANGO_SETTINGS_MODULE'] = 'workarounds.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
The code basically adds /srv/
and /srv/workarounds
to the python path when the wsgi is run by Apache.
SO....
How can I adjust my wsgi to keep using Python 2.5.2 instead of 2.6? Or if not, how can I update my virtualenv to use python 2.6 packages with pip
?
THINGS IM NOT SURE ABOUT
Perhaps it runs on 2.6 and does grab the
tagging
module, but the tagging module or a part of it isn't compatible, therefore that's why it complains ofNo module named tagging.views
?Maybe this has nothing to do with 2.5 to 2.6 and was always present? But I doubt this very much because I did several restarts with that code in place, and it worked fine.
It could have been a Django subtle version update, I might have updated 1.1 or 1.2beta to 1.2.3 final.
EDIT: If I have the right mod_wsgi.so, it's compiled for 2.6.
/usr/lib/apache2/modules$ ldd mod_wsgi.so
linux-gate.so.1 => (0xf57fe000)
libpython2.6.so.1.0 => /usr/lib/libpython2.6.so.1.0 (0xb760f000)
libpthread.so.0 => /lib/libpthread.so.0 (0xb75f6000)
libdl.so.2 => /lib/libdl.so.2 (0xb75f2000)
libutil.so.1 => /lib/libutil.so.1 (0xb75ed000)
libm.so.6 => /lib/libm.so.6 (0xb75c7000)
libc.so.6 => /lib/libc.so.6 (0xb7482000)
libssl.so.0.9.8 => /usr/lib/i686/cmov/libssl.so.0.9.8 (0xb7438000)
libcrypto.so.0.9.8 => /usr/lib/i686/cmov/libcrypto.so.0.9.8 (0xb72e0000)
libz.so.1 => /usr/lib/libz.so.1 (0xb72cc000)
/lib/ld-linux.so.2 (0xb788e000)
I think if you changed this it might work:
ALLDIRS = [os.path.join(virtualenv, 'lib', 'python%s' % sys.version[:3], 'site-packages')]
to
ALLDIRS = [os.path.join(virtualenv, 'lib', 'python2.5', 'site-packages')]
Setting your WSGIPythonHome is what is suggested when using virtualenv with modwsgi:
virtualenv --no-site-packages --python=python2.5 BASELINE
and setup modwsgi to use that as the wsgipythonhome
Baseline Environment
The first step in using virtual environments with mod_wsgi is to point mod_wsgi at a baseline Python environment. This step is actually optional and if not done the main Python installation for the system, usually that which mod_wsgi was compiled for, would be used as the baseline environment.
Although the main Python installation can be used, especially in a shared environment where daemon mode of mod_wsgi is used to host WSGI applications for different users, it is better to make the baseline environment a virgin environment with an effectively empty 'site-packages' directory. This way there is no possibility of conflicts between modules and packages in a users individual Python virtual environment and the baseline environment.
To create a virgin environment using the 'virtualenv' program, the '--no-site-packages' option should be supplied when creating the environment.
$ cd /usr/local/pythonenv
$ virtualenv --no-site-packages BASELINE
New python executable in BASELINE/bin/python
Installing setuptools............done.
Note that the version of Python from which this baseline environment is created must be the same version of Python that mod_wsgi was compiled for. It is not possible to mix environments based on different major/minor versions of Python.
Once the baseline Python environment has been created, the WSGIPythonHome directive should be defined within the global part of the main Apache configuration files. The directive should refer to the top level directory for the baseline environment created by the 'virtualenv' script.
WSGIPythonHome /usr/local/pythonenv/BASELINE
This Python environment will now be used as the baseline environment for all WSGI applications running under mod_wsgi, whether they be run in embedded mode or daemon mode.
There is no need to set the WSGIPythonHome directive if you want to use the main Python installation as the baseline environment.
精彩评论