Target WSGI script cannot be loaded as Python module

PythonDjangoApacheMod WsgiWsgi

Python Problem Overview


I am trying to deploy mod_wsgi with apache to run a django application but I am getting an error 500 internal server error The apache logs shows:

[Thu Jun 23 14:01:47 2011] [error] [client 152.78.95.64] mod_wsgi (pid=16142): Exception occurred processing WSGI script '/home/user/bms/apache/django.wsgi'.
[Thu Jun 23 14:01:47 2011] [error] [client 152.78.95.64] Traceback (most recent call last):
[Thu Jun 23 14:01:47 2011] [error] [client 152.78.95.64]   File "/home/user/bms/apache/django.wsgi", line 13, in <module>
[Thu Jun 23 14:01:47 2011] [error] [client 152.78.95.64]     import django.core.handlers.wsgi
[Thu Jun 23 14:01:47 2011] [error] [client 152.78.95.64] ImportError: No module named django.core.handlers.wsgi

My apache virtual host is as follows:

<VirtualHost *:80>

    DocumentRoot /home/user/bms

    <Directory /home/user/bms>
        Order allow,deny
        Allow from all
    </Directory>

WSGIDaemonProcess bms user=user group=user processes=2 threads=25 python-path=/usr/local/lib/python2.7/site-packages


    WSGIProcessGroup bms

    WSGIScriptAlias / /home/user/bms/apache/django.wsgi

</VirtualHost>

And the referenced wsgi file in my app directory with 0777 permissions:

import os
import sys

path = '/home/user/bms'
if path not in sys.path:
    sys.path.append(path)

os.environ['DJANGO_SETTINGS_MODULE'] = 'bms.settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

I heard that this may be because the apache user does not have the correct permissions. However I have no idea how to fix this. I also tried starting the deamon with the www-data user and this did not solve the issue.

EDIT:

I solved this by copying the virtual hosts file into the default one and then disabling the old one with a2dissite. I have no idea how I can do it "properly" and set it so apache goes to the virtual host I want it to though.

Python Solutions


Solution 1 - Python

For me the problem was wsgi python version mismatch. I was using python 3, so:

$ sudo apt-get remove libapache2-mod-python libapache2-mod-wsgi
$ sudo apt-get install libapache2-mod-wsgi-py3

Warning from @alxs before you copy/paste these commands:
If there are python 2 projects running on the server that use wsgi and apache, the above commands will effectively shut them down.

Solution 2 - Python

For me the issue was that the WSGI script wasn't executable.

sudo chmod a+x django.wsgi

or just

sudo chmod u+x django.wsgi

so long as you have the correct owner

Solution 3 - Python

I had a similar problem with this error message in the logs:

> Target WSGI script '/home/web2py/wsgihandler.py' cannot be loaded as Python module.

The solution was the deletion of an incorrect WSGIPythonHome directive (pointing to the application directory) from /etc/httpd/conf.d/wsgi.conf

I'm on RedHat using CentOS repositories.

Recommend following Graham Dumpleton's installation/configuration instructions. Testing configuration against the helloworld application showed me that mod_wsgi was working and the configuration was at fault.

However, the error message gave little clue as to what was wrong.

Solution 4 - Python

I had the same problem and at first I didn't realise I could scroll further down and see the actual error message. In my case, it was an import error:

ImportError: No module named bootstrap3

After installing it via pip (pip install django-bootstrap3), I restarted Apache and it worked.

Solution 5 - Python

Appending path in wsgi.py is the direction, but instead of appending django appending path sys.path.append("/path/to/virtual/environment/lib/pythonX.X/site-packages") fixed my case.

This is for a django project using python2.7 on ubuntu 16.04.

Solution 6 - Python

