82👍
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.
23👍
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
- [Django]-Bypass confirmation prompt for pip uninstall
- [Django]-Programmatically saving image to Django ImageField
- [Django]-How to write a unit test for a django view?
6👍
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.
- [Django]-How to add clickable links to a field in Django admin?
- [Django]-Django Footer and header on each page with {% extends }
- [Django]-Django, Turbo Gears, Web2Py, which is better for what?
5👍
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.
- [Django]-Object does not support item assignment error
- [Django]-Querying django migrations table
- [Django]-How to view corresponding SQL query of the Django ORM's queryset?
3👍
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.
- [Django]-Django Many-to-Many (m2m) Relation to same model
- [Django]-Django rest framework: query parameters in detail_route
- [Django]-Import error django corsheaders
2👍
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.
- [Django]-How to access request body when using Django Rest Framework and avoid getting RawPostDataException
- [Django]-Composite primary key in django
- [Django]-Creating a dynamic choice field
1👍
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.
- [Django]-How to resize an ImageField image before saving it in python Django model
- [Django]-Can I access constants in settings.py from templates in Django?
- [Django]-How to handle request.GET with multiple variables for the same parameter in Django
1👍
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
- [Django]-Check for pending Django migrations
- [Django]-Define css class in django Forms
- [Django]-Django.db.utils.ProgrammingError: relation "bot_trade" does not exist
1👍
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
- [Django]-Django Rest Framework – Updating a foreign key
- [Django]-Django: guidelines for speeding up template rendering performance
- [Django]-Function decorators with parameters on a class based view in Django
1👍
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
- [Django]-Django-allauth: Linking multiple social accounts to a single user
- [Django]-Django Setup Default Logging
- [Django]-Detect mobile, tablet or Desktop on Django
0👍
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.
- [Django]-How to view corresponding SQL query of the Django ORM's queryset?
- [Django]-Django won't refresh staticfiles
- [Django]-Django 1.3.1 compilemessages. Error: sh: msgfmt: command not found
0👍
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
- [Django]-Why is assertDictEqual needed if dicts can be compared by `==`?
- [Django]-CSS styling in Django forms
- [Django]-In Django models.py, what's the difference between default, null, and blank?
0👍
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
- [Django]-Django delete FileField
- [Django]-Where should signal handlers live in a django project?
- [Django]-How can I get tox and poetry to work together to support testing multiple versions of a Python dependency?
0👍
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.
- [Django]-Rendering a template variable as HTML
- [Django]-Django: show the count of related objects in admin list_display
- [Django]-DateTimeField doesn't show in admin system
0👍
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
- [Django]-What is the SQL ''LIKE" equivalent on Django ORM queries?
- [Django]-Is there a way to negate a boolean returned to variable?
- [Django]-Django: How can I create a multiple select form?
0👍
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).
- [Django]-What does Django's @property do?
- [Django]-How to pull a random record using Django's ORM?
- [Django]-Django: reverse accessors for foreign keys clashing
0👍
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()
- [Django]-PicklingError: Can't pickle <class 'decimal.Decimal'>: it's not the same object as decimal.Decimal
- [Django]-How can I list urlpatterns (endpoints) on Django?
- [Django]-You need to install postgresql-server-dev-X.Y for building a server-side extension or libpq-dev for building a client-side application
- [Django]-List field in model?
- [Django]-Annotate with latest related object in Django
- [Django]-How to set current user to user field in Django Rest Framework?
0👍
Making sure my project’s venv
folder resides inside my project’s root directory solved this problem for me.
- [Django]-Django include template from another app
- [Django]-How to run Django's test database only in memory?
- [Django]-How do I do an OR filter in a Django query?