[Django]-Where should WSGIPythonPath point in my virtualenv?


You are getting the error because WSGIPythonPath Directive cannot be used inside the VirtualHost context. You have to declare it inside your main Apache configuration file. If you still want to point to the directories in your virtualenv inside the VirtualHost context, Use WSGIDaemonProcess Directive instead, it has a python-path option for you to declare your relevant python directories.

For example: your virtual host configuration file should look something like this:

<VirtualHost *:80>
ServerName example.com
CustomLog logs/example.com-access_log common
ErrorLog logs/example.com-error_log

WSGIDaemonProcess example.com python-path=/virtualenvpathto/site-packages:/pathto/exampleprojecthome
WSGIProcessGroup example.com


The full colon : is used when you have more than one python directories you want to be added to $PYTHON_PATH environment variable so that say import example.foo works fine. In the above example, there are two directories, they could be more or less depending on how you have setup your project.

If you are on windows, use semicolon ; instead of full colon.

I hope this helps.



tl;dr: Use WSGIDaemonProcess python-home=…. The alternatives using either WSGIPythonPath or WSGIDaemonProcess python-path=… (with -path instead of -home!) are no longer recommended.

The old and the new way

As mentioned by @kaykae, WSGIPythonPath cannot be used in a VirtualHost context but WSGIDaemonProcess python-path=… is the equivalent. However while this can still work, it is no longer the recommended way to set up Apache mod_wsgi with virtual Python environments:

Note that prior practice was that these ways of setting the Python module search path [namely WSGIDaemonProcess …python-path=… and WSGIPythonPath] were used to specify the location of the Python virtual environment. Specifically, they were used to add the site-packages directory of the Python virtual environment. You should not do that.

The better way to specify the location of the Python virtual environment is using the python-home option of the WSGIDaemonProcess directive for daemon mode, or the WSGIPythonHome directive for embedded mode. These ways of specifying the Python virtual environment have been available since mod_wsgi 3.0 and Linux distributions have not shipped such an old version of mod_wsgi for quite some time. If you are using the older way, please update your configurations.

(Source: WSGI Docs: User Guides: Virtual Environments)

How to do it the new way

The fact that you try to configure mod_wsgi inside a VirtualHost context shows you use the “daemon mode” configuration version. According to the quote above, the recommended way to include your virtualenv environment into your Python path would then be a section like this in your VirtualHost section (though it can also be defined outside, as it can be referenced with the myapp1 identifier for the daemon process group that you choose):

<IfModule mod_wsgi.c>
  WSGIDaemonProcess myapp1 user=user1 group=group1 threads=5 

Note that /path/to/project/venv is the base path of your virtualenv environment. It would be a subdirectory venv in the directory where you called virtualenv venv to create it.

Also note that you can add other paths to your Python path to make your import statements work for packages not managed via PIP or similar. For example you can add python-path=/path/to/project. Just don’t use that mechanism to tell wsgi about the whole virtualenv setup – for that they introduced python-home.



Here is the official documentation: https://docs.djangoproject.com/en/1.10/howto/deployment/wsgi/modwsgi/#using-a-virtualenv

Using a virtualenv¶

If you install your project’s Python dependencies inside a virtualenv,
you’ll need to add the path to this virtualenv’s site-packages
directory to your Python path as well. To do this, add an additional
path to your WSGIPythonPath directive, with multiple paths separated
by a colon (:) if using a UNIX-like system, or a semicolon (;) if
using Windows. If any part of a directory path contains a space
character, the complete argument string to WSGIPythonPath must be

> WSGIPythonPath
> /path/to/mysite.com:/path/to/your/venv/lib/python3.X/site-packages

Make sure you give the correct path to your virtualenv, and replace
python3.X with the correct Python version (e.g. python3.4).

Leave a comment