Forums

Django error "No module named core.handlers.wsgi"

Hey, i'm trying to deploy my app but i'm stuck on this error.

ImportError: No module named core.handlers.wsgi

I have no idea why this happens, I've tried to debug this via help section and google search, ended up with importing manually dist-packages and site-packages as suggested, but these hints aren't making any difference. Here's my wsgi file:

path = '/home/xolir/goodjob/kasztany/myproject/kasztan/settings/'
if path not in sys.path:
    sys.path.append(path)
#
os.environ['DJANGO_SETTINGS_MODULE'] = 'base'

path = '/home/xolir/goodjob/kasztany/lib/python2.7/site-packages/'
if path not in sys.path:
    sys.path.append(path)

path = '/usr/local/lib/python2.7/dist-packages/'
if path not in sys.path:
    sys.path.append(path)

#
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

I've tried this in shell, and both settings file and import are working withing declared paths.

Thanks in advance!

Which version of Python/Django are you using? More recent versions have a different way of importing the WSGI handler:

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

Currently it is python 2.7 and django 1.3.7, I've tried to make it to latest version, but --upgrade is not working, giving an error instead

OSError: [Errno 13] Permission denied: '/usr/local/bin/django-admin.py'

Your best bet for using a different version of django is to use a virtualenv. Check out this guide? https://www.pythonanywhere.com/wiki/VirtualEnvForNewerDjango

I'm using one already, which I downloaded from my PC via git, but it seems like I cannot port it fully (everything besides Django version is as i put it on my PC in my virtualenv), because of this error I've shown above.

virtualenvs aren't really portable, so you shouldn't try to copy one from one computer to another. instead, create the virtualenv from scratch on each machine...

It's common to use a requirements.txt for this sort of thing, and keep that in your git repo...

# on your pc
pip freeze > requirements.txt
git add requirements.txt
git commit -m"add requirementst.txt"
git push

# on pythonanywhere\
git pull
source virtualenvwrapper.sh
mkvirtualenv --python=/usr/bin/python2.7 django17
pip install -r requirements.txt

And then you'll need to edit your wsgi file to get it to point at the new virtualenv activate_this.

Incidentally, if you haven't already, it's worth adding virtualenvwrapper to your bashrc:

echo '' >> ~/.bashrc && echo 'source virtualenvwrapper.sh' >> ~/.bashrc

Final tip -- remember to use

workon django17

to activate your virtualenv whenever you're working in a bash console. You should see the prompt change from this:

14:51 ~ $

to this

(django17)14:51 ~ $ which pip

which tells you the virtualenv is active. that 'permission denied' error will only happen if you've not activated your virtualenv...