简体   繁体   中英

Target WSGI script cannot be loaded as a python module

I have been trying to get my django app deployed for a while now and no matter what I have followed and tried I keep running into errors.

I am currently getting this error:

Target WSGI script '/home/brickmane/djangoapp/myserver/backend/core/wsgi.py' cannot be loaded as Python module.

and

ModuleNotFoundError: No module named 'contextvars'

which is not something I used myself in my project.

I have tried some solutions like using:

sudo chmod a+x wsgi.py

and trying libapache2-mod-wsgi-py3 vs libapache2-mod-wsgi

my apache2.conf file where I placed my virtual host and the settings for my app:

WSGIPythonHome /home/brickmane/djangoapp/myserver/venv
WSGIPythonPath /home/brickmane/djangoapp/myserver/backend
<VirtualHost *:80>
  ServerAlias www.d8pricecheck.tk
  WSGIProcessGroup backend
  Alias /static/ /home/brickmane/djangoapp/myserver/backend/static/
  <Directory /home/brickmane/djangoapp/myserver/backend/static>
    Require all granted
  </Directory>
  WSGIScriptAlias / /home/brickmane/djangoapp/myserver/backend/core/wsgi.py
  <Directory /home/brickmane/djangoapp/myserver/backend/core>
    <Files wsgi.py>
      Require all granted
    </Files>
  </Directory>
 WSGIDaemonProcess backend python-path=/home/brickmane/djangoapp/myserver/backend/core python-home=/home/brickmane/djangoapp/myserver/venv
 WSGIProcessGroup backend
</VirtualHost>

my wsgi.py file:

import os, sys
# add the hellodjango project path into the sys.path
sys.path.append('/home/brickmane/djangoapp/myserver/backend/')

sys.path.append('/home/brickmane/djangoapp/myserver/backend/core/')
# add the virtualenv site-packages path to the sys.path
sys.path.append('/home/brickmane/djangoapp/myserver/venv/lib/python3.10/site-packages')

# poiting to the project settings
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "core.settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

This is my full apache error log from restarting the service and trying to access my website:

