[Python] Cannot find settings module in mod_wsgi (Apache2 on Ubuntu with django)

Discussão em 'Python' iniciado por Stack, Julho 16, 2025.

  1. Stack

    Stack Membro Participativo

    I am serving a Python app using Django through an Apache2 server. I have the wsgi.py file in a directory

    home/peter/django-apps/anaaccess/anaaccess/ana_access/wsgi.py


    I have a venv in home/peter/django-apps/anaaccess/anaaccess/myenv into which I have installed mod_wsgi and django, etc. I have put these lines into apache.conf to set up this venv to handle the python:

    LoadModule wsgi_module "/home/peter/django-apps/anaaccess/anaaccess/myenv/lib/python3.12/site-packages/mod_wsgi/server/mod_wsgi-py312.cpython-312-x86_64-linux-gnu.so"
    WSGIPythonHome "/home/peter/django-apps/anaaccess/anaaccess/myenv"


    I call the application in the virtual host section of the Apache2 configuration:

    WSGIScriptAlias /bayeux /home/peter/django-apps/anaaccess/anaaccess/ana_access/wsgi.py
    <Directory /home/peter/django-apps/anaaccess/anaaccess/ana_access>
    <Files wsgi.py>
    Require all granted
    </Files>
    </Directory>


    In the wsgi.py file I call the application as follows:

    import os
    import django
    from django.core.wsgi import get_wsgi_application
    #os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'ana_access.settings')
    os.environ["DJANGO_SETTINGS_MODULE"] = "ana_access.settings"
    application = get_wsgi_application()


    All seems to start fine. Apache finds and loads the mod_wsgi module, finds the wsgi.py file, and finds the os and django modules. But it fails to find the settings file, with this error:

    mod_wsgi (pid=98141): Failed to exec Python script file '/home/peter/django-apps/anaaccess/anaaccess/ana_access/wsgi.py'., referer: http://109.123.108.170/
    mod_wsgi (pid=98141): Exception occurred processing WSGI script '/home/peter/django-apps/anaaccess/anaaccess/ana_access/wsgi.py'., referer: http://109.123.108.170/
    Traceback (most recent call last):, referer: http://109.123.108.170/
    File "/home/peter/django-apps/anaaccess/anaaccess/ana_access/wsgi.py", line 19, in <module>, referer: http://109.123.108.170/
    application = get_wsgi_application(), referer: http://109.123.108.170/
    ^^^^^^^^^^^^^^^^^^^^^^, referer: http://109.123.108.170/
    File "/home/peter/django-apps/anaaccess/anaaccess/myenv/lib/python3.12/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application, referer: http://109.123.108.170/
    django.setup(set_prefix=False), referer: http://109.123.108.170/
    File "/home/peter/django-apps/anaaccess/anaaccess/myenv/lib/python3.12/site-packages/django/__init__.py", line 19, in setup, referer: http://109.123.108.170/
    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING), referer: http://109.123.108.170/
    ^^^^^^^^^^^^^^^^^^^^^^^, referer: http://109.123.108.170/
    File "/home/peter/django-apps/anaaccess/anaaccess/myenv/lib/python3.12/site-packages/django/conf/__init__.py", line 81, in __getattr__, referer: http://109.123.108.170/
    self._setup(name), referer: http://109.123.108.170/
    File "/home/peter/django-apps/anaaccess/anaaccess/myenv/lib/python3.12/site-packages/django/conf/__init__.py", line 68, in _setup, referer: http://109.123.108.170/
    self._wrapped = Settings(settings_module), referer: http://109.123.108.170/
    ^^^^^^^^^^^^^^^^^^^^^^^^^, referer: http://109.123.108.170/
    File "/home/peter/django-apps/anaaccess/anaaccess/myenv/lib/python3.12/site-packages/django/conf/__init__.py", line 166, in __init__, referer: http://109.123.108.170/
    mod = importlib.import_module(self.SETTINGS_MODULE), referer: http://109.123.108.170/
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^, referer: http://109.123.108.170/
    File "/usr/lib/python3.12/importlib/__init__.py", line 90, in import_module, referer: http://109.123.108.170/
    return _bootstrap._gcd_import(name[level:], package, level), referer: http://109.123.108.170/
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^, referer: http://109.123.108.170/
    File "<frozen importlib._bootstrap>", line 1387, in _gcd_import, referer: http://109.123.108.170/
    File "<frozen importlib._bootstrap>", line 1360, in _find_and_load, referer: http://109.123.108.170/
    File "<frozen importlib._bootstrap>", line 1310, in _find_and_load_unlocked, referer: http://109.123.108.170/
    File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed, referer: http://109.123.108.170/
    File "<frozen importlib._bootstrap>", line 1387, in _gcd_import, referer: http://109.123.108.170/
    File "<frozen importlib._bootstrap>", line 1360, in _find_and_load, referer: http://109.123.108.170/
    File "<frozen importlib._bootstrap>", line 1324, in _find_and_load_unlocked, referer: http://109.123.108.170/
    ModuleNotFoundError: No module named 'ana_access', referer: http://109.123.108.170/


    I have been using mod_wsgi/Django/Apache2 for over twenty years now, through lots of upgrades, server moves, etc. These problems appeared in the latest move, to Apache/2.4.58 on Ubuntu 24.04.2 LTS (GNU/Linux 6.8.0-63-generic x86_64).

    Help gratefully received.

    Continue reading...

Compartilhe esta Página