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

virtualenv安装Python2.7版Trac后Apache2 WSGI运行报错如何解决

问题根因

你直接执行pip install trac默认安装了适配Python3的高版本Trac,该版本已将Python2独有的BaseHTTPServer依赖替换为Python3的http模块,因此在Python2.7环境下运行会抛出导入失败错误,pip install http无效是因为http是Python3标准库,无法在Python2环境下安装使用。

解决步骤

  1. 卸载当前不兼容的高版本Trac
    在你的Python2.7虚拟环境中执行命令:
pip uninstall -y trac
  1. 安装适配Python2.7的最高稳定版Trac
    Trac 1.4.x是官方最后一个支持Python2的稳定分支,执行以下命令安装指定版本:
pip install trac==1.4.6
  1. 修改wsgi配置文件,加载虚拟环境路径
    你当前的wsgi配置没有指定使用自建的Python2.7虚拟环境,Apache的mod_wsgi默认会调用系统Python,因此需要在wsgi文件最开头添加如下代码,注意路径要和你创建的虚拟环境路径匹配:
# 加载Python2.7虚拟环境
activate_this = '/var/lib/trac/env/bin/activate_this.py'
with open(activate_this) as f:
    exec(f.read(), dict(__file__=activate_this))

修改后的完整wsgi配置如下:

# 先加载虚拟环境
activate_this = '/var/lib/trac/env/bin/activate_this.py'
with open(activate_this) as f:
    exec(f.read(), dict(__file__=activate_this))

import sys
sys.stdout = sys.stderr

import os

os.environ['PKG_RESOURCES_CACHE_ZIP_MANIFESTS'] = '1'
os.environ['TRAC_ENV_PARENT_DIR'] = '/var/lib/trac'
os.environ['PYTHON_EGG_CACHE'] = '/var/lib/trac/eggs'

import trac.web.main

application = trac.web.main.dispatch_request
  1. 检查Apache mod_wsgi版本
    确认你安装的是适配Python2.7的mod_wsgi版本:
  • Debian/Ubuntu系需安装libapache2-mod-wsgi,不要安装libapache2-mod-wsgi-py3
  • RHEL/CentOS系需安装mod_wsgi对应的Python2版本包

所有配置修改完成后重启Apache服务即可生效:

systemctl restart apache2

内容的提问来源于stack exchange,提问作者Tomasz Brzezina

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.26 06:06:05