Target WSGI script cannot be loaded as Python module
Solution 1
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
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
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
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
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.
![Admin](/assets/logo_square_200-5d0d61d6853298bd2a4fe063103715b4daf2819fc21225efa21dfb93e61952ea.png)
Admin
Updated on July 05, 2022Comments
-
Admin almost 2 years
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.
-
Graham Dumpleton about 12 yearsAfter the "Target WSGI script '/home/web2py/wsgihandler.py' cannot be loaded as Python module." line in log there should have been a bunch of other stuff including Python traceback and exception message. There should have been a lot more information than that, that is only the first line and not the real information.
-
user83039 over 9 yearsWho should be the owner of
wsgi.py
? I haveroot
set as group/owner. -
Gerry over 9 yearsSorry I can't remember. That was back in 2013. Try
sudo chmod a+x django.wsgi
and that will allow execution no matter who tries to run it. If that works then some searching on google might turn up the answer. -
jsetting32 about 9 yearswhere is the django.wsgi file?
-
Gerry about 9 yearsWell for the Op, the file is located at /home/user/bms/apache/django.wsgi. You may need to use the "locate" (or mlocate) command: thegeekstuff.com/2012/03/locate-command-examples
-
alxs over 7 yearsBefore someone copies/pastes the above, note that if there are python 2 projects running on the server that use wsgi and apache, the above commands will effectively shut them down.
-
Mahn over 7 yearsSolved my issue after paying close attention to the traceback, listen to @GrahamDumpleton because he's right.
-
Graham Dumpleton over 7 yearsAs the author of mod_wsgi I know of no length limitation on the name of the process group. I would suspect the problem was elsewhere and your restart of Apache probably resolved it.
-
user1129682 over 7 years@GrahamDumpleton Maybe, but that had to be a problem that is implicitly fixed with an apache restart, because it was a fresh deployment and the config files came from my repository. I definetly only changed these four lines between restarts. Not unlikely though! Maybe keystone trips over not existing log files and or directories which are implicitly created, but too late. After a restart these files/directories are there.
-
User about 4 years@alxs So I guess there's no way to run Python 2 and 3 projects on Apache?
-
alxs almost 4 years@User I haven't tried to do something like that tbh, so I can't really tell. Maybe this answers your question though: stackoverflow.com/questions/14375520/…. In any case, it could prove troublesome (if it's possible) and given that Python 2 is officialy not maintaiined anymore, I think it would be a good choice to consider moving to Python 3.
-
Almett over 3 yearsThis solution finally helped. Thank you so much!