Wsgi: ModuleNotFoundError: No module named 'django' error
I am trying to host my Django app on my Ubuntu server and when trying to access my website, I get this error from the Apache log:
ModuleNotFoundError: No module named 'django'
I am using a venv for my Django app with python version 3.8 (I have also compiled and installed mod_wsgi in my venv). Running pip freeze I see that I do have Django installed in my venv:
APScheduler==3.8.1
asgiref==3.5.0
backports.zoneinfo==0.2.1
certifi==2021.10.8
charset-normalizer==2.0.10
colorama==0.4.4
commonmark==0.9.1
deepdiff==5.7.0
Django==4.0.1
django-cors-headers==3.11.0
djangorestframework==3.13.1
idna==3.3
lxml==4.7.1
mod-wsgi==4.9.1.dev1
ordered-set==4.0.2
prettytable==3.0.0
psycopg2-binary==2.9.3
Pygments==2.11.2
pytz==2021.3
pytz-deprecation-shim==0.1.0.post0
requests==2.27.1
rich==11.1.0
six==1.16.0
soupsieve==2.3.1
sqlparse==0.4.2
tzdata==2021.5
tzlocal==4.1
urllib3==1.26.8
wcwidth==0.2.5
whitenoise==5.3.0
And just in case it might solve it, I installed Django globally but still got the error in Apache. I have been trying to follow some of the common solutions but can't seem to get it to work. Is there anything I am missing or any setting that may be off?
I do notice my Apache says it is configured with 3.6, could this be the cause? is there a way to make it use 3.8 which is my python3 default?
My wsgi for my Django project (backend/core/wsgi.py):
import os, sys
sys.path.append('/home/brickmane/djangoapp/pricewatcher/backend/')
sys.path.append('/home/brickmane/djangoapp/pricewatcher/backend/core/')
sys.path.append('/home/brickmane/djangoapp/pricewatcher/venv/lib/python3.8/site-packages')
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings')
application = get_wsgi_application()
My virtualhost settings in /etc/apache2/apache2.conf:
WSGIPythonHome /home/brickmane/djangoapp/pricewatcher/venv
WSGIPythonPath /home/brickmane/djangoapp/pricewatcher/backend
<VirtualHost *:80>
ServerAlias www.d8pricecheck.tk
WSGIProcessGroup backend
Alias /static/ /home/brickmane/djangoapp/pricewatcher/backend/static/
<Directory /home/brickmane/djangoapp/pricewatcher/backend/static>
Require all granted
</Directory>
WSGIScriptAlias / /home/brickmane/djangoapp/pricewatcher/backend/core/wsgi.py
<Directory /home/brickmane/djangoapp/pricewatcher/backend/core>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess backend python-path=/home/brickmane/djangoapp/pricewatcher/backend/core python-home=/home/brickmane/djangoapp/pricewatcher/venv
WSGIProcessGroup backend
</VirtualHost>
Full Apache error log from restarting and visiting website:
[Tue Feb 01 16:24:09.488925 2022] [mpm_prefork:notice] [pid 30163] AH00169: caught SIGTERM, shutting down
[Tue Feb 01 16:24:09.618015 2022] [ssl:warn] [pid 30456] AH01916: Init: (brickmaneserver.hsd1.ga.comcast.net:443) You configured HTTP(80) on the standard HTTPS(443) port!
[Tue Feb 01 16:24:09.696360 2022] [ssl:warn] [pid 30459] AH01916: Init: (brickmaneserver.hsd1.ga.comcast.net:443) You configured HTTP(80) on the standard HTTPS(443) port!
[Tue Feb 01 16:24:09.702058 2022] [mpm_prefork:notice] [pid 30459] AH00163: Apache/2.4.29 (Ubuntu) OpenSSL/1.1.1g mod_wsgi/4.5.17 Python/3.6 configured -- resuming normal operations
[Tue Feb 01 16:24:09.702100 2022] [core:notice] [pid 30459] AH00094: Command line: '/usr/sbin/apache2'
[Tue Feb 01 16:24:11.607243 2022] [wsgi:error] [pid 30490] [remote 27.147.213.242:33593] mod_wsgi (pid=30490): Target WSGI script '/home/brickmane/djangoapp/myserver/backend/core/wsgi.py' cannot be loaded as Python module.
[Tue Feb 01 16:24:11.607289 2022] [wsgi:error] [pid 30490] [remote 27.147.213.242:33593] mod_wsgi (pid=30490): Exception occurred processing WSGI script '/home/brickmane/djangoapp/myserver/backend/core/wsgi.py'.
[Tue Feb 01 16:24:11.607468 2022] [wsgi:error] [pid 30490] [remote 27.147.213.242:33593] Traceback (most recent call last):
[Tue Feb 01 16:24:11.607495 2022] [wsgi:error] [pid 30490] [remote 27.147.213.242:33593] File "/home/brickmane/djangoapp/myserver/backend/core/wsgi.py", line 21, in <module>
[Tue Feb 01 16:24:11.607502 2022] [wsgi:error] [pid 30490] [remote 27.147.213.242:33593] from django.core.wsgi import get_wsgi_application
[Tue Feb 01 16:24:11.607520 2022] [wsgi:error] [pid 30490] [remote 27.147.213.242:33593] ModuleNotFoundError: No module named 'django'
[Tue Feb 01 16:24:14.525726 2022] [wsgi:error] [pid 30490] [remote 108.162.238.29:17852] mod_wsgi (pid=30490): Target WSGI script '/home/brickmane/djangoapp/myserver/backend/core/wsgi.py' cannot be loaded as Python module.
[Tue Feb 01 16:24:14.525891 2022] [wsgi:error] [pid 30490] [remote 108.162.238.29:17852] mod_wsgi (pid=30490): Exception occurred processing WSGI script '/home/brickmane/djangoapp/myserver/backend/core/wsgi.py'.
[Tue Feb 01 16:24:14.526144 2022] [wsgi:error] [pid 30490] [remote 108.162.238.29:17852] Traceback (most recent call last):
[Tue Feb 01 16:24:14.526242 2022] [wsgi:error] [pid 30490] [remote 108.162.238.29:17852] File "/home/brickmane/djangoapp/myserver/backend/core/wsgi.py", line 21, in <module>
[Tue Feb 01 16:24:14.526266 2022] [wsgi:error] [pid 30490] [remote 108.162.238.29:17852] from django.core.wsgi import get_wsgi_application
[Tue Feb 01 16:24:14.526327 2022] [wsgi:error] [pid 30490] [remote 108.162.238.29:17852] ModuleNotFoundError: No module named 'django'
[Tue Feb 01 16:24:14.810004 2022] [wsgi:error] [pid 30490] [remote 162.158.187.159:17484] mod_wsgi (pid=30490): Target WSGI script '/home/brickmane/djangoapp/myserver/backend/core/wsgi.py' cannot be loaded as Python module.
[Tue Feb 01 16:24:14.810169 2022] [wsgi:error] [pid 30490] [remote 162.158.187.159:17484] mod_wsgi (pid=30490): Exception occurred processing WSGI script '/home/brickmane/djangoapp/myserver/backend/core/wsgi.py'.
[Tue Feb 01 16:24:14.810397 2022] [wsgi:error] [pid 30490] [remote 162.158.187.159:17484] Traceback (most recent call last):
[Tue Feb 01 16:24:14.810484 2022] [wsgi:error] [pid 30490] [remote 162.158.187.159:17484] File "/home/brickmane/djangoapp/myserver/backend/core/wsgi.py", line 21, in <module>
[Tue Feb 01 16:24:14.810506 2022] [wsgi:error] [pid 30490] [remote 162.158.187.159:17484] from django.core.wsgi import get_wsgi_application
[Tue Feb 01 16:24:14.810566 2022] [wsgi:error] [pid 30490] [remote 162.158.187.159:17484] ModuleNotFoundError: No module named 'django'
You have to install in venv Python version at least 3.8, because 3.7 and lower are not compatible with Django 4.0+. You should delete venv completely and reinitialize with installed Python with newer (newest is 3.10.2) version.
If Ubuntu has at least 3.8, then it should be fine. If not, try to install/upgrade it. Here you have some hints how to do it: https://cloudbytes.dev/snippets/upgrade-python-to-latest-version-on-ubuntu-linux
This question is bit old, but if someone still faces this. I have solved this by setting the python-path to my python's site-packages. Also for your case above
WSGIDaemonProcess backend python-path=/home/brickmane/djangoapp/pricewatcher/venv/lib/python
3.8/site-packages python-home=/home/brickmane/djangoapp/pricewatcher/venv
This solved my problem
For me, I determined that Apache2 mod_wsgi was compiled against python 3.9 and my django instance was using python 3.10.
ldd /usr/lib/apache2/modules/mod_wsgi.so
linux-vdso.so.1 (0x00007ffe32535000)
libpython3.9.so.1.0 => /usr/lib/x86_64-linux-gnu/libpython3.9.so.1.0 (0x00007f9c59b77000)
. . .
Making sure my system Python was also set to 3.9 fixed this error.