fix: 修复打包后ModuleNotFoundError问题

- 添加setup_path()函数处理PyInstaller打包后的路径
- 检测sys.frozen判断是打包环境还是开发环境
- 打包环境使用sys._MEIPASS作为基准路径
- 确保'gui'模块在两种环境下都能正确导入
This commit is contained in:
congsh
2026-02-12 10:50:33 +08:00
parent c6025d9858
commit 114f432f09

View File

@@ -8,10 +8,24 @@ CutThenThink 应用入口
import sys import sys
import os import os
# 添加src目录到路径 def setup_path():
"""设置Python路径兼容开发和打包环境"""
if getattr(sys, 'frozen', False):
# PyInstaller打包后的环境
# 在打包环境中src目录会被解压到sys._MEIPASS
base_path = sys._MEIPASS
src_path = os.path.join(base_path, 'src')
if os.path.exists(src_path):
sys.path.insert(0, src_path)
else:
sys.path.insert(0, base_path)
else:
# 开发环境
current_dir = os.path.dirname(os.path.abspath(__file__)) current_dir = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, current_dir) sys.path.insert(0, current_dir)
setup_path()
from gui.main_window import main from gui.main_window import main