|
32 | 32 | import json
|
33 | 33 | import logging
|
34 | 34 | import os
|
| 35 | +import platform |
| 36 | +import shutil |
35 | 37 | import tempfile
|
36 | 38 |
|
37 | 39 | # pylint: disable=too-few-public-methods
|
38 | 40 |
|
39 | 41 | try:
|
40 | 42 | 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 |
42 | 75 |
|
43 | 76 | if not hasattr(FreeCAD, "Console"):
|
44 | 77 | raise ImportError("Unrecognized FreeCAD version")
|
@@ -105,6 +138,9 @@ def translate(context: str, sourceText: str, disambiguation: str = "", n: int =
|
105 | 138 | def Version():
|
106 | 139 | return 1, 1, 0, "dev"
|
107 | 140 |
|
| 141 | + def get_python_exe(): |
| 142 | + return shutil.which("python3") |
| 143 | + |
108 | 144 | class ConsoleReplacement:
|
109 | 145 | """If FreeCAD's Console is not available, create a replacement by redirecting FreeCAD
|
110 | 146 | log calls to Python's built-in logging facility."""
|
|
0 commit comments