Nathaniel Nathaniel - 1 month ago 24
Python Question

How do I use a conda environment with mod_wsgi?

My first post, and I'm new to both Python and Apache, so please go easy on me. I have a Python web application in a conda environment that serves up content just fine using Flask. Now I need to serve everything with Apache instead of Flask. I can get Apache to run Python with mod_wsgi, but only with virtualenv. I tried installing virtualenv with Anaconda, but I received a warning that such a setup is unsupported and not recommended. So my question is this:

How do I set up Apache to run my application in a conda environment?

Here, there's a section on working with virtual environments, and it says I need to enter a line into my wsgi file showing where to find activate_this.py. A virtualenv virtual environment has activate_this.py, but a conda environment does not. It seems that without the right instructions for activating the virtual environment, I'm just getting error 500 in my browser and "Import error: no module named flask" in my httpd error_log.

This is in my httpd.conf file:

<VirtualHost *:80>
DocumentRoot /var/www
WSGIScriptAlias / var/www/tsm.wsgi
<Directory /var/www/tsm>
Order allow,deny
Allow from all
</Directory>
</VirtualHost>


And here is tsm.wsgi:

import sys
import site
site.addsitedir('~/.conda/envs/tsm/lib/python2.7/site-packages')
sys.path.insert(0, '/var/www/tsm/server/time_series_machine')
sys.stdout = sys.stderr
from app import app as application


Any ideas? Thanks for any help you can provide.

Answer

Your mod_wsgi would need to be compiled against Anaconda Python to start with and not your system Python. In other words you cannot use the system supplied mod_wsgi packages but would need to compile it yourself. Then follow what it says in:

That is, use daemon mode and use the python-home option to WSGIDaemonProcess.

Do note that there have been reports suggesting that Anaconda Python is broken in some way and will not work with systems that want to embed Python. So may not work anyway.

BTW, you cannot use '~' in the path in your WSGI script file anyway with the way you were doing it. It would not be expanded to be the home directory. But then, follow that post and you will not need that.