You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu22服务器Apache2 WSGI部署Django时Python版本错误排查

Apache2 + WSGI部署Django网站时Python版本不匹配导致Numpy导入错误

在Ubuntu 22服务器上通过Apache2 + WSGI部署Django网站时,网站无法访问,错误日志显示Numpy导入失败,核心问题是系统使用了错误的Python版本:

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.

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.
Original error was: No module named 'numpy.core._multiarray_umath'

Python和Numpy均部署在虚拟环境内,且Numpy已正确安装:

  • 虚拟环境内Python版本:
(djangoenv) ubuntu@server~$ python -V
Python 3.9.16
  • Numpy安装位置正确:
(djangoenv) ubuntu@server:~$ ls /var/www/djangoenv/lib/python3.9/site-packages/ | grep numpy
numpy
numpy-1.22.0.dist-info

Apache虚拟主机配置文件

website.conf(ServerName、Alias及域名已替换为xxxx匿名)

<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>

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>

已尝试的解决方法

  • 使用pip install --upgrade numpy升级NumPy
  • 将NumPy降级至1.21.0
  • 完全重新安装NumPy
  • 使用pip install numpy==1.22.0 --no-binary=:all:安装NumPy
  • 安装Ubuntu22适配的apt包更新WSGI
  • 在虚拟环境内安装mod-wsgi

注:通过运行python manage.py runserver可确认Django应用本身无错误。

内容的提问来源于stack exchange,提问作者Jack Avante

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 21:15:42