ImportError: No module named django.core.wsgi for uwsgi
Asked Answered
C

6

27

I'm using uwsgi for my Django(version =1.4) project, but there's an error if I run

uwsgi --ini django.ini
from django.core.wsgi import get_wsgi_application
    ImportError: No module named django.core.wsgi

but I could import django.core.wsgi as follows:

>>> import django.core.wsgi

the django.ini file:

[uwsgi]
chdir=/path/to/my/app
module=app.wsgi:application
master=True
vacuum=True
max-requests=5000
socket=127.0.0.1:9000

wsgi.py

import os

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "app.settings")

# This application object is used by any WSGI server configured to use this
# file. This includes Django's development server, if the WSGI_APPLICATION
# setting points here.
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
Consumable answered 7/1, 2013 at 11:19 Comment(1)
Do you have a local django.py file at all?Curiosa
S
14

If you use virtualenv try to add home to django.ini:

home=/path/to/venv/

To test it through web browser:

uwsgi --ini django.ini --protocol=http
Shift answered 7/1, 2013 at 12:42 Comment(3)
where is the "django.ini" ?Prevot
look at the docShift
Receiving : No module named 'encodings'Hymettus
D
28

If you installed gunicorn on both sudo apt-get install gunicorn and (venv) pip install gunicorn, use sudo apt-get remove gunicorn and restart your virtual environment. This way, it worked for me.

Delaware answered 9/7, 2017 at 19:22 Comment(2)
It is working, Me too first installed using apt-get install gunicorn and then with pip. I removed sudo apt-get remove. It is working fine. ThanksDoralynn
Wow this worked for me, THANKS! I reinstalled Gunicorn using pip3 install gunicorn instead of sudo apt-get install gunicornSour
K
24

The error ImportError: No module named django.core.wsgi generally arises when uwsgi tries reading the wsgi.py file, and comes to the line:

from django.core.wsgi import get_wsgi_application

It can't find this these modules because Django is not installed, or if it is installed, it is not in PYTHONPATH.

If your project is in a virtualenv and Django is only installed in this virtualenv, somehow the path to the Django modules are not in the PYTHONPATH, and thus Python can't find it.

If you are curious, you can insert the following code into the wsgi.py file, and see the PYTHONPATH:

import os
print '===== sys.path / PYTHONPATH ====='
for k in sorted(os.environ.keys()):
    v = os.environ[k]
    print ('%-30s %s' % (k,v[:70]))

If you run a local version of uwsgi, installed in the virtualenv, then the path will be set correct, but if you run a global version of uwsgi it will normally not have the PYTHONPATH set correctly.

You can tell uWSGI the path to the virtualenv, and it will figure out the correct PYTHONPATH. Just use the --virtualenv command line argument, eg:

uwsgi --http :8001 --module wsgi --virtualenv /home/jdoe/myvirtualenv

(The following arguments does exactly the same as --virtualenv: --venv, --home, -H)

Surprisingly, setting $VIRTUAL_ENV has no effect on PYTHONPATH

Strangely enough, if you don't use the --virtualenv argument, the environment variable $VIRTUAL_ENV will be set correctly. Test this by inserting into wsgi.py:

print os.environ['VIRTUAL_ENV']

This will print:

/home/jdoe/myvirtualenv

But the PYTHONPATH is not set correctly, and does not include anything from the virtualenv.

I can't explain why this is.

Kyongkyoto answered 19/12, 2014 at 15:25 Comment(1)
The --virtualenv was the key! Thanks for the perfect analysis @mads-skjernBissextile
S
14

If you use virtualenv try to add home to django.ini:

home=/path/to/venv/

To test it through web browser:

uwsgi --ini django.ini --protocol=http
Shift answered 7/1, 2013 at 12:42 Comment(3)
where is the "django.ini" ?Prevot
look at the docShift
Receiving : No module named 'encodings'Hymettus
V
3

In my case, I installed the Django application and everything else for Python3, but the uwsgi was using Python2. Just check the log while running uwsgi whether it is using Python2 or Python3, and reinstall uwsgi if it is not consistent. Look for the line similar to below line in uwsgi startup log.

Python version: 3.4.3 (default, Oct 14 2015, 20:31:36) [GCC 4.8.4] VS Python version: 2.7.6 (default, Jun 22 2015, 18:01:27) [GCC 4.8.2]

Vetavetch answered 31/5, 2016 at 8:44 Comment(0)
E
0

Since you accepted the answer which mentions virtualenv, it seems that you use it. In this case make sure that django is installed in your virtualenv directory (say venv).

You can separately install it from pip under virtualenv or manually create a symbolic link (if you are on Unix-like system) to venv's site-packages

ln -s /usr/path_to_django venv/lib/python2.7/site-packages/django
Ettieettinger answered 17/4, 2015 at 9:42 Comment(0)
I
0

I received this error because I created the virtual environment in a shared folder of virtualbox which didn't allow symbolic links. I recieved some errors but everything seemed to work so I continued until I got this error. The problem was solved when I recreated my virtual environment, made sure there were no errors and pointed uwsgi to the folder.

Incubator answered 25/9, 2016 at 1:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.