Where should WSGIPythonPath point in my virtualenv?
Solution 1
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
...
</VirtualHost>
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.
Solution 2
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=…
andWSGIPythonPath
] were used to specify the location of the Python virtual environment. Specifically, they were used to add thesite-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 theWSGIDaemonProcess
directive for daemon mode, or theWSGIPythonHome
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.
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
python-home=/path/to/project/venv
</IfModule>
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
.
Related videos on Youtube
user83039
Updated on July 09, 2022Comments
-
user83039 almost 2 years
I have a folder called
python2.7
inside oflib
in the virtual environment.After reading half a dozen tutorials, I can't figure out exactly what I'm suppose to point the WSGIPythonPath to. I've seen some pointing to
site-packages
but some have been a colon:
separated list.Syntax error on line 1019 of /etc/httpd/conf/httpd.conf: WSGIPythonPath cannot occur within <VirtualHost> section
Where should WSGIPythonPath point in my virtualenv?
-
Timusan over 9 yearsWhat is your setup? Are you trying to run a web application through Apache (via Mod_WSGI)? Or another webserver? Where specifically are you trying to set the "WSGIPythonPath"?
-
user83039 over 9 yearsApache and mod_wsgi, yes. Trying to set "WSGIPythonPath" in the apache configuration file for
<VirtualHost *:80>
-
Timusan over 9 yearsAnd are you using Mod_WSGI in daemon mode or in embedded mode? It may also be helpful to post your VirtualHost/Apache configuration entry together with your question.
-
user83039 over 9 yearsTo be honest, I'm not sure @Timusan. I'll try to figure out which one
-
user83039 over 9 yearsBasically, right now, the error is
Syntax error on line 1019 of /etc/httpd/conf/httpd.conf: WSGIPythonPath cannot occur within <VirtualHost> section
-
-
Graham Dumpleton over 7 yearsThat link is not using best practices. You should use
WGIPythonHome
to refer to virtual environment when using embedded mode and thepython-home
option when using daemon mode. See: blog.dscpl.com.au/2014/09/…