WSGI Script Cannot Be Loaded as Python Module — 500 Internal Server Error

According to my error log, my Target WSGI Script cannot be loaded as a Python module, but at the end of my log, I also get an ImportError: No module named foodshop.settings. I can verify that the module exists.

Here is my error log:

[Tue Sep 15 03:49:04.828560 2015] [mpm_event:notice] [pid 7384:tid 140261847136128] AH00491: caught SIGTERM, shutting down
[Tue Sep 15 03:49:05.880294 2015] [mpm_event:notice] [pid 7539:tid 139824273098624] AH00489: Apache/2.4.7 (Ubuntu) mod_wsgi/3.4 Python/2.7.6 configured -- resuming normal operations
[Tue Sep 15 03:49:05.880314 2015] [core:notice] [pid 7539:tid 139824273098624] AH00094: Command line: '/usr/sbin/apache2'
[Tue Sep 15 03:49:34.435217 2015] [:error] [pid 7543:tid 139824089818880] [remote] mod_wsgi (pid=7543): Target WSGI script '/home/ubuntu/gather/src/foodshop/' cannot be loaded as Python module.
[Tue Sep 15 03:49:34.435250 2015] [:error] [pid 7543:tid 139824089818880] [remote] mod_wsgi (pid=7543): Exception occurred processing WSGI script '/home/ubuntu/gather/src/foodshop/'.
[Tue Sep 15 03:49:34.435290 2015] [:error] [pid 7543:tid 139824089818880] [remote] Traceback (most recent call last):
[Tue Sep 15 03:49:34.435307 2015] [:error] [pid 7543:tid 139824089818880] [remote]   File "/home/ubuntu/gather/src/foodshop/", line 16, in <module>
[Tue Sep 15 03:49:34.435360 2015] [:error] [pid 7543:tid 139824089818880] [remote]     application = get_wsgi_application()
[Tue Sep 15 03:49:34.435372 2015] [:error] [pid 7543:tid 139824089818880] [remote]   File "/usr/local/lib/python2.7/dist-packages/django/core/", line 14, in get_wsgi_application
[Tue Sep 15 03:49:34.435406 2015] [:error] [pid 7543:tid 139824089818880] [remote]     django.setup()
[Tue Sep 15 03:49:34.435417 2015] [:error] [pid 7543:tid 139824089818880] [remote]   File "/usr/local/lib/python2.7/dist-packages/django/", line 17, in setup
[Tue Sep 15 03:49:34.435449 2015] [:error] [pid 7543:tid 139824089818880] [remote]     configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
[Tue Sep 15 03:49:34.435460 2015] [:error] [pid 7543:tid 139824089818880] [remote]   File "/usr/local/lib/python2.7/dist-packages/django/conf/", line 48, in __getattr__
[Tue Sep 15 03:49:34.435521 2015] [:error] [pid 7543:tid 139824089818880] [remote]     self._setup(name)
[Tue Sep 15 03:49:34.435532 2015] [:error] [pid 7543:tid 139824089818880] [remote]   File "/usr/local/lib/python2.7/dist-packages/django/conf/", line 44, in _setup
[Tue Sep 15 03:49:34.435545 2015] [:error] [pid 7543:tid 139824089818880] [remote]     self._wrapped = Settings(settings_module)
[Tue Sep 15 03:49:34.435552 2015] [:error] [pid 7543:tid 139824089818880] [remote]   File "/usr/local/lib/python2.7/dist-packages/django/conf/", line 92, in __init__
[Tue Sep 15 03:49:34.435562 2015] [:error] [pid 7543:tid 139824089818880] [remote]     mod = importlib.import_module(self.SETTINGS_MODULE)
[Tue Sep 15 03:49:34.435570 2015] [:error] [pid 7543:tid 139824089818880] [remote]   File "/usr/lib/python2.7/importlib/", line 37, in import_module
[Tue Sep 15 03:49:34.435604 2015] [:error] [pid 7543:tid 139824089818880] [remote]     __import__(name)
[Tue Sep 15 03:49:34.435619 2015] [:error] [pid 7543:tid 139824089818880] [remote] ImportError: No module named foodshop.settings

Additionally, here is my apache2.conf file:

Mutex file:${APACHE_LOCK_DIR} default


Timeout 300

KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5

# These need to be set in /etc/apache2/envvars

HostnameLookups Off

ErrorLog ${APACHE_LOG_DIR}/error.log

LogLevel warn

IncludeOptional mods-enabled/*.load
IncludeOptional mods-enabled/*.conf

Include ports.conf

<Directory />
        Options FollowSymLinks
        AllowOverride None
        Require all denied

<Directory /usr/share>
        AllowOverride None
        Require all granted

<Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted

AccessFileName .htaccess

<FilesMatch "^\.ht">
        Require all denied

LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %O" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent

IncludeOptional conf-enabled/*.conf

IncludeOptional sites-enabled/*.conf

ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
CustomLog ${APACHE_LOG_DIR}/access.log combined

WSGIScriptAlias / /home/ubuntu/gather/src/foodshop/

<Directory /home/ubuntu/gather/src/foodshop/>
    WSGIProcessGroup gather
    WSGIApplicationGroup %{GLOBAL}
    Options All
    AllowOverride All
    Require all granted

Alias /media/ /home/ubuntu/gather/src/foodshop/media/
<Directory /home/ubuntu/gather/src/foodshop/media/>
    Options FollowSymLinks MultiViews
    Order deny,allow
    Allow from all

Alias /static/ /home/ubuntu/gather/src/foodshop/static/
<Directory /home/ubuntu/gather/src/foodshop/static/>
    Options FollowSymLinks MultiViews
    Order allow,deny
    Allow from all

And my 000-default.conf file:

<VirtualHost *:80>

            ServerAdmin webmaster@localhost
            DocumentRoot /var/www/html

            ErrorLog ${APACHE_LOG_DIR}/error.log
            CustomLog ${APACHE_LOG_DIR}/access.log combined

            Alias /static /home/ubuntu/gather/static
            <Directory /home/ubuntu/gather/static>
                    Require all granted

            <Directory /home/ubuntu/gather/src/foodshop>
                            Require all granted

            WSGIDaemonProcess gather processes=2 threads=15
            WSGIProcessGroup gather
            WSGIScriptAlias / /home/ubuntu/gather/src/foodshop/


And finally, this is my file:

import os

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "foodshop.settings")

from django.core.wsgi import get_wsgi_application

application = get_wsgi_application()

I'm connected to an EC2 instance (Ubuntu 14.04), and I'm running only 1 app on this instance. When I run this on a localhost, it works perfectly.

Answers 1

  • You haven't done anything to put your app - and its settings file - on your Python path.

    If you're not using a virtualenv (though you should be), the simplest thing to do would be to do in your wsgi file:

    import os, sys
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "foodshop.settings")

Related Articles