Home > database >  Apache/mod_wsgi/Django AJAX : 500 Internal Server Error: ModuleNotFoundError: No module named '
Apache/mod_wsgi/Django AJAX : 500 Internal Server Error: ModuleNotFoundError: No module named '

Time:10-09

Our issue is stemming from a Django project finding the corsheaders module running via Apache/WSGI. The code runs fine using the Django's local runserver but throws a 500 Internal Server Error when acccessed throught Apache (v.2.4.41). If we comment out the application and middleware in settings.py, the site and other code works fine (execpt the API functionality that needs corsheaders).

We have exhausted the online resources we can find, so thank you in advance for the advice to uninstall and reinstall django-cors-headers in a variety of ways using pip. We do use several other modules that have been installed via this gateway with no issue. The best we can tell, the issue stems from the django wsgi not seeing the module.

I included the relevant logs and settings below. I also noted the install locations for the corsheader module.

Relevant Versions

  • django-cors-headers==3.13.0 (installed at: /home/geekfest/.local/lib/python3.8/site-packages)
  • django-cors-middleware==1.5.0
  • python==3.8.10
  • Django==4.1.2
  • Ubuntu==20.04.1

pythonpath: ['', '/usr/lib/python38.zip', '/usr/lib/python3.8', '/usr/lib/python3.8/lib-dynload', '/home/geekfest/.local/lib/python3.8/site-packages', '/usr/local/lib/python3.8/dist-packages', '/usr/lib/python3/dist-packages']

**settings.py**

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'corsheaders',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.humanize',
    'stats',
]

MIDDLEWARE = [
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.common.CommonMiddleware',
    'stats.middleware.EventData',
]

ROOT_URLCONF = 'geekstats.urls'

CORS_ORIGIN_ALLOW_ALL = True

error.log

[Fri Oct 07 11:01:49.957188 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61279] mod_wsgi (pid=11860): Failed to exec Python script file '/home/geekfest/geekstats/csgo-geekstats/gee>
[Fri Oct 07 11:01:49.957302 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61279] mod_wsgi (pid=11860): Exception occurred processing WSGI script '/home/geekfest/geekstats/csgo-geeks>
[Fri Oct 07 11:01:49.960516 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61279] Traceback (most recent call last):
[Fri Oct 07 11:01:49.960613 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61279]   File "/home/geekfest/geekstats/csgo-geekstats/geekstats/geekstats/wsgi.py", line 20, in <module>
[Fri Oct 07 11:01:49.960625 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61279]     application = get_wsgi_application()
[Fri Oct 07 11:01:49.960633 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61279]   File "/usr/lib/python3/dist-packages/django/core/wsgi.py", line 12, in get_wsgi_application
[Fri Oct 07 11:01:49.960637 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61279]     django.setup(set_prefix=False)
[Fri Oct 07 11:01:49.960645 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61279]   File "/usr/lib/python3/dist-packages/django/__init__.py", line 24, in setup
[Fri Oct 07 11:01:49.960651 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61279]     apps.populate(settings.INSTALLED_APPS)
[Fri Oct 07 11:01:49.960674 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61279]   File "/usr/lib/python3/dist-packages/django/apps/registry.py", line 91, in populate
[Fri Oct 07 11:01:49.960680 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61279]     app_config = AppConfig.create(entry)
[Fri Oct 07 11:01:49.960690 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61279]   File "/usr/lib/python3/dist-packages/django/apps/config.py", line 90, in create
[Fri Oct 07 11:01:49.960695 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61279]     module = import_module(entry)
[Fri Oct 07 11:01:49.960721 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61279]   File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
[Fri Oct 07 11:01:49.960726 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61279]     return _bootstrap._gcd_import(name[level:], package, level)
[Fri Oct 07 11:01:49.960735 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61279]   File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
[Fri Oct 07 11:01:49.960742 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61279]   File "<frozen importlib._bootstrap>", line 991, in _find_and_load
[Fri Oct 07 11:01:49.960748 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61279]   File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked
[Fri Oct 07 11:01:49.960770 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61279] ModuleNotFoundError: No module named 'corsheaders'
[Fri Oct 07 11:01:50.241210 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61280] mod_wsgi (pid=11860): Failed to exec Python script file '/home/geekfest/geekstats/csgo-geekstats/gee>
[Fri Oct 07 11:01:50.241287 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61280] mod_wsgi (pid=11860): Exception occurred processing WSGI script '/home/geekfest/geekstats/csgo-geeks>
[Fri Oct 07 11:01:50.241442 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61280] Traceback (most recent call last):
[Fri Oct 07 11:01:50.241482 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61280]   File "/home/geekfest/geekstats/csgo-geekstats/geekstats/geekstats/wsgi.py", line 20, in <module>
[Fri Oct 07 11:01:50.241486 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61280]     application = get_wsgi_application()
[Fri Oct 07 11:01:50.241494 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61280]   File "/usr/lib/python3/dist-packages/django/core/wsgi.py", line 12, in get_wsgi_application
[Fri Oct 07 11:01:50.241497 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61280]     django.setup(set_prefix=False)
[Fri Oct 07 11:01:50.241504 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61280]   File "/usr/lib/python3/dist-packages/django/__init__.py", line 24, in setup
[Fri Oct 07 11:01:50.241506 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61280]     apps.populate(settings.INSTALLED_APPS)
[Fri Oct 07 11:01:50.241513 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61280]   File "/usr/lib/python3/dist-packages/django/apps/registry.py", line 83, in populate
[Fri Oct 07 11:01:50.241516 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61280]     raise RuntimeError("populate() isn't reentrant")
[Fri Oct 07 11:01:50.241532 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61280] RuntimeError: populate() isn't reentrant

