Roman Reimche
Roman Reimche

Reputation: 143

Django, uwsgi, nginx, virtualenv, ImportError: No module named site

I have a Django app and a virtual env and I try to run it under nginx+uwsgi. I've configured the whole system as is described here. I have the CentOS 7 as well.

However, I get the famous "ImportError: No module named site". No other topic here on stack overflow helped me to solve this.

In the log of uwsgi I have these two lines:

    Set PythonHome to /hedgehog/.virtualenv/hedgehog
    ImportError: No module named site
/etc/uwsgi/sites/hedgehog.ini: 
    [uwsgi]
    project = hedgehog
    username = hedgehog
    base = /var/www/%(username)/code

    chdir = /var/www/hedgehog/code
    home = /%(username)/.virtualenv/%(username)
    module = %(username).wsgi:application

    master = true
    processes = 5

    uid = %(username)
    socket = /run/uwsgi/%(project).sock
    chown-socket = %(username):nginx
    chmod-socket = 660
    vacuum = true
    logto = /var/www/%(username)/log/uwsgi.log

/etc/systemd/system/uwsgi.service:

[Unit]
Description=uWSGI Emperor service

[Service]
ExecStartPre=/usr/bin/bash -c 'mkdir -p /run/uwsgi; chown hedgehog:nginx/run/uwsgi'
ExecStart=/usr/bin/uwsgi --emperor /etc/uwsgi/sites
Restart=always
KillSignal=SIGQUIT
Type=notify
NotifyAccess=all

[Install]
WantedBy=multi-user.target

The app is in /var/www/hedgehog/code.

Seems that uwsgi somehow doesn't use the virtualenv. I've tried adding this to the ini file:

plugins = python
virtualenv = %(home)

It didn't help.

However, if I run "import site" in python interpreter in this virtualenv it gives no error:

[rreimche@rreimche-web sites]$ sudo -u hedgehog -H bash -l
[sudo] password for rreimche: 
[hedgehog@rreimche-web sites]$ python
Python 2.7.5 (default, Jun 24 2015, 00:41:19) 
[GCC 4.8.3 20140911 (Red Hat 4.8.3-9)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import site
>>> exit()

Upvotes: 3

Views: 3450

Answers (3)

Dzhuang
Dzhuang

Reputation: 1995

This also happened to me when I switch my virtualenv to use Python 3, it had been working well under Python 2.7. Here's how I solve it:

replace

plugins = python

with

plugins = python3

Upvotes: 0

Vingtoft
Vingtoft

Reputation: 14656

I had the same error and a mistake: I used a Python3 virtualenv by mistake. Once I used a Python2.7 virtualenv it worked.

Cheers

Upvotes: 1

You don't really need the virtualenv entry in your config since it's the same option as home entry. /%(username)/.virtualenv/%(username) should resolve as /hedgehog/.virtualenv/hedgehog. What does ls -l /hedgehog/.virtualenv/hedgehog say? You may also want to check that you are loading the python plugin for the very same version of python you are using creating your virtualenv. Please paste more log, there may be more hints on what is wrong.

Upvotes: 0

Related Questions