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

Python3.8部署Django CMS遇django模块缺失及网关超时问题求助

问题描述

此前相同配置可正常运行基于Python3.7的Django应用,但安装基于Python3.8的Django CMS后配置失效,出现ModuleNotFoundError: No module named 'django'错误,相关信息如下:

错误日志

Traceback (most recent call last):
File "/home/siteuser/public_html/core/wsgi.py", line 12, in
from django.core.wsgi import get_wsgi_application
ModuleNotFoundError: No module named 'django'
mod_wsgi (pid=13800): Failed to exec Python script file '/home/siteuser/public_html/core/wsgi.py'.
mod_wsgi (pid=13800): Exception occurred processing WSGI script '/home/siteuser/public_html/core/wsgi.py'.
Traceback (most recent call last):
File "/home/siteuser/public_html/core/wsgi.py", line 12, in
from django.core.wsgi import get_wsgi_application
ModuleNotFoundError: No module named 'django'
mod_wsgi (pid=13800): Failed to exec Python script file '/home/siteuser/public_html/core/wsgi.py'.
mod_wsgi (pid=13800): Exception occurred processing WSGI script '/home/siteuser/public_html/core/wsgi.py'.
Traceback (most recent call last):
File "/home/siteuser/public_html/core/wsgi.py", line 12, in
from django.core.wsgi import get_wsgi_application
ModuleNotFoundError: No module named 'django'
Timeout when reading response headers from daemon process 'siteuser.net': /home/siteuser/public_html/core/wsgi.py
Timeout when reading response headers from daemon process 'siteuser.net': /home/siteuser/public_html/core/wsgi.py
Timeout when reading response headers from daemon process 'siteuser.net': /home/siteuser/public_html/core/wsgi.py, referer: https://siteuser.net/
Timeout when reading response headers from daemon process 'siteuser.net': /home/siteuser/public_html/core/wsgi.py

当前wsgi.py代码

import os
from django.core.wsgi import get_wsgi_application
sys.path.append('/home/siteuser/public_html/')
sys.path.append('/venv/lib/python3.8/site-packages')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings')
application = get_wsgi_application()

当前.conf配置

WSGIDaemonProcess siteuser.net python-home=/home/siteuser/public_html/venv
WSGIProcessGroup siteuser.net
WSGIScriptAlias / /home/siteuser/public_html/core/wsgi.py

<Directory "/home/siteuser/public_html/core">
    <Files wsgi.py>
        Require all granted
    </Files>
</Directory>
解决方案

1. 修复wsgi.py的路径与导入顺序问题

wsgi.py存在两个问题:一是未导入sys模块就调用sys.path;二是虚拟环境路径为相对路径,Python无法识别;三是导入django的语句放在路径配置前,导致路径生效前就尝试加载模块。修改后的代码如下:

import sys
import os

# 添加项目根目录和虚拟环境包路径
sys.path.append('/home/siteuser/public_html/')
sys.path.append('/home/siteuser/public_html/venv/lib/python3.8/site-packages')

# 设置Django配置文件
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings')

# 路径配置完成后再导入Django模块
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

2. 验证虚拟环境的Python版本

执行以下命令确认虚拟环境使用的是Python3.8:

/home/siteuser/public_html/venv/bin/python --version

如果输出不是Python3.8,需重新创建对应版本的虚拟环境,并重新安装Django CMS及依赖包。

3. 检查mod_wsgi与Python3.8的兼容性

确保服务器上的mod_wsgi是针对Python3.8编译的,执行命令查看版本匹配情况:

mod_wsgi-express module-config

若输出中的Python版本不是3.8,需重新编译或安装适配Python3.8的mod_wsgi版本。

4. 重启Apache服务

修改配置后必须重启Apache才能让新配置生效:

sudo systemctl restart apache2

内容的提问来源于stack exchange,提问作者Abzal Ali

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 00:52:40