From 114f432f09338bea7cd2dc4dd5fa35ca9a2802e8 Mon Sep 17 00:00:00 2001 From: congsh Date: Thu, 12 Feb 2026 10:50:33 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E6=89=93=E5=8C=85?= =?UTF-8?q?=E5=90=8EModuleNotFoundError=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加setup_path()函数处理PyInstaller打包后的路径 - 检测sys.frozen判断是打包环境还是开发环境 - 打包环境使用sys._MEIPASS作为基准路径 - 确保'gui'模块在两种环境下都能正确导入 --- src/main.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/main.py b/src/main.py index fd0c0ec..07e8b3e 100644 --- a/src/main.py +++ b/src/main.py @@ -8,9 +8,23 @@ CutThenThink 应用入口 import sys import os -# 添加src目录到路径 -current_dir = os.path.dirname(os.path.abspath(__file__)) -sys.path.insert(0, current_dir) +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__)) + sys.path.insert(0, current_dir) + +setup_path() from gui.main_window import main