@@ -92,14 +92,56 @@ def _set_process_callback(self, callback: Callable) -> None:
9292 self .pyinstaller_packager .set_process_callback (callback )
9393 self .nuitka_packager .set_process_callback (callback )
9494
95- def _get_python_path (self , config : Dict ) -> Optional [str ]:
96- """获取 Python 解释器路径(不处理虚拟环境,仅获取基础解释器)"""
97- # 优先使用配置指定的解释器
95+ def _get_python_path (self , config : Dict ) -> Tuple [Optional [str ], str ]:
96+ """
97+ 获取 Python 解释器路径(不处理虚拟环境,仅获取基础解释器)
98+
99+ 优先级:
100+ 1. 用户在配置中手动指定的路径(经过有效性校验)
101+ 2. 当前运行的解释器(仅在非打包环境下)
102+ 3. 通过 PythonFinder 在系统中搜索
103+
104+ Returns:
105+ (python_path, error_message) 元组。
106+ 成功时 python_path 为有效路径、error_message 为空字符串;
107+ 失败时 python_path 为 None、error_message 为描述性错误信息。
108+ """
109+ # 1. 优先使用配置指定的解释器
98110 python_path = config .get ("python_path" ) or config .get ("python" )
99111 if python_path and os .path .exists (python_path ):
100- return python_path
101- # 否则使用当前运行的解释器
102- return sys .executable
112+ # 即使用户手动指定,也要验证它不是打包环境中的临时文件
113+ if PythonFinder .is_valid_python_interpreter (python_path ):
114+ return python_path , ""
115+ else :
116+ self .log (f"警告: 指定的 Python 路径不是有效的解释器: { python_path } " )
117+
118+ # 2. 检测是否处于 PyInstaller/Nuitka 打包后的环境中
119+ if PythonFinder .is_bundled_environment ():
120+ self .log (
121+ "检测到当前运行在打包环境中,sys.executable 不可用于创建虚拟环境"
122+ )
123+ self .log (f" sys.executable = { sys .executable } " )
124+ self .log ("正在搜索系统中安装的 Python 解释器..." )
125+
126+ finder = PythonFinder ()
127+ system_python = finder .find_python ()
128+ if system_python :
129+ self .log (f"✓ 找到系统 Python: { system_python } " )
130+ return system_python , ""
131+ else :
132+ error_msg = (
133+ "未在系统中找到可用的 Python 解释器。\n \n "
134+ "请在工具界面的「Python路径」中手动指定系统安装的 Python 解释器路径\n "
135+ "(例如 C:\\ Python311\\ python.exe)。\n \n "
136+ "如果尚未安装 Python,请先从 https://www.python.org 下载安装,\n "
137+ "安装时请勾选「Add Python to PATH」。"
138+ )
139+ self .log ("错误: 未在系统中找到可用的 Python 解释器" )
140+ self .log ("请在工具界面中手动指定 Python 路径" )
141+ return None , error_msg
142+
143+ # 3. 非打包环境,使用当前解释器
144+ return sys .executable , ""
103145
104146 def _setup_venv_if_needed (
105147 self ,
@@ -125,6 +167,10 @@ def _setup_venv_if_needed(
125167
126168 if not use_venv :
127169 self .log ("未启用虚拟环境,使用指定的 Python 解释器" )
170+ # 验证基础 Python 解释器是否存在
171+ if not os .path .exists (base_python_path ):
172+ self .log (f"错误: 指定的 Python 解释器不存在: { base_python_path } " )
173+ raise FileNotFoundError (f"Python 解释器不存在: { base_python_path } " )
128174 return base_python_path
129175
130176 project_dir = config .get ("project_dir" )
@@ -140,15 +186,37 @@ def _setup_venv_if_needed(
140186 self .log ("虚拟环境设置" )
141187 self .log ("=" * 50 )
142188
189+ # 验证基础 Python 解释器路径
190+ if not os .path .exists (base_python_path ):
191+ self .log (f"错误: 基础 Python 解释器不存在: { base_python_path } " )
192+ self .log ("将无法创建虚拟环境,请检查 Python 安装" )
193+ raise FileNotFoundError (f"Python 解释器不存在: { base_python_path } " )
194+
195+ self .log (f"基础 Python 解释器: { base_python_path } " )
196+
143197 # 1. 检查是否存在虚拟环境
144198 existing_venv = self .venv_manager .check_existing_venv (project_dir )
199+ venv_python : Optional [str ] = None
200+ active_venv_path : Optional [str ] = None
145201
146202 if existing_venv :
147203 self .log (f"✓ 检测到现有虚拟环境: { existing_venv } " )
148204 venv_python = self .venv_manager .get_venv_python (existing_venv )
149- else :
205+ active_venv_path = existing_venv
206+
207+ # 验证现有虚拟环境的有效性
208+ if not os .path .exists (venv_python ):
209+ self .log (f"警告: 现有虚拟环境的 Python 解释器不存在: { venv_python } " )
210+ self .log ("尝试验证虚拟环境..." )
211+ if not self .venv_manager .validate_venv (existing_venv , verbose = True ):
212+ self .log ("现有虚拟环境无效,将创建新的虚拟环境" )
213+ existing_venv = None
214+ venv_python = None
215+ active_venv_path = None
216+
217+ if not existing_venv :
150218 # 创建新的虚拟环境
151- self .log ("未检测到虚拟环境 ,正在创建..." )
219+ self .log ("未检测到有效的虚拟环境 ,正在创建..." )
152220 venv_path = self .venv_manager .setup_venv (
153221 project_dir ,
154222 base_python_path ,
@@ -161,14 +229,29 @@ def _setup_venv_if_needed(
161229
162230 self .log (f"✓ 虚拟环境创建成功: { venv_path } " )
163231 venv_python = self .venv_manager .get_venv_python (venv_path )
232+ active_venv_path = venv_path
164233
165- # 升级 pip
166- self .venv_manager .upgrade_pip (venv_path )
234+ # 再次验证虚拟环境是否真的可用
235+ if not os .path .exists (venv_python ):
236+ self .log (f"错误: 虚拟环境创建后 Python 解释器仍不存在: { venv_python } " )
237+ self .log ("虚拟环境创建失败,使用原始 Python 解释器" )
238+ return base_python_path
167239
168- if not os .path .exists (venv_python ):
169- self .log ("警告: 虚拟环境 Python 不存在,使用原始解释器" )
240+ # 升级 pip
241+ if not self .venv_manager .upgrade_pip (venv_path ):
242+ self .log ("警告: pip 升级失败,但将继续使用虚拟环境" )
243+
244+ # 最终验证
245+ if not venv_python or not os .path .exists (venv_python ):
246+ self .log (f"错误: 虚拟环境 Python 解释器不存在: { venv_python } " )
247+ self .log ("详细诊断信息:" )
248+ if active_venv_path :
249+ self .venv_manager .get_venv_python (active_venv_path , verify = True )
250+ self .log ("回退到使用原始 Python 解释器" )
170251 return base_python_path
171252
253+ self .log (f"✓ 将使用虚拟环境 Python: { venv_python } " )
254+
172255 # 2. 安装依赖
173256 self ._install_venv_dependencies (project_dir , venv_python , config )
174257
@@ -784,9 +867,9 @@ def package(
784867 return False , "打包已取消" , None
785868
786869 # 1. 获取基础 Python 路径
787- base_python_path = self ._get_python_path (config )
870+ base_python_path , python_error = self ._get_python_path (config )
788871 if not base_python_path :
789- return False , "未找到 Python 环境" , None
872+ return False , python_error or "未找到 Python 环境" , None
790873
791874 self .log (f"基础 Python: { base_python_path } " )
792875
0 commit comments