uwsgi + Flask + virtualenv ImportError: no module named site

010110110101 picture 010110110101 · Jun 30, 2013 · Viewed 31.1k times · Source

(Other posts on SO are similar, but none have the specific combination of uwsgi + Flask + virtualenv) (This one is closest)

I installed uwsgi via apt-get. I also tried pip install wsgi. Both gave me the same issue.

Test command:

sudo uwsgi -s /tmp/uwsgi.sock -w myapp:app -H myvirtualenv

Result:

Python version: 2.7.4 (default, Apr 19, 2013, 18:35:44)  [GCC 4.7.3]
Set PythonHome to myvirtualenv
ImportError: No module named site

I can otherwise run my app in the virtual env.

Answer

JRajan picture JRajan · Mar 8, 2015

The path to your virtual environment is wrong. That's the reason for this error.

I'm using virtualenvwrapper and my virtual environments are set at ~/.virtualenvs. So in my case, the uwsgi call would look something like

sudo uwsgi -s /tmp/uwsgi.sock -w myapp:app -H ~/.virtualenvs/myapp

Hope this helps next time someone comes looking for this one.

Thanks to Cody for pointing it out in the comments.