Question

I'm trying to set up WAMP server. I've got Apache working correctly, and I've installed mod_wsgi without a hitch.

Problem is, I'm using virtual environments (using virtualenv) for my projects. So obviously, mod_wsgi is having problems locating my installation of Django.

I'm trying to understand how I can get mod_wsgi to work well with the virtualenvs. The documentation seems to think this isn't possible:

Note that the WSGIPythonHome directive can only be used on UNIX systems and is not available on Windows systems. This is because on Windows systems the location of the Python DLL seems to be what dictates where Python will look for the Python library files. It is not known at this point how one could create a distinct baseline environment independent of the main Python installation on Windows.

From here: mod_wsgi + virtualenv docs.

Does anyone have some idea on how to make this work?

Was it helpful?

Solution

You can activate the environment programmatically from Python adding this to your .wsgi file before importing anything else.

From virtualenv's docs:

Sometimes you can't or don't want to use the Python interpreter created by the virtualenv. For instance, in a mod_python or mod_wsgi environment, there is only one interpreter.

Luckily, it's easy. You must use the custom Python interpreter to install libraries. But to use libraries, you just have to be sure the path is correct. A script is available to correct the path. You can setup the environment like:

activate_this = '/path/to/env/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))

This will change sys.path and even change sys.prefix, but also allow you to use an existing interpreter. Items in your environment will show up first on sys.path, before global items. However, this cannot undo the activation of other environments, or modules that have been imported. You shouldn't try to, for instance, activate an environment before a web request; you should activate one environment as early as possible, and not do it again in that process.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top