I know this question is pretty old, but I wrestled with this for about eight hours just now. If you have a system with SELinux enabled and you've put your virtualenv in particular places, mod_wsgi won't be able to add your specified python-path to the site-packages. It also won't raise any errors; as it turns out the mechanism it uses to add the specified python-path to the site packages is with the Python site module, specifically site.adduserdir(). This method doesn't raise any errors if the directory is missing or can't be accessed, so mod_wsgi also doesn't raise any errors.

Anyway, try turning off SELinux with

sudo setenforce 0

or by making sure that the process you're running Apache as has the appropriate ACLs with SELinux to access the directory the virtualenv is in.

Solution 7 - Python

Did you try it without the WSGIDaemonProcess option?

I had no trouble setting up mod_wsgi at home, but did it without the daemon option. You mentioned solving by moving around virtual hosts files and I note this caveat in the docs for WSGIDaemonProcess:

> Also note that the name of the daemon > process group must be unique for the > whole server. That is, it is not > possible to use the same daemon > process group name in different > virtual hosts.

Don't know if that's coincidence.

Solution 8 - Python

In my own case on windows in xampp, I was incorrectly loading the application path in the wsgi.py file like so:

Incorrect:

import sys

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

application = get_wsgi_application()
sys.path.append('C:/xampp/htdocs/advisory_portal/advisory_portal')
sys.path.append('C:/xampp/htdocs/advisory_portal')
    

Instead of:

Correct:

import sys

sys.path.append('C:/xampp/htdocs/advisory_portal/advisory_portal')
sys.path.append('C:/xampp/htdocs/advisory_portal')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "advisory_portal.settings")

application = get_wsgi_application()

Don't forget to import sys python package

Solution 9 - Python

I was getting this error. I'm using python 3 in a virtual environment found this in my apache logs

> [Fri Dec 21 08:01:43.471561 2018] [mpm_prefork:notice] [pid 21786] > AH00163: Apache/2.4.6 (Red Hat Enterprise Linux) mod_wsgi/3.4 > Python/2.7.5 configured -- resuming normal operations

I had installed wsgi using yum -y install mod_wsgi. This had installed mod_wsgi compiled for python 2. So I uninstalled it

yum remove mod_wsgi

and installed mod_wsgi compiled for python 3 using

yum install python35u-mod_wsgi

It worked after that

Solution 10 - Python

I had the same error

> Target WSGI script cannot be loaded as Python module

This was because the python path was not added in the apache.conf

Depending on the Apache and Ubuntu version you need to update the apache.conf or httpd.conf file with the pythonWSGIPath

In my case I need to edit the apache.conf

sudo nano /etc/apache2/apache2.conf

Then add the following line at the end (Change the my_project to the project your project name)

WSGIPythonPath /var/www/my_project

And everything works with charm

Solution 11 - Python

As this question became sort of a pool for collecting solutions for problems that result in the error giving this question its title, I'd like to add this one, too.

In my case, I want to run OpenStack Keystone (Ocata) using Apache and WSGI on Ubuntu 16.04.2. The processes start but as soon as I query keystone I get

mod_wsgi (pid=20103): Target WSGI script '/opt/openstack/bin/keystone-wsgi-public' cannot be loaded as Python module.

I had two vhosts, one had

WSGIDaemonProcess keystone-public ...
WSGIProcessGroup keystone-public ...

while the other had

WSGIDaemonProcess keystone-admin ...
WSGIProcessGroup keystone-admin ...

I solved the problem by renaming them. The vhost entries now read:

WSGIDaemonProcess kst-pub ...
WSGIProcessGroup kst-pub ...

and

WSGIDaemonProcess kst-adm ...
WSGIProcessGroup kst-adm ...

I didn't investigate any further. Solved as works for me.

Solution 12 - Python

If you install your project’s Python dependencies inside a virtualenv, you’ll need to add the path to this virtualenv’s directory to your Python path as well. To do this, add an additional path to your WSGIPythonPath directive, with multiple paths separated by a colon (:) if using a UNIX-like system, or a semicolon (;) if using Windows

Solution 13 - Python

Adding on to the list this is how I got it working.

