Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
975 views
in Technique[技术] by (71.8m points)

python - ImportError: No module named django.core.wsgi for uwsgi

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()
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...