52👍
I know that this is an old thread but I’ve just bumped into the same issue and I don’t think that this is caused by a missing package. As the Django core distribution contains the correct wsgi handler already.
The problem here is that when wsgi.py is executed it’s missing the packages of the site-packages from your virtualenv. (If you have activated your virtualenv, and done pip install django then everything is fine. You have the necessary django packages).
As far as I’m concerned, I fixed the issue modifying the sys.path in my Path/to/Project/Project/wsgi.py file.
You have to append your project dir and your virtualenv site-packages to the sys.path List.
Here is my wsgi.py file contained in my project (Talking about the wsgi.py created with django-admin.py start-project)… that I had to modify in order to make it work with Apache
# =====================
# wsgi.py file begin
import os, sys
# add the hellodjango project path into the sys.path
sys.path.append('<PATH_TO_MY_DJANGO_PROJECT>/hellodjango')
# add the virtualenv site-packages path to the sys.path
sys.path.append('<PATH_TO_VIRTUALENV>/Lib/site-packages')
# poiting to the project settings
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hellodjango.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
# wsgi.py file end
# ===================
Make sure:
-
you added mod_wsgi to the Apache modules dir
mod_wsgi must be compiled for the OS, Apache and Python version you have -
added the load module command into your httpd.conf to load mod_wsgi module
LoadModule wsgi_module modules/mod_wsgi.so -
configured Django specifics in your httpd.conf or any conf you include in your httpd.conf
Based on the documentation How to use Django with Apache and mod_wsgi
WSGIScriptAlias / <PATH_TO_PROJECT>/hellodjango/hellodjango/wsgi.py
WSGIPythonPath <PATH_TO_PROJECT>:<PATH_TO_VIRTUALENV>/Lib/site-packages
<Directory <PATH_TO_PROJECT>/hellodjango/hellodjango>
<Files wsgi.py>
Order deny,allow
Require all granted
</Files>
</Directory>
Hope this helps. It worked for me.
30👍
I had the same issue. My libapache2-mod-wsgi was python 2.x and not python 3.x
found the solution here: https://stackoverflow.com/a/28118284/2489042
credits to @nima
$ 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.
- [Django]-How do I use the built in password reset/change views with my own templates
- [Django]-Difference between User.objects.create_user() vs User.objects.create() vs User().save() in django
- [Django]-How can I list urlpatterns (endpoints) on Django?
7👍
Add this to the Apache configuration file:
WSGIPythonHome /home/ec2-user/.virtualenvs/mysite-main
- [Django]-Get Timezone from City in Python/Django
- [Django]-Django – Login with Email
- [Django]-Separation of business logic and data access in django
4👍
For me, this indicated Django wasn’t installed on the sever. Fixed via
pip install Django
In general, make sure you installed requirements:
pip install -r requirements.txt
If using a virtual environment, activate it before installing requirements.
- [Django]-Django Queryset with year(date) = '2010'
- [Django]-How to dynamically compose an OR query filter in Django?
- [Django]-Django Reverse with arguments '()' and keyword arguments '{}' not found
4👍
I had a similar error just now. It turns out that our Django code was developed on python 3.5, but for some reasons the people who deployed our server setup virtualEnv with python 2.7. We redeployed with python 3.5 and everything worked for us
Below was the error message I received:
$ python serviceStartup.py
Traceback (most recent call last):
File "serviceStartup.py", line 10, in <module>
from django.core.wsgi import get_wsgi_application
ImportError: No module named django.core.wsgi
Hope this will help with anyone seeing a similar error message!
- [Django]-How to get the username of the logged-in user in Django?
- [Django]-Pip install PIL fails
- [Django]-How to access the user profile in a Django template?
1👍
You’ve configured everything very well my friend, just need to give the apache user permission to access both project and virtualenv dirs.
Example:
sudo chown -R www-data:www-data /home/ubuntu/projects
sudo chown -R www-data:www-data /home/ubuntu/virtualenv
This solved my problem with ImportError: No module named django.core.wsgi
(virtualenvs folders) and ImportError: No module named <project-name>.settings
(projects folders)
- [Django]-How do I go straight to template, in Django's urls.py?
- [Django]-Class Based Views VS Function Based Views
- [Django]-Django optional URL parameters
0👍
At first glance, I am sorry for my English. I also faced this issue, and I have solved it by changing ‘wsgi.py’ file to:
import os import django from django.core.handlers.wsgi import WSGIHandler os.environ.setdefault("DJANGO_SETTINGS_MODULE", "eisentask.settings.production") django.setup(set_prefix=False) application = WSGIHandler()
- [Django]-Sending images using Http Post
- [Django]-How to use "get_or_create()" in Django?
- [Django]-Django-Forms with json fields
0👍
I am using Centos and the nginx and gunicorn for my project…
I had this problem…
wsgi file is not reachable maybe it’s bcz of the your current directory
I changed my current directory and It worked…
my project name is coda so I typed
cd coda
and then
gunicorn --bind 0.0.0.0:8001 coda.wsgi
- [Django]-How to perform OR condition in django queryset?
- [Django]-Location of Django logs and errors
- [Django]-Docker/Kubernetes + Gunicorn/Celery – Multiple Workers vs Replicas?
0👍
In case someone came here using AWS Lightsail bitnami..
The problem is that ‘python install packages in a directory that apache do not see’.
so: packages get installed in ‘/home/bitnami/.local/lib/python3.8/site-packages/’ and Apache looks in ‘/opt/bitnami/python/lib/python3.8/site-packages/’.
The temporary solution I followed is copying packages to Apache eyes folder with this command ‘cp -r /home/bitnami/.local/lib/python3.8/site-packages/* /opt/bitnami/python/lib/python3.8/site-packages/’
- [Django]-How can I handle Exceptions raised by dango-social-auth?
- [Django]-Images from ImageField in Django don't load in template
- [Django]-Object does not support item assignment error
0👍
Hi I got the same issue in Ubuntu 20.04 server. I fixed it by installing apache2-dev
sudo apt-get install apache2-dev
In mod_wsgi documentation its important to have both apache2
and apache2-dev
installed.
- [Django]-Find object in list that has attribute equal to some value (that meets any condition)
- [Django]-Check for presence in a list django template
- [Django]-How to squash recent Django migrations?
0👍
Run these commands:
$ sudo apt-get remove libapache2-mod-python libapache2-mod-wsgi
$ sudo apt-get install libapache2-mod-wsgi-py3
- [Django]-Django Queryset with year(date) = '2010'
- [Django]-Cannot import name _uuid_generate_random in heroku django
- [Django]-Django DRF with oAuth2 using DOT (django-oauth-toolkit)
-2👍
For me it was some variables that needed to be setted (for windows) :
set PYTHONHOME=F:\path\to\python
set PYTHONPATH=F:\path\to\python
- [Django]-Django Queryset with filtering on reverse foreign key
- [Django]-Django filter on the basis of text length
- [Django]-GeoDjango GEOSException error