Skip to content

Commit 29fd992

Browse files
committed
Provide get_python_exe for old versions of FreeCAD
1 parent 2b4bc64 commit 29fd992

File tree

2 files changed

+38
-10
lines changed

2 files changed

+38
-10
lines changed

addonmanager_freecad_interface.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,46 @@
3232
import json
3333
import logging
3434
import os
35+
import platform
36+
import shutil
3537
import tempfile
3638

3739
# pylint: disable=too-few-public-methods
3840

3941
try:
4042
import FreeCAD
41-
from freecad.utils import get_python_exe
43+
44+
try:
45+
from freecad.utils import get_python_exe
46+
except ImportError:
47+
# This was only added in FreeCAD 1.0 -- to support FreeCAD 0.21 a backup strategy must be
48+
# used. This code is borrowed from FreeCAD 1.1dev.
49+
def get_python_exe():
50+
prefs = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/PythonConsole")
51+
python_exe = prefs.GetString("ExternalPythonExecutable", "Not set")
52+
fc_dir = FreeCAD.getHomePath()
53+
if not python_exe or python_exe == "Not set" or not os.path.exists(python_exe):
54+
python_exe = os.path.join(fc_dir, "bin", "python3")
55+
if "Windows" in platform.system():
56+
python_exe += ".exe"
57+
58+
if not python_exe or not os.path.exists(python_exe):
59+
python_exe = os.path.join(fc_dir, "bin", "python")
60+
if "Windows" in platform.system():
61+
python_exe += ".exe"
62+
63+
if not python_exe or not os.path.exists(python_exe):
64+
python_exe = shutil.which("python3")
65+
66+
if not python_exe or not os.path.exists(python_exe):
67+
python_exe = shutil.which("python")
68+
69+
if not python_exe or not os.path.exists(python_exe):
70+
return ""
71+
72+
python_exe = python_exe.replace("/", os.path.sep)
73+
prefs.SetString("ExternalPythonExecutable", python_exe)
74+
return python_exe
4275

4376
if not hasattr(FreeCAD, "Console"):
4477
raise ImportError("Unrecognized FreeCAD version")
@@ -105,6 +138,9 @@ def translate(context: str, sourceText: str, disambiguation: str = "", n: int =
105138
def Version():
106139
return 1, 1, 0, "dev"
107140

141+
def get_python_exe():
142+
return shutil.which("python3")
143+
108144
class ConsoleReplacement:
109145
"""If FreeCAD's Console is not available, create a replacement by redirecting FreeCAD
110146
log calls to Python's built-in logging facility."""

addonmanager_utilities.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,6 @@
4949

5050
import addonmanager_freecad_interface as fci
5151

52-
try:
53-
from freecad.utils import get_python_exe
54-
except ImportError:
55-
56-
def get_python_exe():
57-
"""Use shutil.which to find python executable"""
58-
return shutil.which("python")
59-
6052

6153
if fci.FreeCADGui:
6254

@@ -597,7 +589,7 @@ def create_pip_call(args: List[str]) -> List[str]:
597589
call_args = [python_exe, "-m", "pip", "--disable-pip-version-check"]
598590
call_args.extend(args)
599591
else:
600-
python_exe = get_python_exe()
592+
python_exe = fci.get_python_exe()
601593
if not python_exe:
602594
raise RuntimeError("Could not locate Python executable on this system")
603595
call_args = [python_exe, "-m", "pip", "--disable-pip-version-check"]

0 commit comments

Comments
 (0)