Skip to content

Commit

Permalink
Refactor Python interpreter/Julia executable discovery (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
soininen authored Feb 19, 2024
2 parents 2542f77 + d7c9f2f commit d35bdc3
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 20 deletions.
4 changes: 2 additions & 2 deletions spine_engine/project_item/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
)
Expand Down
62 changes: 44 additions & 18 deletions spine_engine/utils/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@
# this program. If not, see <http://www.gnu.org/licenses/>.
######################################################################################################################

"""
Helpers functions and classes.
"""
""" Helpers functions and classes. """
import collections
import os
import sys
Expand Down Expand Up @@ -68,8 +65,6 @@ class AppSettings:

def __init__(self, settings):
"""
Init.
Args:
settings (dict)
"""
Expand Down Expand Up @@ -114,13 +109,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
Expand All @@ -130,19 +139,36 @@ def resolve_python_interpreter(python_path):
return EMBEDDED_PYTHON # Use embedded <app_install_dir>/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)


Expand Down

0 comments on commit d35bdc3

Please sign in to comment.