[Fixed]-ImportError: No module named django.core.wsgi when deploying with nginx

1👍

Well, the error is simple:

Traceback (most recent call last):
  File "microbird/wsgi.py", line 20, in <module>
    from django.core.wsgi import get_wsgi_application
ImportError: No module named django.core.wsgi

django isn’t on your Python’s module search path. To figure out why that is so, I would suggest putting some debug code in your wsgi.py file, here’s a start:

import sys

def application(environ, start_response):
    status = '200 OK'

    output = 'sys.path = %s\n' % repr(sys.path)
    output += 'sys.version = %s\n' % repr(sys.version)
    output += 'sys.prefix = %s\n' % repr(sys.prefix)

    response_headers = [('Content-type', 'text/plain'),
                        ('Content-Length', str(len(output)))]
    start_response(status, response_headers)

    return [output]

(under the assumption that either your sys.path or your Python is the reason for django not being found).

If you’re trying to use a virtualenv, then you need to activate it. Here’s how I do it (in the wsgi.py file):

VIRTUAL_ENV = '/path/to/root/of/virtualenv/'
# activate virtualenv
_activate = "%s/%s/activate_this.py" % (
    VIRTUAL_ENV,
    'Scripts' if sys.platform == 'win32' else 'bin'
)
if sys.version_info >= (3, 0):
    exec(compile(open(_activate, 'rb').read(), _activate, 'exec'))
else:
    execfile(_activate, dict(__file__=_activate))

I’m not sure how well the python 3 branch works (never been used).

Leave a comment