简体   繁体   中英

Django/mod_wsgi/Apache - mod_wsgi is not using the Python version it was compiled for - “ModuleNotFoundError: No module named 'math' ”

I'm attempting to deploy a Django application with Apache2 and mod_wsgi on a Ubuntu 16.04.6 server, but I'm struggling with getting mod_wsgi to use the correct python version.

I installed mod_wsgi from source, and configured it to compile against the system python3, specifically python3.7.8

My Virtual environment for the Django app is also running python3.7.8.

My VirtualHost config file looks like this:

<IfModule mod_ssl.c>
    <VirtualHost *:443>
        ServerName kumberas.com
        ServerAdmin webmaster@localhost.com

        WSGIScriptAlias / /home/dan/app/kumberas/kumberas/main/wsgi.py
        Alias /static/ /home/dan/app/kumberas/kumberas/static/

        <Directory /home/dan/app/kumberas/kumberas/static>
            Require all granted
        </Directory>

        <Directory /home/dan/app/kumberas/venv>
            Require all granted
        </Directory>

        <Directory /home/dan/app/kumberas/kumberas/main>
            <Files wsgi.py>
                Require all granted
            </Files>
        </Directory>

        <Location />
            AuthType Basic
            AuthName "Restricted Content"
            AuthUserFile /etc/apache2/.htpasswd
            Require user kr
            AuthBasicProvider file
        </Location>

        WSGIDaemonProcess kumberas \
            python-home=/home/dan/app/kumberas/venv \
            python-path=/home/dan/app/kumberas
        WSGIProcessGroup kumberas

    </VirtualHost>
</IfModule>

And when I try to access the site, I get a 500 Error and the following in my Apache error log.

[wsgi:error] [pid 21635] [remote xx.xx.xx.xx:58603] mod_wsgi (pid=21635): Failed to exec Python script file '/home/dan/app/kumberas/kumberas/main/wsgi.py'.
[wsgi:error] [pid 21635] [remote xx.xx.xx.xx:58603] mod_wsgi (pid=21635): Exception occurred processing WSGI script '/home/dan/app/kumberas/kumberas/main/wsgi.py'.
[wsgi:error] [pid 21635] [remote xx.xx.xx.xx:58603] Traceback (most recent call last):
[wsgi:error] [pid 21635] [remote xx.xx.xx.xx:58603]   File "/home/dan/app/kumberas/kumberas/main/wsgi.py", line 12, in <module>
[wsgi:error] [pid 21635] [remote xx.xx.xx.xx:58603]     from django.core.wsgi import get_wsgi_application
[wsgi:error] [pid 21635] [remote xx.xx.xx.xx:58603]   File "/home/dan/app/kumberas/venv/lib/python3.7/site-packages/django/__init__.py", line 1, in <module>
[wsgi:error] [pid 21635] [remote xx.xx.xx.xx:58603]     from django.utils.version import get_version
[wsgi:error] [pid 21635] [remote xx.xx.xx.xx:58603]   File "/home/dan/app/kumberas/venv/lib/python3.7/site-packages/django/utils/version.py", line 1, in <module>
[wsgi:error] [pid 21635] [remote xx.xx.xx.xx:58603]     import datetime
[wsgi:error] [pid 21635] [remote xx.xx.xx.xx:58603]   File "/usr/lib/python3.7/datetime.py", line 8, in <module>
[wsgi:error] [pid 21635] [remote xx.xx.xx.xx:58603]     import math as _math
[wsgi:error] [pid 21635] [remote xx.xx.xx.xx:58603] ModuleNotFoundError: No module named 'math'

I did a bit of digging and found this wsgi.py file in the mod_wsgi documentation for checking the python version that mod_wsgi is using, and get the following output when I visit the site:

sys.version = '3.7.2 (default, Jan 23 2020, 09:44:10) \n[GCC 5.4.0 20160609]'
sys.prefix = '/home/dan/app/kumberas/venv'

I assume the mismatching python version 3.7.8.= 3.7.2 is the issue here, but as far as I'm aware there isn't a python3.7.2 installation on the server, and I've reinstalled mod_wsgi several times making certain that I configure it to use 3.7.8.

I've also confirmed that while in the virtual environment I'm, a) able to run the Django application with manage.py runserver , and b) can run get_wsgi_application() manually in the Django shell.

Does anyone have any knowledge on whether this version mismatch is the issue, and how it could be resolved?

If virtualenv or virtualenvwrapper have been used to create environment then you need to call activation script in wsgi.py:

python_home = '/home/dan/app/kumberas/venv'

activate_this = python_home + '/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))

It looks like you might have a version mismatch. Some linux is still using python 2.7 as the default.

Also check if the system has more than one version of python using locate python and/or locate python3

If it's an issue on modwsgi , it might be easier for you to build mod_wsgi from source following link https://modwsgi.readthedocs.io/en/develop/user-guides/quick-installation-guide.html

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