Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

uWSGI + virtualenv 'No module named site'

So this seems to be a really common problem with this setup, but I can't find any solutions that work on SO. I've setup a very new Ubuntu 15.04 server, then installed nginx, virtualenv (and -wrapper), and uWSGI (via apt-get, so globally, not inside the virtualenv).

My virtualenv is located at /root/Env/example. Inside of the virtualenv, I installed Django, then at /srv/www/example/app ran Django's startproject command with the project name example, so I have vaguely this structure:

-root
  -Env
    -example
      -bin
      -lib
-srv 
  -www
    -example
      -app
        -example
          manage.py
          -example
            wsgi.py
            ...

My example.ini file for uWSGI looks like this:

[uwsgi]
project = example

plugin = python

chdir = /srv/www/example/app/example
home = /root/Env/example
module = example.wsgi:application

master = true
processes = 5

socket = /run/uwsgi/app/example/example.socket
chmod-socket = 664
uid = www-data
gid = www-data
vacuum = true

But no matter whether I run this via uwsgi --ini /etc/uwsgi/apps-enabled/example.ini or via daemon, I get the exact same error:

Python version: 2.7.9 (default, Apr  2 2015, 15:37:21)  [GCC 4.9.2]
Set PythonHome to /root/Env/example
ImportError: No module named site

I should note that the Django project works via the built-in development server ./manage.py runserver, and that when I remove home = /root/Env/example the thing works (but is obviously using the global Python and Django rather than the virtualenv versions, which means it's useless for a proper virtualenv setup).

Can anyone see some obvious path error that I'm not seeing? As far as I can tell, home is entirely correct based on my directory structure, and everything else in the ini too, so why is it not working with this ImportError?

like image 464
Matthew Johnston Avatar asked May 26 '15 01:05

Matthew Johnston


1 Answers

In my case, I was seeing this issue because the django app I was trying to run was written in python 3 whereas uwsgi was configured for python 2. I fixed the problem by:

  1. recompiling uwsgi to support both python 2 and python 3 apps (I followed this guide)
  2. adding this to my mydjangoproject_uwsgi.ini:
plugins         = python35 # or whatever you specified while compiling uwsgi 

For other folks using Django, you should also make sure you are correctly specifying the following:

# Django dir that contains manage.py
chdir           = /var/www/project/myprojectname
# Django wsgi (myprojectname is the name of your top-level project)
module          = myprojectname.wsgi:application
# the virtualenv you are using (full path)
home            = /home/ubuntu/Env/mydjangovenv
plugins         = python35
like image 82
danyamachine Avatar answered Nov 20 '22 20:11

danyamachine