1
# SPDX-License-Identifier: LGPL-2.1-or-later
2
# ***************************************************************************
4
# * Copyright (c) 2022-2023 FreeCAD Project Association *
5
# * Copyright (c) 2018 Gaël Écorchard <galou_breizh@yahoo.fr> *
7
# * This file is part of FreeCAD. *
9
# * FreeCAD is free software: you can redistribute it and/or modify it *
10
# * under the terms of the GNU Lesser General Public License as *
11
# * published by the Free Software Foundation, either version 2.1 of the *
12
# * License, or (at your option) any later version. *
14
# * FreeCAD is distributed in the hope that it will be useful, but *
15
# * WITHOUT ANY WARRANTY; without even the implied warranty of *
16
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
17
# * Lesser General Public License for more details. *
19
# * You should have received a copy of the GNU Lesser General Public *
20
# * License along with FreeCAD. If not, see *
21
# * <https://www.gnu.org/licenses/>. *
23
# ***************************************************************************
32
def get_python_exe() -> str:
33
"""Find Python. In preference order
34
A) The value of the PythonExecutableForPip user preference
35
B) The executable located in the same bin directory as FreeCAD and called "python3"
36
C) The executable located in the same bin directory as FreeCAD and called "python"
37
D) The result of a shutil search for your system's "python3" executable
38
E) The result of a shutil search for your system's "python" executable"""
39
prefs = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/PythonConsole")
40
python_exe = prefs.GetString("ExternalPythonExecutable", "Not set")
41
fc_dir = FreeCAD.getHomePath()
42
if not python_exe or python_exe == "Not set" or not os.path.exists(python_exe):
43
python_exe = os.path.join(fc_dir, "bin", "python3")
44
if "Windows" in platform.system():
47
if not python_exe or not os.path.exists(python_exe):
48
python_exe = os.path.join(fc_dir, "bin", "python")
49
if "Windows" in platform.system():
52
if not python_exe or not os.path.exists(python_exe):
53
python_exe = shutil.which("python3")
55
if not python_exe or not os.path.exists(python_exe):
56
python_exe = shutil.which("python")
58
if not python_exe or not os.path.exists(python_exe):
61
python_exe = python_exe.replace("/", os.path.sep)
62
prefs.SetString("ExternalPythonExecutable", python_exe)