I am running a Debian Lenny box, which comes with Python 2.5.2. I would like to run Python 2.7 for my WSGI apps only, keeping 2.5 as default for the system, but I cannot get it to work.

First, I installed Python 2.7.2 from source into /usr/local:

Python-2.7.2 # ./configure --enable-shared
Python-2.7.2 # make
Python-2.7.2 # make altinstall

Calling python2.7 now from the command line works without any problems. It also finds packages installed via pip in /usr/local/lib/python2.7/packages.

Next, I built mod_wsgi and copied it manually to the Apache module directory, so as not to touch files placed by apt-get.

mod_wsgi-3.3 # ./configure --with-python=/usr/local/bin/python2.7
mod_wsgi-3.3 # make
mod_wsgi-3.3 # cp .libs/mod_wsgi.so /usr/lib/apache2/modules/mod_wsgi.so-2.7
mod_wsgi-3.3 # cd /usr/lib/apache2/modules
modules # ln -sf mod_wsgi.so-2.7 mod_wsgi.so
modules # ldd mod_wsgi.so-2.7
        linux-vdso.so.1 =>  (0x00007fff08f53000)
        libpython2.7.so.1.0 => /usr/local/lib/libpython2.7.so.1.0 (0x00002b0317e1f000)
        libpthread.so.0 => /lib/libpthread.so.0 (0x00002b03181fd000)
        libdl.so.2 => /lib/libdl.so.2 (0x00002b031841a000)
        libutil.so.1 => /lib/libutil.so.1 (0x00002b031861e000)
        libm.so.6 => /lib/libm.so.6 (0x00002b0318821000)
        libc.so.6 => /lib/libc.so.6 (0x00002b0318aa5000)
        /lib64/ld-linux-x86-64.so.2 (0x00002b03179cd000)

I then modified /etc/apache2/mods-available/wsgi.conf and added the following directive:

<IfModule mod_wsgi.c>
    WSGIPythonHome /usr/local

(Of course, wsgi.conf is symlinked in /etc/apache2/mods-enabled.)

Finally, I restarted apache, but my WSGI scripts won’t run. I get the following traceback in Apache’s error log:

mod_wsgi (pid=20746): Exception occurred processing WSGI script '/path/to/script.wsgi'.
Traceback (most recent call last):
  File "/usr/local/lib/python2.5/site-packages/Flask-0.7.2-py2.5.egg/flask/app.py", line 1306, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/local/lib/python2.5/site-packages/Flask-0.7.2-py2.5.egg/flask/app.py", line 1295, in wsgi_app
    return response(environ, start_response)
  File "/usr/local/lib/python2.5/site-packages/Werkzeug-0.6.2-py2.5.egg/werkzeug/wrappers.py", line 1017, in __call__
    start_response(status, headers)
TypeError: expected byte string object for status, value of type str found

I also tried the test WSGI script found in the Installation Issues section of mod_wsgi’s documentation, but I get the same TypeError here. Judging from the traceback, I would guess that Apache now runs Python 2.5 in combination with mod_wsgi compiled for 2.7.

Any ideas?


Problem solved. mod_python was also loaded, disabling it did the trick. As it is the vanilla mod_python shipped with Lenny’s Apache, it is compiled against Python 2.5.2—naturally, this cannot work in combination with mod_wsgi compiled against a different version.

  • if this was your solution, you should accept your own answer. and welcome to serverfault :) – Michael Lowman Jul 14 '11 at 20:24
  • Thanks, Michael, for the warm welcome. I cannot answer my own question within an 48 hour timespan, though. ;-) – igor Jul 15 '11 at 5:53

tl;dr Make sure the PATH inherited by the Apache user account sees the Python2.7 /bin and not the older version.


"If you have multiple versions of Python installed and you are not using that which is the default, you may have to organise that the PATH inherited by the Apache application when run will result in Apache finding the alternate version. Alternatively, the WSGIPythonExecutable directive should be used to specify the exact location of the 'python' executable corresponding to the version of Python compiled against. If this is not done, the version of Python running within Apache may attempt to use the Python modules from the wrong version of Python."

Other than that there is nothing that can be done aside from running another debian inside debian.


Having Nginx reverse proxy to the different virtual environments.

  • According to the Documentation (code.google.com/p/modwsgi/wiki/…), the WSGIPythonExecutable directive is deprecated. Furthermore, the section about Multiple Python Versions (code.google.com/p/modwsgi/wiki/…) discourages modifying the PATH environment variable for Apache. This is why I didn’t go for any of these proposed solutions. – igor Jul 14 '11 at 18:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.