Where should WSGIPythonPath point in my virtualenv?
Asked Answered
F

3

22

I have a folder called python2.7 inside of lib 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?

Fotina answered 8/1, 2015 at 4:8 Comment(5)
What 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"?Comeuppance
Apache and mod_wsgi, yes. Trying to set "WSGIPythonPath" in the apache configuration file for <VirtualHost *:80>Fotina
And 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.Comeuppance
To be honest, I'm not sure @Timusan. I'll try to figure out which oneFotina
Basically, right now, the error is Syntax error on line 1019 of /etc/httpd/conf/httpd.conf: WSGIPythonPath cannot occur within <VirtualHost> sectionFotina
S
29

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.

Stephine answered 10/1, 2015 at 20:41 Comment(0)
A
12

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 
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.

Allhallowtide answered 2/9, 2018 at 20:28 Comment(0)
U
-1

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 quoted:

> 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).

Undo answered 21/9, 2016 at 10:33 Comment(1)
That link is not using best practices. You should use WGIPythonHome to refer to virtual environment when using embedded mode and the python-home option when using daemon mode. See: blog.dscpl.com.au/2014/09/…Organ

© 2022 - 2024 — McMap. All rights reserved.