How to get mod_wsgi to pick up my virtualenv

If you are using mod_wsgi, read the documentation at:

  • http://modwsgi.readthedocs.io/en/develop/user-guides/virtual-environments.html

TLDR:

From Documentation - to use a Python virtual environment, all you need to do is add the python-home option to the WSGIDaemonProcess directive resulting in

add this line to your virtual host to enable virtualenv

WSGIDaemonProcess application_name python-home=/path/to/app/venv


The best and cleanest way I've found without doing some “kind of magic” with obscure scripts is to simply begin the .wsgi with the reference to the python interpreter that lies within the environment. Just start your .wsgi with this, and no need to fiddle after that:

#!/path/to/your/venv/bin/python

I wish I thought about this straightforward solution before unsuccessfully spending hours on this - and wish someone else had mentioned it.


I was having the same issue, the solution is actually quite simple. You need to install libapache2-mod-wsgi-py3 instead of libapache2-mod-wsgi. The latter is for python 2.

You can then activate your environment by adding the environment's site-packages to the system path. For example, for me (using venv) I can do this by adding the following line to my *.wgsi file.

sys.path.insert(0,"/path/to/venv/lib/python3.8/site-packages")