uWSGI and python virtual env

I'm trying to use uWSGI with a virtual env in order to use the Trac bug tracker on it.

I've installed system-wide uwsgi via pip.

Next, I've installed trac in a virtualenv

$ virtualenv venv
$ . venv/bin/activate
$ pip install trac

I've then written a simple uWSGI configuration script:

[uwsgi]
master = true
processes = 1
socket = localhost:3032

home = /srv/http/trac/venv/
no-site = true

gid = www-data
uid = www-data
env = TRAC_ENV=/srv/http/trac/projects/my_project
module = trac.web.main:dispatch_request

But when I try to launch it, it fails:

$ uwsgi --http :8000 --ini /etc/uwsgi/vassals-available/my_project.ini --gid www-data --uid www-data
...
Set PythonHome to /srv/http/trac/venv/
...
*** Operational MODE: single process ***
ImportError: No module named trac.web.main
unable to load app 0 (mountpoint='') (callable not found or import error)

I think uWSGI isn't using the virtual env. When inside the virtual env, I can import trac.web.main without having an ImportError.

How can I do that ? Thanks


Solution 1:

Try adding this in your ini file:

plugins = python
virtualenv = /path/to/your/venv

You might also need router_basicauth to handle authentication.

Solution 2:

To use the activated virtualenv you can use this config snippet in your uwsgi.ini:

; If VIRTAL_ENV is set then use its value to specify the virtualenv directory
if-env = VIRTUAL_ENV
virtualenv = %(_)
endif =