I was trying to install CKAN 2.7.2 on CentOS 7 from source and kept coming up against this error. For me it was because SELinux was enabled. I didn't need to disable it. Instead, after reading https://www.endpoint.com/blog/2010/10/13/selinux-httpd-modwsgi-26-rhel-centos-5, I found that turning on httpd_can_network_connect fixed it:

setsebool -P httpd_can_network_connect on

From that page:

> httpd_can_network_connect - Allows httpd to make network connections, > including the local ones you'll be making to a database

Solution 14 - Python

I had similar issue eg apache log error "wsgi.py cannot be loaded as Python module."

It turned out I had to stop and then start apache instead of just restarting it.

Solution 15 - Python

I had the same problem and it got solved using

sudo easy_install cx_Oracle

but remember to unistall cx_oracle before installing it using easy_install.

Command to uninstall: pip uninstall cx_oracle

Solution 16 - Python

The solution that finally worked for me, after trying many of these options unsuccessfully was simple, but elusive because I struggled to figure out what actual paths to use.

I created a mezzanine project, which is based on django, with the following commands. I list them here to make the paths explicit.

/var/www/mysite$ python3 -m venv ./venv
/var/www/mysite$ source ./venv/bin/activate
(venv) /var/www/mysite$ mezzanine-project mysite
(venv) /var/www/mysite$ cd mysite
(venv) /var/www/mysite/mysite$

Now, the path to the wsgi.py file is:

/var/www/mysite/mysite/mysite/wsgi.py

The directives that worked for this installation within my /etc/apache2/sites-available/mysite.conf file follow:

...<VirtualHost...>
    ...
    WSGIDaemonProcess mysite python-home=/var/www/mysite/venv python-path=/var/www/mysite/mysite
    WSGIProcessGroup mysite
    WSGIScriptAlias / /var/www/mysite/mysite/mysite/wsgi.py process-group=accounting
    <Directory /var/www/mysite/mysite/mysite>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>
    ...
</VirtualHost>...

I tried numerous versions of python-home and python-path and got the OP's error repeatedly. Using the correct paths here should also accomplish the same things as @Dev's answer without having to add paths in the wsgi.py file (supplied by mezzanine, and no editing necessary in my case).

Solution 17 - Python

Sometimes when I get stuck on this I hard code a path to project in the wsgi file like:

import os
import sys


sys.path.append("/var/www/html/myproject")
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")

application = get_wsgi_application()

Solution 18 - Python

I recommend trying to downgrade DJANGO to version 2.1.1.

Solution 19 - Python

Making sure my project's venv folder resides inside my project's root directory solved this problem for me.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAdam ThomasView Question on Stackoverflow
Solution 1 - PythonnimaView Answer on Stackoverflow
Solution 2 - PythonGerryView Answer on Stackoverflow
Solution 3 - Pythonuser475443View Answer on Stackoverflow
Solution 4 - PythondnmhView Answer on Stackoverflow
Solution 5 - PythonlydfenView Answer on Stackoverflow
Solution 6 - PythonAlex Van LiewView Answer on Stackoverflow
Solution 7 - Pythonuser447688View Answer on Stackoverflow
Solution 8 - PythonDevView Answer on Stackoverflow
Solution 9 - PythonLeroy KayandaView Answer on Stackoverflow
Solution 10 - PythonTrectView Answer on Stackoverflow
Solution 11 - Pythonuser1129682View Answer on Stackoverflow
Solution 12 - PythondongView Answer on Stackoverflow
Solution 13 - PythonJohn DoyleView Answer on Stackoverflow
Solution 14 - PythoncurtispView Answer on Stackoverflow
Solution 15 - PythonBickyView Answer on Stackoverflow
Solution 16 - PythonmightypileView Answer on Stackoverflow
Solution 17 - PythonAtmaView Answer on Stackoverflow
Solution 18 - PythonBaxa VoiceView Answer on Stackoverflow
Solution 19 - PythonTextbookView Answer on Stackoverflow