How do I run uwsgi with virtualenv
Solution 1
Use -H
to set virtualenv to python path.
uwsgi -H /path/to/your/virtualenv
http://uwsgi-docs.readthedocs.org/en/latest/Options.html#virtualenv
Solution 2
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 =
Solution 3
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.
Solution 4
Others' answers didn't help, and I added path to virtualenv to uwsgi.ini configuration file. The error disappeared.
pythonpath = /path-to-virtualenv/project/lib/python2.7/site-packages
Solution 5
I had this issue a few months back and have a full example of demo configs here including nginx, uwsgi starting automatically with upstart on linux.
Comments
-
Asken over 3 years
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 underprojectname\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 thestartup
script and not in thenginx
config. -
Asken over 10 yearsit changed but now I get:
ImportError: No module named site
. Do you know what that could be? -
Asken over 10 yearsTried starting with
--no-site
but then I can't importos
module -
iMom0 over 10 years@Asken Try to create your virtualenv with
--system-site-packages
? -
Asken over 10 yearswill that copy the
system-site-packages
into my virtualenv? -
RickyA about 10 yearsHad the same import error:
ImportError: No module named site
. Turned out I only supplied the name of the virtualenv, but you need to supply the full path -
attolee over 8 yearsCould you give a specific example of my_virtualenv-name? I use virtualenvwrapper, and I have tried the path ~/.virtualenvs/a-virtualenv-created-by-virtualenvwrapper/ and the name a-virtualenv-created-by-virtualenvwrapper, it's not work.
-
Palasaty over 8 years@attolee, try the path
~/.virtualenvs/
and the optionvirtualenv = a-virtualenv-created-by-virtualenvwrapper
-
attolee over 8 yearsit doesn't work. I got the output,
Python version: 2.7.6 (default, Jun 22 2015, 18:01:27) [GCC 4.8.2] Set PythonHome to ~/.virtualenvs/ ImportError: No module named site
, andPython version: 2.7.6 (default, Jun 22 2015, 18:01:27) [GCC 4.8.2] Set PythonHome to python3.4.3-uwsgi-django1.8-nginx ImportError: No module named site
, by the way, i also use pyenv to manage python version. -
attolee over 8 years
~/.virtualenvs/a-virtualenv-created-by-virtualenvwrapper/
works. -
Alain1405 over 8 yearsThis is the best solution if you use virtualenv as it has also the flexibility to work in different environment (production/staging/development) with different venv names!
-
Rajesh Veeranki over 7 yearsI solved it using
uwsgi -H /path/to/your/virtualenv --ini file.ini
It was expecting --ini flag to be passed. -
TheExorcist over 6 years@attolee Its same with me , best solution is to activate the environment manually.
-
paperreduction over 6 years
pipenv
creates dynamically named virtualenvs, so I suspect this approach will increasingly be more common as adoption ofpipenv
continues. -
Denys Kotsur about 6 yearsWhat's the purpose of this message on SO?
-
user4830534 about 6 yearsNot sure I understand your question of purpose. This answer was directly related to the OP topic, but I have insufficient rep yet to simply comment, so I had to write an answer to provide input. Paragraphs starting with "It makes me wonder" and "System's python3" are the main points of new info I provided in hopes it saves others the time it took me to figure this out. I can comment to your comment but not to any other? Hmmm...
-
Denys Kotsur about 6 years@user4830532 Try not to use your feelings and opinions, just use facts. Here you can find explanation why your answer can be downvoted and deleted afterwards.
-
user4830534 about 6 yearsOK, point taken Denys Kotsur. I edited my post to remove the emotional baggage.
-
viswanath almost 5 yearsI faced the same problem. In my case, I was using python3. so, installing uwsgi with pip3 instead of pip solved the issue
-
m3nda over 2 yearsCase of command line translates into
uwsgi --virtualenv $VIRTUAL_ENV etc