wsgi.py:

import os,sys

sys.path.append('/home/geekfest/geekstats/.../geekstats')
sys.path.append('/usr/lib/python3/dist-packages')
sys.path.append('/home/geekfest/.local/lib/python3.8/site-packages')

from django.core.wsgi import get_wsgi_application

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

application = get_wsgi_application()

Apache .conf virtual host definition:

<VirtualHost *:80>
    ServerName xxx.com
    ServerAlias stats.xxx.com
    DocumentRoot /home/geekfest/.../geekstats/stats
    ErrorLog ${APACHE_LOG_DIR}/gferror.log
    CustomLog ${APACHE_LOG_DIR}/gfaccess.log combined
    WSGIDaemonProcess xxx.com python-path=/usr/lib/python3.8:/home/geekfest/geekstats/...geekstats/geekstats:/usr/lib/python3/dist-packages:/home/geekfest/.local/lib/python3.8/site-packages
    WSGIProcessGroup xxx.com

# Tell Apache what it should call when the “xxx” url alias is called
    WSGIScriptAlias / /home/geekfest/.../geekstats/geekstats/wsgi.py

# Tell Apache where to find the “static” subdir for the django site
     Alias /static /home/geekfest/.../stats/static

# Give Apache the location and permissions to where the wsgi.py file resides in>
    <Directory /home/geekfest/.../geekstats/geekstats>
        <Files wsgi.py>
            Require all granted
            Options  Indexes  ExecCGI
        </Files>
    </Directory>

# Give Apache the location and permissions to the static directory (images) for>
    <Directory /home/geekfest/.../stats/static>
        Order deny,allow
        Allow from all
        Require all granted
    </Directory>

</VirtualHost>

CodePudding user response:

Make sure you have installed coreheaders. To install it you can try pip install django-cors-headers. If you are using virtualenv for python packages then you have to specify in the apache configuration file where your virtualenv is located.

CodePudding user response:

Ok, so I figured it out. I knew it had to be path challenge because the module WAS there. If you look above, you will pip installed it to /home. WSGI was working from the /user directory. Once I forced Django to tell me where my other working libraries were, I saw the issue and force installed the library to /user.

The command to direct pip to force reinstall another directoy is:

sudo pip install --upgrade --force-reinstall --target /usr/lib/python3/dist-packages/ django-cors-headers
  • Related