ImportError: No module named django.core.wsgi (ubuntu)

12,820

Solution 1

Based on: http://www.webforefront.com/django/setupapachewebserverwsgi.html

the www-data user (user running apache folder) must have read access to the folder containing the django installation. I just did a test where I copied the django installation and my web application to: /var/www/test and changed the permission for that folder:

sudo chgrp -R www-data /var/www/test
sudo chmod -R g+rwx /var/www/test

and the application now loads.

Solution 2

I got this error just now when I was trying to set up my Django, Nginx and WSGI according to this tutorial: https://uwsgi.readthedocs.io/en/latest/tutorials/Django_and_nginx.html

When I typed in the command: uwsgi --http :8000 --module mysite.wsgi

I get the error:

from django.core.wsgi import get_wsgi_application ImportError: No module named django.core.wsgi

It made no sense to me because when I entered the python shell in my virtualenv and imported the module, it worked perfect.

However, I found this tutorial to be useful in getting rid of the error: https://gist.github.com/evildmp/3094281

uwsgi --http :8000 --module project.wsgi --virtualenv /path/to/virtualenv

Typing in the path to your virtualenv causes WSGI to find the module it was missing.

You can find the path to the virtualenv by typing in $ECHO virtualenv in your command prompt / terminal.

Share:
12,820
u123
Author by

u123

Updated on June 04, 2022

Comments

  • u123
    u123 almost 2 years

    I am following this guide to configure apache for my django web application: https://docs.djangoproject.com/en/1.9/howto/deployment/wsgi/modwsgi/#basic-configuration

    I have installed Django, mod_wsgi-express (the new way) and mod_wsgi for apache on an ubuntu 15.10 server using:

    pip install Django
    pip install mod_wsgi
    sudo aptitude install libapache2-mod-wsgi
    

    Next I have added the below to: /etc/apache2/apache2.conf

    WSGIScriptAlias / /home/user/mysite/mysite/wsgi.py
    WSGIPythonPath /home/user/mysite:/home/user/.local/lib/python2.7/site-packages
    
    <Directory /home/user/mysite/mysite>
    <Files wsgi.py>
    Require all granted
    </Files>
    </Directory>
    

    But when I start apache I get the below error:

    [Sat Mar 26 12:10:06.876025 2016] [wsgi:error] [pid 10622:tid 139806697674496] [client 217.40.250.17:52124]     from django.core.wsgi import get_wsgi_application
    [Sat Mar 26 12:10:06.876057 2016] [wsgi:error] [pid 10622:tid 139806697674496] [client 217.40.250.17:52124] ImportError: No module named django.core.wsgi
    

    But that module indeed exists in the path I have supplied in WSGIPythonPath above:

    ~/.local/lib/python2.7/site-packages/django/core $ cat wsgi.py
    import django
    from django.core.handlers.wsgi import WSGIHandler
    

    And:

    ~/.local/lib/python2.7/site-packages $ ll
    total 24
    drwx------  6 user user 4096 Mar 24 22:25 ./
    drwx------  3 user user 4096 Mar 20 20:56 ../
    drwxrwxr-x 17 user user 4096 Mar 24 22:25 django/
    drwxrwxr-x  2 user user 4096 Mar 24 22:25 Django-1.9.4.dist-info/
    drwxrwxr-x  5 user user 4096 Mar 20 20:59 mod_wsgi/
    drwxrwxr-x  2 user user 4096 Mar 20 20:59 mod_wsgi-4.4.22.egg-info/
    

    I also tried to validate the version of python the mod_wsgi module was build against:

     /usr/lib/apache2/modules $ ldd mod_wsgi.so
        linux-vdso.so.1 =>  (0x00007ffe50f31000)
        libpython2.7.so.1.0 => /usr/lib/x86_64-linux-gnu/libpython2.7.so.1.0 (0x00007f376fbc2000)
        libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f376f9a4000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f376f5d9000)
        libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007f376f3bf000)
        libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f376f1bb000)
        libutil.so.1 => /lib/x86_64-linux-gnu/libutil.so.1 (0x00007f376efb7000)
        libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f376ecaf000)
        /lib64/ld-linux-x86-64.so.2 (0x00005609838b3000)
    

    So it appears it was build against Python 2.7 which is the same version Django is being installed under.

    So why does apache fail to load my application/django?