Search code examples
djangoapachemod-wsgiwsgi

ModuleNotFoundError: No module named 'projectName' - wsgi Django Deployment


I am trying to run my Django Project using Apache.

When I launch Apache I receive an 'Internal Server Error', inspecting the logs I have found that it fails to execute the wsgi.py for my project and throws the errors below.

The error presented says there is No module named 'localassets', i.e. the projectname.

mod_wsgi (pid=5128): Failed to exec Python script file 'C:/Env/localassets/localassets/wsgi.py'., referer: http://localhost/
mod_wsgi (pid=5128): Exception occurred processing WSGI script 'C:/Env/localassets/localassets/wsgi.py'., referer: http://localhost/
Traceback (most recent call last):\r, referer: http://localhost/
   File "C:/Env/localassets/localassets/wsgi.py", line 16, in <module>\r, referer: http://localhost/
     application = get_wsgi_application()\r, referer: http://localhost/
   File "C:\\Env\\Lib\\site-packages\\django\\core\\wsgi.py", line 12, in get_wsgi_application\r, referer: http://localhost/
     django.setup(set_prefix=False)\r, referer: http://localhost/
   File "C:\\Env\\Lib\\site-packages\\django\\__init__.py", line 19, in setup\r, referer: http://localhost/
    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)\r, referer: http://localhost/
   File "C:\\Env\\Lib\\site-packages\\django\\conf\\__init__.py", line 102, in __getattr__\r, referer: http://localhost/
    self._setup(name)\r, referer: http://localhost/
   File "C:\\Env\\Lib\\site-packages\\django\\conf\\__init__.py", line 89, in _setup\r, referer: http://localhost/
     self._wrapped = Settings(settings_module)\r, referer: http://localhost/
   File "C:\\Env\\Lib\\site-packages\\django\\conf\\__init__.py", line 217, in __init__\r, referer: http://localhost/
     mod = importlib.import_module(self.SETTINGS_MODULE)\r, referer: http://localhost/
   File "C:\\Program Files\\Python310\\Lib\\importlib\\__init__.py", line 126, in import_module\r, referer: http://localhost/
     return _bootstrap._gcd_import(name[level:], package, level)\r, referer: http://localhost/
   File "<frozen importlib._bootstrap>", line 1050, in _gcd_import\r, referer: http://localhost/
   File "<frozen importlib._bootstrap>", line 1027, in _find_and_load\r, referer: http://localhost/
   File "<frozen importlib._bootstrap>", line 992, in _find_and_load_unlocked\r, referer: http://localhost/
   File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed\r, referer: http://localhost/
   File "<frozen importlib._bootstrap>", line 1050, in _gcd_import\r, referer: http://localhost/
   File "<frozen importlib._bootstrap>", line 1027, in _find_and_load\r, referer: http://localhost/
   File "<frozen importlib._bootstrap>", line 1004, in _find_and_load_unlocked\r, referer: http://localhost/
 ModuleNotFoundError: No module named 'localassets'\r, referer: http://localhost/

httpd.conf

LoadFile "C:/Program Files/Python310/python310.dll"
LoadModule wsgi_module "C:/Env/Lib/site-packages/mod_wsgi/server/mod_wsgi.cp310-win_amd64.pyd"
WSGIPythonHome "C:/Program Files/Python310"
WSGIScriptAlias / "C:/Env/localassets/localassets/wsgi.py"
WSGIPythonPath "C:/Env/Lib/site-packages"

<Directory "C:/Env/localassets/localassets/">
    <Files wsgi.py>
        Require all granted
    </Files>
</Directory>

Alias /static "C:/Env/localassets/localassets/static"
<Directory "C:/Env/localassets/localassets/static">
    Require all granted
</Directory>

Inspecting the wsgi.py file I don't see any issues here either.

import os

from django.core.wsgi import get_wsgi_application

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

application = get_wsgi_application()

It seems as though I am missing something in wsgi.py but I am unsure as to what I am missing for this to work correctly?


Solution

  • just add the apps folder to sys.path in wsgi.py:

    import os
    import sys
    
    sys.path.insert(0, 'C:/Env/localassets')
    
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'localassets.settings')
    
    application = get_wsgi_application()
    

    btw. you seem to have the virtual env folder mixed with your app. I would recommend to separate like

    project_folder
       env
       localassets
          localassets
             wsgi.py
    
    etc.