fix: 修复打包后ModuleNotFoundError问题
- 添加setup_path()函数处理PyInstaller打包后的路径 - 检测sys.frozen判断是打包环境还是开发环境 - 打包环境使用sys._MEIPASS作为基准路径 - 确保'gui'模块在两种环境下都能正确导入
This commit is contained in:
20
src/main.py
20
src/main.py
@@ -8,9 +8,23 @@ CutThenThink 应用入口
|
|||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
|
|
||||||
# 添加src目录到路径
|
def setup_path():
|
||||||
current_dir = os.path.dirname(os.path.abspath(__file__))
|
"""设置Python路径,兼容开发和打包环境"""
|
||||||
sys.path.insert(0, current_dir)
|
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__))
|
||||||
|
sys.path.insert(0, current_dir)
|
||||||
|
|
||||||
|
setup_path()
|
||||||
|
|
||||||
from gui.main_window import main
|
from gui.main_window import main
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user