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

Flask应用Apache+mod_wsgi部署时ModuleNotFoundError问题求助

解决Flask应用通过Apache mod_wsgi部署时"devices"模块找不到的问题

问题根源

你在app.wsgi中错误地将sys.path指向了devices目录本身,导致Python无法把devices识别为一个可导入的包。本地Werkzeug服务器运行正常,是因为你大概率是在grp_exp_structure目录下启动脚本,Python自动将该父目录加入了sys.path,而Apache mod_wsgi环境下没有这个默认行为。

解决方案

1. 修改app.wsgi的路径配置

将sys.path的插入路径改为devices的父目录grp_exp_structure,同时调整webapp的导入路径:

#! C:/Users/pvachal/AppData/Local/Programs/Python/Python310/python.exe

import sys

# 把父目录加入sys.path,让Python识别devices为合法包
sys.path.insert(0, "c:/users/pvachal/documents/projekty/grp_exp_structure")

activate_this = "c:/users/pvachal/documents/projekty/grp_exp_structure/venv/scripts/activate_this.py"
with open(activate_this) as file_:
    exec(file_.read(), dict(__file__=activate_this))

# 从devices包中导入webapp模块
from devices.webapp import Webapp
application = Webapp().app

2. 保持webapp.py的导入逻辑不变

webapp.py中的导入语句无需修改,因为现在grp_exp_structure在sys.path中,devices是标准的Python包(存在__init__.py):

from devices import Dev1Cls

3. 额外验证项

  • 检查devices/__init__.py是否存在语法错误,语法错误会导致包加载失败
  • 确认Apache运行用户对grp_exp_structure及其子目录有读取权限
  • Windows环境下尽量保持代码中路径与实际目录名大小写一致,避免潜在的识别问题

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 00:02:46