How do I run uwsgi with virtualenv
I'm currently developing my first real python flask project and am about to set up the build server to deploy the "Latest Build" which is built on every check-in.
I have set up a startup script where I start the application using uwsgi and this part is working fine. I have recently also started using virtualenv
and by doing so the packages installed are added to my project under projectname\flask\Lib\site-packages
.
I'm using nginx
as the web server and the config looks like this:
location / { try_files $uri @graderbuild; }
location @graderbuild {
include uwsgi_params;
uwsgi_param UWSGI_CHDIR /usr/local/grader/build;
uwsgi_param UWSGI_PYHOME /usr/local/grader/build;
uwsgi_pass 127.0.0.1:3031;
}
I'm starting uwsgi
using this:
exec /usr/local/bin/uwsgi --master --socket 127.0.0.1:3031
--wsgi-file restserver.py --callable app --processes 4 --die-on-term
--threads 2 >> /var/log/grader-build.log 2>&1
Now to where I know if I'm doing it right... currently I am deploying the entire folder to the build server. I don't want to install global python modules just to get my build to work. Right or wrong?
The error I get currently is:
ImportError: No module named flask_wtf
If I'm right, how do I configure the setup to use the virtualenv
site-packages? My preferred location would be in the startup
script and not in the nginx
config.
Use -H
to set virtualenv to python path.
uwsgi -H /path/to/your/virtualenv
http://uwsgi-docs.readthedocs.org/en/latest/Options.html#virtualenv
To use the activated virtualenv you can use this config snippet in your uwsgi.ini
:
; If VIRTUAL_ENV is set then use its value to specify the virtualenv directory
if-env = VIRTUAL_ENV
virtualenv = %(_)
endif =
As user995394 pointed out, there is a way to tell uWSGI use existing virtual environment.
However, when I pass uWSGI option in form virtualenv = /full/path/to/my/virtualenv
(it's from INI config) it complains about ImportError: No module named site
. The workaround I found is that you launch uWSGI from folder where your virtualenv is and pass just virtualenv = my_virtualenv_name
(i.e. path is relative).
I use uWSGI 2.0.