I'm trying to run my Django website on Apache 2 using WSGI running Ubuntu 22 server, however I found that it is using the incorrect Python. When I try to connect to the website, it fails, and the error log reveals the following information:
ImportError:
IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!
Importing the numpy C-extensions failed. This error can happen for
many reasons, often due to issues with your setup or how NumPy was
installed.
We have compiled some common reasons and troubleshooting tips at:
https://numpy.org/devdocs/user/troubleshooting-importerror.html
Please note and check the following:
* The Python version is: Python3.10 from "/var/www/djangoenv/bin/python"
* The NumPy version is: "1.22.0"
and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.
Original error was: No module named 'numpy.core._multiarray_umath'
The python and numpy are located in a virtual environment, and numpy has been indeed installed into this virtual environment. The numpy version is 1.22.0.
(djangoenv) ubuntu@server~$ python -V
Python 3.9.16
And we can see it is indeed located at the correct location (numpy):
(djangoenv) ubuntu@server:~$ ls /var/www/djangoenv/lib/python3.9/site-packages/ | grep numpy
numpy
numpy-1.22.0.dist-info
And the Virtual Host file in Apache2 is set accordingly:
website.conf
Note: ServerName, Alias and domains replaced with xxxx for anonymization purposes
<VirtualHost *:80>
ServerName xxxx
ServerAlias xxxx
Alias /static /var/www/djangoproject/static
<Directory /var/www/djangoproject/static>
Require all granted
</Directory>
<Directory /var/www/djangoproject/djangoproject>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
<Directory "/var/lib/letsencrypt/http_challenges">
AllowOverride None
Options -Indexes -FollowSymLinks
# Allow open access:
Require all granted
</Directory>
WSGIDaemonProcess djangoproject python-path=/var/www/djangoproject/var/www/djangoenv/lib/python3.9/site-packages python-home=/var/www/djangoenv
WSGIProcessGroup djangoproject
WSGIApplicationGroup %{GLOBAL}
WSGIScriptAlias / /var/www/djangoproject/djangoproject/wsgi.py
RewriteEngine on
RewriteCond %{SERVER_NAME} =xxxx
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
And SSL version website-le-ssl.conf
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName xxxx
ServerAlias xxxx
Alias /static /var/www/djangoproject/static
<Directory /var/www/djangoproject/static>
Require all granted
</Directory>
<Directory /var/www/djangoproject/djangoproject>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
# WSGIDaemonProcess djangoproject python-path=/var/www/djangoproject:/var/www/djangoenv/lib/python3.9/site-packages
WSGIProcessGroup djangoproject
WSGIApplicationGroup %{GLOBAL}
WSGIScriptAlias / /var/www/djangoproject/djangoproject/wsgi.py
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/xxxx/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/xxxx/privkey.pem
</VirtualHost>
</IfModule>
This problem has been described and addressed in several SO threads and GitHub Issues but none of them seem to solve the issue.
Here is a list of things thus attempted:
- Upgrade NumPy using
pip install --upgrade numpy - Downgrade NumPy to 1.21.0
- Reinstall NumPy completely
- Install NumPy using
pip install numpy==1.22.0 --no-binary=:all: - Update WSGI by installing the Ubuntu 22 appropriate apt package
- Install mod-wsgi in the virtual environment itself
I can confirm that the django app works by itself by running
python manage.py runserver with no errors or issues.
Regarding this error:
No module named 'numpy.core._multiarray_umath'I looked at your Apache configs and noticed this line in your HTTP (port 80) config:
The value being passed to the
python-pathoption looks incorrect:/var/www/djangoproject/var/www/djangoenv/lib/python3.9/site-packagesIt looks like it should be this instead:
/var/www/djangoenv/lib/python3.9/site-packages