From 43434135881383d2ed1a868521e120e958248774 Mon Sep 17 00:00:00 2001 From: Antti Soininen Date: Mon, 19 Feb 2024 09:38:17 +0200 Subject: [PATCH 1/2] Change how Python interpreter is resolved resolve_python_interpreter() has now been refactored into two functions: resolve_current_python_interpreter() returns the current Python executable while resolve_python_interpreter() returns returns the interpreter set in application settings. Re spine-tools/Spine-Toolbox#2446 --- spine_engine/project_item/connection.py | 4 ++-- spine_engine/utils/helpers.py | 31 +++++++++++++++++-------- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/spine_engine/project_item/connection.py b/spine_engine/project_item/connection.py index f7b8257b..ff626e5b 100644 --- a/spine_engine/project_item/connection.py +++ b/spine_engine/project_item/connection.py @@ -28,7 +28,7 @@ labelled_resource_args, ) from spine_engine.utils.helpers import ( - resolve_python_interpreter, + resolve_current_python_interpreter, ItemExecutionFinishState, PartCount, ExecutionDirection as ED, @@ -674,7 +674,7 @@ def _is_python_script_condition_true(self, jump_counter): with tempfile.TemporaryFile("w+", encoding="utf-8") as script_file: script_file.write(script) script_file.seek(0) - python = resolve_python_interpreter("") + python = resolve_current_python_interpreter() result = subprocess.run( [python, "-", *expanded_args], encoding="utf-8", stdin=script_file, capture_output=True ) diff --git a/spine_engine/utils/helpers.py b/spine_engine/utils/helpers.py index 1bd74ff4..d4da7369 100644 --- a/spine_engine/utils/helpers.py +++ b/spine_engine/utils/helpers.py @@ -10,10 +10,7 @@ # this program. If not, see . ###################################################################################################################### -""" -Helpers functions and classes. - -""" +""" Helpers functions and classes. """ import collections import os import sys @@ -114,13 +111,27 @@ def resolve_conda_executable(conda_path): return conda_exe -def resolve_python_interpreter(python_path): - """If given python_path is empty, returns the - full path to Python interpreter depending on user's - settings and whether the app is frozen or not. +def resolve_python_interpreter(settings): + """Returns a path to Python interpreter in settings or the current executable if none is set. + + Args: + settings (AppSettings): settings + + Returns: + str: path to Python interpreter + """ + path = settings.value("appSettings/pythonPath") + if path: + return path + return resolve_current_python_interpreter() + + +def resolve_current_python_interpreter(): + """Returns a path to current Python interpreter. + + Returns: + str: path to Python interpreter """ - if python_path != "": - return python_path if not getattr(sys, "frozen", False): return sys.executable # Use current Python # We are frozen From d7c9f2fce63a50d6dcc05587de4fb7429b2ef9dc Mon Sep 17 00:00:00 2001 From: Antti Soininen Date: Mon, 19 Feb 2024 12:15:30 +0200 Subject: [PATCH 2/2] Change how Julia executable is resolved resolve_julia_executable() has now been refactored into two functions: resolve_default_julia_executable() returns the Julia executable from PATH and resolve_julia_executable() returns returns the executable set in application settings. Re spine-tools/Spine-Toolbox#2446 --- spine_engine/utils/helpers.py | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/spine_engine/utils/helpers.py b/spine_engine/utils/helpers.py index d4da7369..43b62eec 100644 --- a/spine_engine/utils/helpers.py +++ b/spine_engine/utils/helpers.py @@ -65,8 +65,6 @@ class AppSettings: def __init__(self, settings): """ - Init. - Args: settings (dict) """ @@ -141,19 +139,36 @@ def resolve_current_python_interpreter(): return EMBEDDED_PYTHON # Use embedded /Tools/python.exe -def resolve_julia_executable(julia_path): - """if given julia_path is empty, tries to find the path to Julia - in user's PATH env variable. If Julia is not found in PATH, - returns an empty string. +def resolve_julia_executable(settings): + """Returns path to Julia executable from settings, and, if not set, path to default Julia executable. + + Args: + settings (AppSettings): application settings + + Returns: + str: path to Julia executable + """ + path = settings.value("appSettings/juliaPath") + if path: + return path + return resolve_default_julia_executable() + + +def resolve_default_julia_executable(): + """Returns path to default Julia executable. + + Tries to find the path to Julia in user's PATH env variable. + If Julia is not found in PATH, returns an empty string. Note: In the long run, we should decide whether this is something we want to do because adding julia-x.x./bin/ dir to the PATH is not recommended because this also exposes some .dlls to other programs on user's (windows) system. I.e. it may break other programs, and this is why the Julia installer does not add (and does not even offer the chance to add) Julia to PATH. + + Returns: + str: path to Julia executable """ - if julia_path != "": - return julia_path return resolve_executable_from_path(JULIA_EXECUTABLE)