Django mod_wsgi Apache Server, ModuleNotFoundError: No Module Named Django

Question:

I read ton of articles, but still can’t figure out what I’m missing. I’m running a django website from virtualenv. Here’s my config file. The website address is replaced by <domain.com>, can’t use that here.

Config

<VirtualHost *:80>
    
        ServerAdmin sidharth@collaboration-management
        ServerName <domain.com>
        ServerAlias <domain.com>
        DocumentRoot /home/sidharth/Downloads/gmcweb

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        Alias /static /home/sidharth/Downloads/gmcweb/static

        <Directory /home/sidharth/Downloads/gmcweb/static>
                Require all granted
        </Directory>

        <Directory /home/sidharth/Downloads/gmcweb/gmcweb>
                <Files wsgi.py>
                        Require all granted
                </Files>
        </Directory>


        WSGIDaemonProcess gmcweb python-home=/home/sidharth/Downloads/gmcwebenvlin python-path=/home/sidharth/Downloads/gmcweb
        WSGIProcessGroup gmcweb
        WSGIScriptAlias / /home/sidharth/Downloads/gmcweb/gmcweb/wsgi.py
</VirtualHost>

Here’s my WSGI.py file, didn’t change anything never had to earlier

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'gmcweb.settings')

application = get_wsgi_application()

Python Versions

My virtualenv python version is 3.9.5
Default Google VM python version is 3.6.9

Python Installed Libraries

Package                  Version
------------------------ ---------
asgiref                  3.4.0
attrs                    21.2.0
autopep8                 1.5.7
beautifulsoup4           4.9.3
certifi                  2021.5.30
cffi                     1.14.5
chardet                  4.0.0
cryptography             3.4.7
defusedxml               0.7.1
Django                   3.2.4
django-allauth           0.44.0
django-livereload-server 0.3.2
idna                     2.10
jsonschema               3.2.0
oauthlib                 3.1.1
pip                      21.2.3
pycodestyle              2.7.0
pycparser                2.20
PyJWT                    2.1.0
pyrsistent               0.18.0
python3-openid           3.2.0
pytz                     2021.1
requests                 2.25.1
requests-oauthlib        1.3.0
setuptools               57.4.0
six                      1.16.0
soupsieve                2.2.1
sqlparse                 0.4.1
toml                     0.10.2
tornado                  6.1
urllib3                  1.26.6

I installed apache modwsgi as well sudo apt-get install python3-pip apache2 libapache2-mod-wsgi-py3

Error Log File

[Thu Sep 23 15:05:06.554545 2021] [mpm_event:notice] [pid 32077:tid 140392561593280] AH00489: Apache/2.4.29 (Ubuntu) mod_wsgi/4.5.17 Python/3.6 configured -- resuming normal operations
[Thu Sep 23 15:05:06.554594 2021] [core:notice] [pid 32077:tid 140392561593280] AH00094: Command line: '/usr/sbin/apache2'
[Thu Sep 23 15:05:19.081581 2021] [wsgi:error] [pid 32617:tid 140392409851648] [remote 103.206.177.13:49604] mod_wsgi (pid=32617): Target WSGI script '/home/sidharth/Downloads/gmcweb/gmcweb/wsgi.py' c$
[Thu Sep 23 15:05:19.081638 2021] [wsgi:error] [pid 32617:tid 140392409851648] [remote 103.206.177.13:49604] mod_wsgi (pid=32617): Exception occurred processing WSGI script '/home/sidharth/Downloads/g$
[Thu Sep 23 15:05:19.081828 2021] [wsgi:error] [pid 32617:tid 140392409851648] [remote 103.206.177.13:49604] Traceback (most recent call last):
[Thu Sep 23 15:05:19.081849 2021] [wsgi:error] [pid 32617:tid 140392409851648] [remote 103.206.177.13:49604]   File "/home/sidharth/Downloads/gmcweb/gmcweb/wsgi.py", line 12, in <module>
[Thu Sep 23 15:05:19.081853 2021] [wsgi:error] [pid 32617:tid 140392409851648] [remote 103.206.177.13:49604]     from django.core.wsgi import get_wsgi_application
[Thu Sep 23 15:05:19.081867 2021] [wsgi:error] [pid 32617:tid 140392409851648] [remote 103.206.177.13:49604] ModuleNotFoundError: No module named 'django'
[Thu Sep 23 15:05:32.244779 2021] [wsgi:error] [pid 32617:tid 140392325842688] [remote 103.206.177.13:52916] mod_wsgi (pid=32617): Target WSGI script '/home/sidharth/Downloads/gmcweb/gmcweb/wsgi.py' c$
[Thu Sep 23 15:05:32.244845 2021] [wsgi:error] [pid 32617:tid 140392325842688] [remote 103.206.177.13:52916] mod_wsgi (pid=32617): Exception occurred processing WSGI script '/home/sidharth/Downloads/g$
[Thu Sep 23 15:05:32.244924 2021] [wsgi:error] [pid 32617:tid 140392325842688] [remote 103.206.177.13:52916] Traceback (most recent call last):
[Thu Sep 23 15:05:32.244946 2021] [wsgi:error] [pid 32617:tid 140392325842688] [remote 103.206.177.13:52916]   File "/home/sidharth/Downloads/gmcweb/gmcweb/wsgi.py", line 12, in <module>
[Thu Sep 23 15:05:32.244951 2021] [wsgi:error] [pid 32617:tid 140392325842688] [remote 103.206.177.13:52916]     from django.core.wsgi import get_wsgi_application
[Thu Sep 23 15:05:32.244966 2021] [wsgi:error] [pid 32617:tid 140392325842688] [remote 103.206.177.13:52916] ModuleNotFoundError: No module named 'django'
Asked By: Sid Kaushik

||

Answers:

The error says that either you haven’t got Django installed or didn’t activate the virtual environment in which the Django was installed. Make sure that you check the list of installed packages and find Django in there, via:

$pip list
Answered By: woxa999

If you get any problem like this and got multiple Python versions then that’s the issue. I spawned a new VM with latest version of Python and only one version. It worked like a charm.

When we install Mod_WSGI it will be installed on the environment path by default.

Answered By: Sid Kaushik

libapache2-mod-wsgi-py3 can be used only with python 3.6.9 in ubuntu 18!!
if your virtualenv uses another version of python, you should download mod_wsgi from this link: mod_wsgi

at this time the latest version of mod_wsgi is 4.9.0. follow the steps below:

$ sudo apt install apache2 apache2-dev
$ wget https://codeload.github.com/GrahamDumpleton/mod_wsgi/tar.gz/4.9.0
$ tar -xf 4.9.0
$ cd mod_wsgi-4.9.0/
$ ./configure --with-python=/your virtualenv path/bin/python[version]
   # example:  ./configure --with-python=/virtualenv/bin/python3.9
$ make
$ sudo make install

$ sudo systemctl restart apache2
Answered By: Masoud Gheisari

After an exhaustive search and hands-on on this issue, I resolved the issue also faced by me and I figure it out that

If your mod_wsgi is installed using python i.e. 3.6 than your virtual environment must be created using the same python version i.e. 3.6.

The issue is also described on the mod_wsgi documentation too where it clearly says that it only works with exactly one python version only at a time. Thus, the python version used in installing or compilation of mod_wsgi must be the same as your python virtual environment.

Rest of the configuration is already provided in django documentation
https://docs.djangoproject.com/en/4.2/howto/deployment/wsgi/modwsgi/

Answered By: Developer Mayank
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.