How do I run uwsgi with virtualenv

60,854

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.

https://stackoverflow.com/a/27221427/567606

Share:
60,854
Asken
Author by

Asken

Trust is good, control is better. :)

Updated on October 04, 2020

Comments

  • Asken
    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 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.

  • Asken
    Asken over 10 years
    it changed but now I get: ImportError: No module named site. Do you know what that could be?
  • Asken
    Asken over 10 years
    Tried starting with --no-site but then I can't import os module
  • iMom0
    iMom0 over 10 years
    @Asken Try to create your virtualenv with --system-site-packages?
  • Asken
    Asken over 10 years
    will that copy the system-site-packages into my virtualenv?
  • RickyA
    RickyA about 10 years
    Had 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
    attolee over 8 years
    Could 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
    Palasaty over 8 years
    @attolee, try the path ~/.virtualenvs/ and the option virtualenv = a-virtualenv-created-by-virtualenvwrapper
  • attolee
    attolee over 8 years
    it 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, and Python 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
    attolee over 8 years
    ~/.virtualenvs/a-virtualenv-created-by-virtualenvwrapper/ works.
  • Alain1405
    Alain1405 over 8 years
    This 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
    Rajesh Veeranki over 7 years
    I solved it using uwsgi -H /path/to/your/virtualenv --ini file.ini It was expecting --ini flag to be passed.
  • TheExorcist
    TheExorcist over 6 years
    @attolee Its same with me , best solution is to activate the environment manually.
  • paperreduction
    paperreduction over 6 years
    pipenv creates dynamically named virtualenvs, so I suspect this approach will increasingly be more common as adoption of pipenv continues.
  • Denys Kotsur
    Denys Kotsur about 6 years
    What's the purpose of this message on SO?
  • user4830534
    user4830534 about 6 years
    Not 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
    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
    user4830534 about 6 years
    OK, point taken Denys Kotsur. I edited my post to remove the emotional baggage.
  • viswanath
    viswanath almost 5 years
    I faced the same problem. In my case, I was using python3. so, installing uwsgi with pip3 instead of pip solved the issue
  • m3nda
    m3nda over 2 years
    Case of command line translates into uwsgi --virtualenv $VIRTUAL_ENV etc