[Mon Jan 31 12:28:54.228176 2022] [mpm_prefork:notice] [pid 19551] AH00173: SIGHUP received.  Attempting to restart
[Mon Jan 31 12:28:54.292016 2022] [ssl:warn] [pid 19551] AH01916: Init: (brickmaneserver.comcast.net:443) You configured HTTP(80) on the standard HTTPS(443) port!
[Mon Jan 31 12:28:54.296542 2022] [mpm_prefork:notice] [pid 19551] AH00163: Apache/2.4.29 (Ubuntu) OpenSSL/1.1.1g mod_wsgi/4.5.17 Python/3.6 configured -- resuming normal operations
[Mon Jan 31 12:28:54.296588 2022] [core:notice] [pid 19551] AH00094: Command line: '/usr/sbin/apache2'
[Mon Jan 31 12:29:00.082063 2022] [wsgi:error] [pid 19618] [remote 108.162.238.175:59436] mod_wsgi (pid=19618): Target WSGI script '/home/brickmane/djangoapp/myserver/backend/core/wsgi.py' cannot be loaded as Python module.
[Mon Jan 31 12:29:00.082120 2022] [wsgi:error] [pid 19618] [remote 108.162.238.175:59436] mod_wsgi (pid=19618): Exception occurred processing WSGI script '/home/brickmane/djangoapp/myserver/backend/core/wsgi.py'.
[Mon Jan 31 12:29:00.082830 2022] [wsgi:error] [pid 19618] [remote 108.162.238.175:59436] Traceback (most recent call last):
[Mon Jan 31 12:29:00.082874 2022] [wsgi:error] [pid 19618] [remote 108.162.238.175:59436]   File "/home/brickmane/djangoapp/myserver/backend/core/wsgi.py", line 12, in <module>
[Mon Jan 31 12:29:00.082880 2022] [wsgi:error] [pid 19618] [remote 108.162.238.175:59436]     from django.core.wsgi import get_wsgi_application
[Mon Jan 31 12:29:00.082890 2022] [wsgi:error] [pid 19618] [remote 108.162.238.175:59436]   File "/home/brickmane/djangoapp/myserver/venv/lib/python3.10/site-packages/django/core/wsgi.py", line 2, in <module>
[Mon Jan 31 12:29:00.082895 2022] [wsgi:error] [pid 19618] [remote 108.162.238.175:59436]     from django.core.handlers.wsgi import WSGIHandler
[Mon Jan 31 12:29:00.082920 2022] [wsgi:error] [pid 19618] [remote 108.162.238.175:59436]   File "/home/brickmane/djangoapp/myserver/venv/lib/python3.10/site-packages/django/core/handlers/wsgi.py", line 3, in <module>
[Mon Jan 31 12:29:00.082925 2022] [wsgi:error] [pid 19618] [remote 108.162.238.175:59436]     from django.conf import settings
[Mon Jan 31 12:29:00.082933 2022] [wsgi:error] [pid 19618] [remote 108.162.238.175:59436]   File "/home/brickmane/djangoapp/myserver/venv/lib/python3.10/site-packages/django/conf/__init__.py", line 19, in <module>
[Mon Jan 31 12:29:00.082938 2022] [wsgi:error] [pid 19618] [remote 108.162.238.175:59436]     from django.utils.deprecation import RemovedInDjango50Warning
[Mon Jan 31 12:29:00.082946 2022] [wsgi:error] [pid 19618] [remote 108.162.238.175:59436]   File "/home/brickmane/djangoapp/myserver/venv/lib/python3.10/site-packages/django/utils/deprecation.py", line 5, in <module>
[Mon Jan 31 12:29:00.082951 2022] [wsgi:error] [pid 19618] [remote 108.162.238.175:59436]     from asgiref.sync import sync_to_async
[Mon Jan 31 12:29:00.082959 2022] [wsgi:error] [pid 19618] [remote 108.162.238.175:59436]   File "/home/brickmane/djangoapp/myserver/venv/lib/python3.10/site-packages/asgiref/sync.py", line 2, in <module>
[Mon Jan 31 12:29:00.082963 2022] [wsgi:error] [pid 19618] [remote 108.162.238.175:59436]     import contextvars
[Mon Jan 31 12:29:00.082981 2022] [wsgi:error] [pid 19618] [remote 108.162.238.175:59436] ModuleNotFoundError: No module named 'contextvars'
[Mon Jan 31 12:29:00.343820 2022] [wsgi:error] [pid 19618] [remote 162.158.187.159:38614] mod_wsgi (pid=19618): Target WSGI script '/home/brickmane/djangoapp/myserver/backend/core/wsgi.py' cannot be loaded as Python module.
[Mon Jan 31 12:29:00.343880 2022] [wsgi:error] [pid 19618] [remote 162.158.187.159:38614] mod_wsgi (pid=19618): Exception occurred processing WSGI script '/home/brickmane/djangoapp/myserver/backend/core/wsgi.py'.
[Mon Jan 31 12:29:00.344018 2022] [wsgi:error] [pid 19618] [remote 162.158.187.159:38614] Traceback (most recent call last):
[Mon Jan 31 12:29:00.344063 2022] [wsgi:error] [pid 19618] [remote 162.158.187.159:38614]   File "/home/brickmane/djangoapp/myserver/backend/core/wsgi.py", line 12, in <module>
[Mon Jan 31 12:29:00.344069 2022] [wsgi:error] [pid 19618] [remote 162.158.187.159:38614]     from django.core.wsgi import get_wsgi_application
[Mon Jan 31 12:29:00.344079 2022] [wsgi:error] [pid 19618] [remote 162.158.187.159:38614]   File "/home/brickmane/djangoapp/myserver/venv/lib/python3.10/site-packages/django/core/wsgi.py", line 2, in <module>
[Mon Jan 31 12:29:00.344084 2022] [wsgi:error] [pid 19618] [remote 162.158.187.159:38614]     from django.core.handlers.wsgi import WSGIHandler
[Mon Jan 31 12:29:00.344092 2022] [wsgi:error] [pid 19618] [remote 162.158.187.159:38614]   File "/home/brickmane/djangoapp/myserver/venv/lib/python3.10/site-packages/django/core/handlers/wsgi.py", line 3, in <module>
[Mon Jan 31 12:29:00.344097 2022] [wsgi:error] [pid 19618] [remote 162.158.187.159:38614]     from django.conf import settings
[Mon Jan 31 12:29:00.344105 2022] [wsgi:error] [pid 19618] [remote 162.158.187.159:38614]   File "/home/brickmane/djangoapp/myserver/venv/lib/python3.10/site-packages/django/conf/__init__.py", line 19, in <module>
[Mon Jan 31 12:29:00.344109 2022] [wsgi:error] [pid 19618] [remote 162.158.187.159:38614]     from django.utils.deprecation import RemovedInDjango50Warning
[Mon Jan 31 12:29:00.344117 2022] [wsgi:error] [pid 19618] [remote 162.158.187.159:38614]   File "/home/brickmane/djangoapp/myserver/venv/lib/python3.10/site-packages/django/utils/deprecation.py", line 5, in <module>
[Mon Jan 31 12:29:00.344122 2022] [wsgi:error] [pid 19618] [remote 162.158.187.159:38614]     from asgiref.sync import sync_to_async
[Mon Jan 31 12:29:00.344130 2022] [wsgi:error] [pid 19618] [remote 162.158.187.159:38614]   File "/home/brickmane/djangoapp/myserver/venv/lib/python3.10/site-packages/asgiref/sync.py", line 2, in <module>
[Mon Jan 31 12:29:00.344135 2022] [wsgi:error] [pid 19618] [remote 162.158.187.159:38614]     import contextvars
[Mon Jan 31 12:29:00.344164 2022] [wsgi:error] [pid 19618] [remote 162.158.187.159:38614] ModuleNotFoundError: No module named 'contextvars'

You have configured Apache with python3.6 but run python3.10 in your virt.env (contextvars is part of python since version 3.7)

You should always run Apache with the same python version as in the virt.env

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM