Ubuntu – apache pointing to the wrong version of python on ubuntu how to change

apache-2.2mod-wsgiUbuntu

I am setting up a flask application on and Ubuntu 12.04.3 LTS EC2 instance and everything seemed to be working well (i.e. I could get to the webpage via the publicly available url) until I tried to import a module (e.g. numpy) and realised the apache python differs from the one I used to compile the mod_wsgi and also the one I am using

I am running apache2.

The apache2 logs show the warnings (specifically the last line shows the path hasnt changed):

[warn] mod_wsgi: Compiled for Python/2.7.5.
[warn] mod_wsgi: Runtime using Python/2.7.3.
[warn] mod_wsgi: Python module path '/usr/lib/python2.7/:/usr/lib/python2.7/plat-linux2:/usr/lib/python2.7/lib-tk:/usr/lib$

I have tried to set the path in my virtual host conf (my python is located in /home/ubuntu/anaconda/bin along with all of the other libraries):

WSGIPythonHome /home/ubuntu/anaconda
WSGIPythonPath /home/ubuntu/anaconda

<VirtualHost *:80>
                ServerName xx-xx-xxx-xxx-xxx.compute-1.amazonaws.com
                ServerAdmin admin@mywebsite.com
                WSGIScriptAlias / /var/www/microblog/microblog.wsgi
                <Directory /var/www/microblog/app/>
                        Order allow,deny
                        Allow from all
                </Directory>
                Alias /static /var/www/microblog/app/static
                <Directory /var/www/FlaskApp/FlaskApp/static/>
                        Order allow,deny
                        Allow from all
                </Directory>
                ErrorLog ${APACHE_LOG_DIR}/error.log
                LogLevel warn
                CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

But I still get the warnings and the apache python path hasnt changed – where do I need to put the relevant directives to point apache at my python version and modules (e.g. scipy, numpy etc)?

Separately, could I have avoided this using virtual environments?

Thanks in advance.

EDIT #1:

I have run:

>>> import sys
>>> print sys.prefix

To get:

/home/ubuntu/anaconda

And running ldd mod_wsgi.so with set/unset LD_LIBRARY_PATH gives the same symlink

libpython2.7.so.1.0 => /usr/lib/libpython2.7.so.1.0 (0x00007f13ca1c1000)

Which is the system version of python NOT the one I want and NOT the one I compiled mod_wsgi when I ran:

./configure --with-python=/home/ubuntu/anaconda/bin/python2.7

Best Answer

The difference in patch level revision doesn't usually matter and is explained in:

More important is what is the value of sys.prefix when scripts are run under mod_wsgi.

And what Python shared library the mod_wsgi.so file is actually using and whether you compiled it correctly to ensure it will pick up the alternate shared library.

If it truly isn't picking up the correct shared library, then the mismatch can be an issue and you need to recompile mod_wsgi.

Read through the documentation and perform the checks on your installation explained there and take the action described there as necessary.

Related Topic