ImportError: no module named django.core.wsgi

I move the web application from Windows to CentOS 5.11 and Apache, and with everything installed, I get 500 when I try to load the site. The error log shows this:

 mod_wsgi (pid=5461): Target WSGI script '/usr/local/treehouse/wsgi/index.wsgi' cannot be loaded as Python module. mod_wsgi (pid=5461): Exception occurred processing WSGI script '/usr/local/treehouse/wsgi/index.wsgi'. Traceback (most recent call last): File "/usr/local/treehouse/wsgi/index.wsgi", line 11, in <module> from django.core.wsgi import get_wsgi_application ImportError: No module named 'django.core.wsgi' 

I found this question (and related ones) that seems similar, but none of the answers fix the problem. I do not work in virtualenv , and django seems to be installed correctly, since I can run an abusive statement:

 >>> from django.core.wsgi import get_wsgi_application 

in an interactive Python interpreter, and it works just fine. Here the script throws an error (it's just the default value of index.wsgi that you see wherever you look for this stuff):

 import os, sys sys.path.append('/usr/local/treehouse/apple') sys.path.append('/usr/local/treehouse/apple/apple') os.environ.setdefault("DJANGO_SETTINGS_MODULE", "apple.settings") from django.core.wsgi import get_wsgi_application application = get_wsgi_application() 

Obviously, the last line causes an error, but here's the interesting part: the last line remains a problem, even if I add it a little higher:

 import django.core import django.core.handlers 

Both basic import packages without a hitch ( django.core.wsgi is just a thin shell around some functionality in django.core.handlers.wsgi ). This only happens when I try to access wsgi packages that are having problems. Checking sys.path inside this script shows all the correct directories ( /usr/local/lib/python3.4/site-packages , etc.). And again, running the same code from an interactive interpreter does not cause errors.

I tried uninstalling / reinstalling django ( pip install django==1.6.5 ), then I reset the virtual machine to a completely clean snapshot and rebuild / reinstall everything, and yet I get the same behavior.

What's happening?

+5
source share
1 answer

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.

+3
source

Source: https://habr.com/ru/post/1211556/


All Articles