wiki:DynamicVirtualenv

Defining VirtualEnv with DynamicApps

VirtualEnv is based on the use of the Py_SetPythonHome() C function.

This function has effect only if called before Py_Initialize() so using it with DynamicApps is impossibile.

So, to allow defining VirtualEnv with DynamicApps a hack is the only solution.

First of all you have to tell python engine to not import the site module.

This is the module (automagically loaded at python initialization) that will add to the sys.path all the site-packages directories.

To emulate VirtualEnv we have to load the site module only after the subinterpreter initializazion

Skipping the first import site, we can simply set sys.prefix and sys.exec_prefix on dynamic app loading and call

PyImport_ImportModule("site");

Some users would want to not disable initial site module loading, so the site module must be reloaded

PyImport_ReloadModule(site_module);

Now we can simply set VirtualEnv dynamically using the UWSGI_PYHOME var:

location / {
                uwsgi_pass 192.168.173.5:3031;
                include uwsgi_params;
                uwsgi_param UWSGI_SCRIPT mytrac;
                uwsgi_param UWSGI_PYHOME /Users/roberto/uwsgi/VENV2;
        }

or in apache2

<Location />
    uWSGISocket 192.168.173.5:3031;
                
    SetEnv UWSGI_SCRIPT mytrac
    SetEnv UWSGI_PYHOME /Users/roberto/uwsgi/VENV2
</Location>