4

I am trying to deploy a Django app using nginx + uwsgi. I created a virtual environment (virtualenv), and installed both uwsgi and Django inside the virtual env (i.e.local to the virtual environment). I have no global Django and uwsgi. When I run the uwsgi --ini project.ini, I am having an 'ImportError: No module named django.core.wsgi' exception:

from django.core.wsgi import get_wsgi_application
ImportError: No module named django.core.wsgi
unable to load app 0 (mountpoint='') (callable not found or import error)
*** no app loaded. going in full dynamic mode ***
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 5987)
spawned uWSGI worker 1 (pid: 5988, cores: 1)
spawned uWSGI worker 2 (pid: 5989, cores: 1)
spawned uWSGI worker 3 (pid: 5990, cores: 1)
spawned uWSGI worker 4 (pid: 5991, cores: 1)

Based on my search, it's recommended to put env and pythonpath variables in the ini if you are using Django1.5 or lower. However, I am using Django 1.7, so I did not place them anymore. Here's my project.ini:

#project.ini file
[uwsgi]

# Django-related settings
# the base directory (full path)
chdir           = /root/virtualenv/project
# Django wsgi file
module          = project.wsgi:application
# the virtualenv (full path)
home            = /root/virtualenv

# process-related settings
# master
master          = true
# maximum number of worker processes
processes       = 4
# the socket (use the full path to be safe
socket          = /root/virtualenv/project/project.sock
# ... with appropriate permissions - may be needed
chmod-socket    = 666
chown-socket    = root:root
# clear environment on exit
vacuum          = true

# other config options
uid = root
gid = root
processes = 4
daemonize = /var/log/uwsgi/project.log
no-site = True

How will i fix this? I'm quite stuck on this for a day already. Any ideas are greatly appreciated. Thanks in advance!

4
  • But how are you running uwsgi? You usually just need to activate the virtualenv before you run it. Commented Feb 3, 2015 at 10:52
  • That's my question; how to run uwsgi... I did activate the virtual environment then run uwsgi. But as mentioned, I got the error above Commented Feb 3, 2015 at 11:53
  • But what did you do when you got that error? Please show the exact sequence of commands you entered. Commented Feb 3, 2015 at 12:08
  • 1.) install python2.7 (with pip and virtualenv) 2.) virtualenv 3.) source bin/activate 4.) install Django 5. install uWSGI 6.) modified project.wsgi 7.) run uwsgi --ini project.ini Commented Feb 3, 2015 at 12:22

4 Answers 4

2

your module is pointed to your project, shouldn't it be pointed to your projects main app that way it can find the wsgi file?

so on my INI file looks like this.

In my particular case I'm using a virtual environment, django 1.7 and uwsgi.

vhost = true
plugins = python
socket = /tmp/noobmusic.sock
master = true
enable-threads = true
processes = 2
wsgi-file = /home/myname/webapps/music/music/music/wsgi.py
virtualenv = /home/myname/webapps/music/musicenv/
chdir = /home/myname/webapps/music/music/

this is the only site I've ever setup uwsgi as I typically use mod-wsgi and unfortunately do not remember all the steps.

Sign up to request clarification or add additional context in comments.

4 Comments

which variable is that?
# Django wsgi file module = project.wsgi:application
i think it can find my wsgi file because chdir is set to /root/virtualenv/project. When this directory is entered, wsgi.py is located in project (so /root/virtualenv/project/project/wsgi.py). Thanks anyway
I updated my answer to provide an example of what my .ini file looks like. Unfortunately I'm not that experienced running uwsgi behind virtualenv. but I do have this site successfully working.
2

I had similar issue. Solved it -after many hours- by making sure that uwsgi is installed using same python version (2 / 3) as the python version of your virtualenv. Otherwise it will not use your virtualenv and thus start throwing 'can not find module xyz' kind of errors. To install uwsgi under python3 you have to use pip3 (which in turn might need to be installed with something like 'apt-get install python-pip3'). When calling uwsgi on cli or via .ini file you need to reference your virtualenv mentioning the full path (which ends one folderlevel above the folder in which the /bin/ is; so /example/myvenv/bin/activate means the full path is /example/myvenv.

I made the uwsgi install global so outside of my virtualenv. I suppose above applies/would work as well when installing uwsgi within the virtualenv, but have not tried (yet).

Comments

1

Keep the system-wide uwsgi the same version as your virtual environment python. In my environment, my virtual environment is python3.7, but the system default python is python3.6. After I uninstall uWSGI, and re-install the system-wide uWSGI with python3.7, the problem has been resolved.

sudo pip uninstall uwsgi
sudo -H python3.7 -m pip install uwsgi

Comments

0

I can't see any problem in your configuration (though I'm not very good at these topics). I can try to advice some steps to localize the problem.

  • Test uwsgi without using virtualenv. Note that the virtual directory is just a directory, so add it to your PYTHONPATH and run uwsgi.

    Before that you can try

    python -c 'import django.core.wsgi'

    If that works, then the problem is in uwsgi virtualenv configuration.

  • Test virtualenv. Run it and check that the module can be imported.

    If that works, then the problem is in uwsgi. Go to the previous case.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.