Some of the django source files hidden on pip are saved with the end of the Windows line: they end with \r\n , not \n . On * nix systems, this leads to a violation of the import in the affected files, because instead of searching for django.core.wsgi it tries to import django.core.wsgi\r .
This is why django.core.handlers can still be imported: the __init__.py file is empty, so there are no line endings to corrupt.
To fix this, run dos2unix in the affected files. I'm not sure how many files are really affected (except that many of them are), so I just hit them from the Python script from the interpreter:
import os from os.path import abspath, join for root, _, files in os.walk('/usr/local/lib/python3.4/site-packages/django'): for f in files: os.system('dos2unix %s' % abspath(join(root, f)))
Et voilà, more import errors!
Storytime
I stumbled upon this problem after I started hacking django source files in desperation. I edited django/core/__init__.py (usually empty), adding the following:
from . import wsgi
I modified index.wsgi to include this:
import django.core django.core.wsgi # no-op to see if we can access it
and ended with a fascinating new mistake:
Traceback (most recent call last): File "/usr/local/treehouse/wsgi/index.wsgi", line 11, in <module> import django.core File "/usr/local/lib/python3.4/site-packages/django/core/__init__.py", line 1, in <module> from . import wsgi File "/usr/local/lib/python3.4/site-packages/django/core/wsgi.py", line 1, in <module> from django.core.handlers.wsgi import WSGIHandler ImportError: No module named 'django.core.handlers.wsgi'
So, from django/core/__init__.py I can access django/core/wsgi.py But django.core.handlers.wsgi is out of reach. I removed the changes to reproduce the original error, but the error has changed. Now I was getting No module named 'django.core.handlers.wsgi' even without explicit relative imports.
That was when he hit me. I opened django/core/handlers/wsgi.py in gedit, hit the end of the line, hit the space bar, delete the insert and save the file. It was supposed to be non-op. But when I restarted the server, suddenly the import error moved to another django import. The only logical explanation was that the line ending was wrong, and by repeatedly saving the file in gedit, I silently fixed them.