WSGI ERROR :Target WSGI script cannot be loaded as Python module

656 Views Asked by At

I am trying to deploy a Django application using apache and i am getting the following error

[Fri Oct 08 07:55:44.393237 2021] [wsgi:error] [pid 12424:tid 140450959271680]  mod_wsgi (pid=12424): Target WSGI script '/home/preinstall/hx_preinstaller/hx_preinstaller/wsgi.py' cannot be loaded as Python module.
[Fri Oct 08 07:55:44.393281 2021] [wsgi:error] [pid 12424:tid 140450959271680]  mod_wsgi (pid=12424): Exception occurred processing WSGI script '/home/preinstall/hx_preinstaller/hx_preinstaller/wsgi.py'.
[Fri Oct 08 07:55:44.393408 2021] [wsgi:error] [pid 12424:tid 140450959271680]  Traceback (most recent call last):
[Fri Oct 08 07:55:44.393430 2021] [wsgi:error] [pid 12424:tid 140450959271680]  File "/home/preinstall/hx_preinstaller/hx_preinstaller/wsgi.py", line 12, in <module>
[Fri Oct 08 07:55:44.393435 2021] [wsgi:error] [pid 12424:tid 140450959271680]  from django.core.wsgi import get_wsgi_application
[Fri Oct 08 07:55:44.393446 2021] [wsgi:error] [pid 12424:tid 140450959271680]  ModuleNotFoundError: No module named 'django'

My apache virtual host

 <VirtualHost *:80>
   DocumentRoot /home/preinstall/hx_preinstaller
    ErrorLog ${APACHE_LOG_DIR}/preinstall_error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    <Directory /home/preinstall/hx_preinstaller/hx_preinstaller>
        <Files wsgi.py>
            Require all granted
        </Files>
         </Directory>
                 <Directory /home/preinstall/hx_preinstaller>
                 Require all granted
                 </Directory>
                 WSGIDaemonProcess preinstall python-path=/home/preinstall/hx_preinstaller:/home/preinstall/.local/lib/python3.6/site-packages
                WSGIProcessGroup preinstall
                WSGIPassAuthorization On
                WSGIScriptAlias / /home/preinstall/hx_preinstaller/hx_preinstaller/wsgi.py
</VirtualHost>

What should i do?..

2

There are 2 best solutions below

1
On

Maybe its a python version mismatch , or you didnt add correctly the python path to the appache.conf . go check this post to know more about this error ->>> link

0
On

May be below solution can work for you This could happen if your server uses the old python version like 3.6 and you updated to 3.11. These are the below steps I recommend

  1. activate the virtual env conda environment (If you used anaconda to update python).
  2. ran "pip install mod_wsgi" in that environment
  3. adjust /apache2/etc/loadmodule.conf to load the new mod_wsgi from inside myvirtualenv
  4. re-started httpd