diff --git a/src/Ext/freecad/CMakeLists.txt b/src/Ext/freecad/CMakeLists.txt index 5a8246f67e..2fea55b9ac 100644 --- a/src/Ext/freecad/CMakeLists.txt +++ b/src/Ext/freecad/CMakeLists.txt @@ -27,6 +27,7 @@ endif() configure_file(__init__.py.template ${NAMESPACE_INIT}) configure_file(project_utility.py ${NAMESPACE_DIR}/project_utility.py) configure_file(UiTools.py ${NAMESPACE_DIR}/UiTools.py) +configure_file(utils.py ${NAMESPACE_DIR}/utils.py) if (INSTALL_TO_SITEPACKAGES) SET(SITE_PACKAGE_DIR ${PYTHON_MAIN_DIR}/freecad) @@ -39,6 +40,7 @@ INSTALL( ${NAMESPACE_INIT} project_utility.py UiTools.py + utils.py DESTINATION ${SITE_PACKAGE_DIR} ) diff --git a/src/Ext/freecad/utils.py b/src/Ext/freecad/utils.py new file mode 100644 index 0000000000..01bb923e50 --- /dev/null +++ b/src/Ext/freecad/utils.py @@ -0,0 +1,63 @@ +# SPDX-License-Identifier: LGPL-2.1-or-later +# *************************************************************************** +# * * +# * Copyright (c) 2022-2023 FreeCAD Project Association * +# * Copyright (c) 2018 Gaël Écorchard * +# * * +# * This file is part of FreeCAD. * +# * * +# * FreeCAD is free software: you can redistribute it and/or modify it * +# * under the terms of the GNU Lesser General Public License as * +# * published by the Free Software Foundation, either version 2.1 of the * +# * License, or (at your option) any later version. * +# * * +# * FreeCAD is distributed in the hope that it will be useful, but * +# * WITHOUT ANY WARRANTY; without even the implied warranty of * +# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * +# * Lesser General Public License for more details. * +# * * +# * You should have received a copy of the GNU Lesser General Public * +# * License along with FreeCAD. If not, see * +# * . * +# * * +# *************************************************************************** + +import os +import platform +import shutil + +import FreeCAD + + +def get_python_exe() -> str: + """Find Python. In preference order + A) The value of the PythonExecutableForPip user preference + B) The executable located in the same bin directory as FreeCAD and called "python3" + C) The executable located in the same bin directory as FreeCAD and called "python" + D) The result of a shutil search for your system's "python3" executable + E) The result of a shutil search for your system's "python" executable""" + prefs = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/PythonConsole") + python_exe = prefs.GetString("ExternalPythonExecutable", "Not set") + fc_dir = FreeCAD.getHomePath() + if not python_exe or python_exe == "Not set" or not os.path.exists(python_exe): + python_exe = os.path.join(fc_dir, "bin", "python3") + if "Windows" in platform.system(): + python_exe += ".exe" + + if not python_exe or not os.path.exists(python_exe): + python_exe = os.path.join(fc_dir, "bin", "python") + if "Windows" in platform.system(): + python_exe += ".exe" + + if not python_exe or not os.path.exists(python_exe): + python_exe = shutil.which("python3") + + if not python_exe or not os.path.exists(python_exe): + python_exe = shutil.which("python") + + if not python_exe or not os.path.exists(python_exe): + return "" + + python_exe = python_exe.replace("/", os.path.sep) + prefs.SetString("ExternalPythonExecutable", python_exe) + return python_exe diff --git a/src/Gui/PreferencePages/DlgSettingsPythonConsole.cpp b/src/Gui/PreferencePages/DlgSettingsPythonConsole.cpp index 0734266741..87ca89034f 100644 --- a/src/Gui/PreferencePages/DlgSettingsPythonConsole.cpp +++ b/src/Gui/PreferencePages/DlgSettingsPythonConsole.cpp @@ -45,6 +45,7 @@ void DlgSettingsPythonConsole::saveSettings() ui->PythonBlockCursor->onSave(); ui->PythonSaveHistory->onSave(); ui->ProfilerInterval->onSave(); + ui->ExternalPythonExecutable->onSave(); } void DlgSettingsPythonConsole::loadSettings() @@ -53,6 +54,7 @@ void DlgSettingsPythonConsole::loadSettings() ui->PythonBlockCursor->onRestore(); ui->PythonSaveHistory->onRestore(); ui->ProfilerInterval->onRestore(); + ui->ExternalPythonExecutable->onRestore(); } void DlgSettingsPythonConsole::changeEvent(QEvent* event) diff --git a/src/Gui/PreferencePages/DlgSettingsPythonConsole.ui b/src/Gui/PreferencePages/DlgSettingsPythonConsole.ui index b7f2ac20b6..0c5292d921 100644 --- a/src/Gui/PreferencePages/DlgSettingsPythonConsole.ui +++ b/src/Gui/PreferencePages/DlgSettingsPythonConsole.ui @@ -11,13 +11,13 @@ - Python console + General - Settings + Console @@ -111,6 +111,47 @@ horizontal space in Python console + + + Other + + + + + + Path to external Python executable (optional): + + + + + + + + 0 + 0 + + + + + 300 + 0 + + + + Used for package installation with pip and debugging with debugpy. Autodetected if needed and not specified. + + + ExternalPythonExecutable + + + PythonConsole + + + + + + + Qt::Vertical @@ -135,6 +176,11 @@ horizontal space in Python console Gui::PrefSpinBox QSpinBox
Gui/PrefWidgets.h
+ + + Gui::PrefFileChooser + QWidget +
Gui/PrefWidgets.h