menu

Questions & Answers

WSGI django ModuleNotFoundError: No module named 'django'

I have been trying forever to get my django api to deploy via apache. I have installed mod_wsgi for python 3.7 and my venv is using python 3.7.15. Trying to go to my django app url I am getting a 500 error.

Error log shows:

[Tue Dec 20 21:31:30.690951 2022] [:error] [pid 19216] /usr
[Tue Dec 20 21:31:30.691287 2022] [:error] [pid 19216]  mod_wsgi (pid=19216): Target WSGI script '.../project/project/wsgi.py' cannot be loaded as Python module.
[Tue Dec 20 21:31:30.691323 2022] [:error] [pid 19216]  mod_wsgi (pid=19216): Exception occurred processing WSGI script '.../project/project/wsgi.py'.
[Tue Dec 20 21:31:30.691393 2022] [:error] [pid 19216]  Traceback (most recent call last):
[Tue Dec 20 21:31:30.691423 2022] [:error] [pid 19216]    File ".../project/project/wsgi.py", line 19, in <module>
[Tue Dec 20 21:31:30.691428 2022] [:error] [pid 19216]      from django.core.wsgi import get_wsgi_application
[Tue Dec 20 21:31:30.691444 2022] [:error] [pid 19216]  ModuleNotFoundError: No module named 'django'
[Tue Dec 20 21:31:51.190670 2022] [:error] [pid 19217] 3.7.15 (default, Oct 31 2022, 22:44:31)
[Tue Dec 20 21:31:51.190707 2022] [:error] [pid 19217] [GCC 7.3.1 20180712 (Red Hat 7.3.1-15)]

apache conf file:

<VirtualHost *:80>

    ServerName api.project.com

    DocumentRoot path/to/project/root

    WSGIScriptAlias / /path/to/wsgi.py
    WSGIDaemonProcess project-name processes=4 threads=1 display-name=%{GROUP} python-path=path/to/lib/python3.7/site-packages:/path/to/project/root
    WSGIProcessGroup project-group

    <Directory "/path/to/project/root">
        Require all granted
    </Directory>

    #SSL stuff...

</VirtualHost>

wsgi.py:

import os
import sys

from django.core.wsgi import get_wsgi_application

path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
if path not in sys.path:
    sys.path.append(path)

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

application = get_wsgi_application()

I originally had the wrong mod_wsgi installed (built for python2). I removed that and installed python3-mod_wsgi.x86_64 and changed module path in httpd.conf. I then realized that my mod_wsgi was for python version 3.7, but my application venv was running python 3.6.8. I then wiped the venv and created a new one with python 3.7.15. Changed the conf WSGIDAEMONPROCESS path to the correct 3.7 site-packages. Still same error.

Answers(1) :

Just installing a virtual environment is not enough, then you'll have to activate it.

Run pip install -r requirements.txt to install the required packages for the project.

The Error

The first error line states Target WSGI script '.../project/project/wsgi.py' cannot be loaded as Python module.

This means your environment variable DJANGO_SETTINGS_MODULE is not set properly. In order to do it, go to .bashrc and set it to the same value as in the asgi.py/wsgi.py file.

Run source .bashrc to reload the configuration file, and restart the bash terminal (or open a new one) where you're trying to run the application.

This should work.