Skip to content

Commit

Permalink
Fix resolve python interpreter when frozen (#143)
Browse files Browse the repository at this point in the history
Always use embedded Python as default when frozen and on Windows. We cannot search python from the PATH because %USERPROFILE%/AppData/Local/Microsoft/WindowsApps/ is in PATH by default on all users and this dir contains a python.exe that either opens the Microsoft Store or opens the Python that was downloaded from the Microsoft Store.

Fixes spine-tools/Spine-Toolbox#2823
  • Loading branch information
ptsavol authored Jun 7, 2024
1 parent a2d2c42 commit d049af2
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 14 deletions.
6 changes: 1 addition & 5 deletions spine_engine/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@
# this program. If not, see <http://www.gnu.org/licenses/>.
######################################################################################################################

"""
Application constants.
"""

"""Spine Engine constants."""
import os
import sys

Expand Down
7 changes: 4 additions & 3 deletions spine_engine/utils/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,10 @@ def resolve_current_python_interpreter():
"""
if not is_frozen():
return sys.executable
path = resolve_executable_from_path(PYTHON_EXECUTABLE)
if path != "":
return path
if not sys.platform == "win32":
path = resolve_executable_from_path(PYTHON_EXECUTABLE)
if path != "":
return path
return EMBEDDED_PYTHON


Expand Down
42 changes: 36 additions & 6 deletions tests/utils/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,20 @@
# this program. If not, see <http://www.gnu.org/licenses/>.
######################################################################################################################

"""
Unit tests for chunk module.
"""
"""Unit tests for chunk module."""
import sys
import unittest

from unittest import mock
from spine_engine.project_item.connection import Connection, FilterSettings
from spinedb_api.filters.scenario_filter import SCENARIO_FILTER_TYPE
from spinedb_api.helpers import remove_credentials_from_url
from spine_engine.utils.helpers import make_dag, gather_leaf_data, get_file_size, required_items_for_execution
from spine_engine.utils.helpers import (
make_dag,
gather_leaf_data,
get_file_size,
required_items_for_execution,
resolve_python_interpreter,
)


class TestRequiredItemsForExecution(unittest.TestCase):
Expand Down Expand Up @@ -188,5 +192,31 @@ def test_get_file_size(self):
self.assertEqual(expected_output, output)


class TestPythonInterpreter(unittest.TestCase):
def test_resolve_python_interpreter(self):
expected_path = "path_to_python"
settings = TestAppSettings(expected_path)
p = resolve_python_interpreter(settings)
self.assertEqual(expected_path, p)
settings = TestAppSettings("")
p = resolve_python_interpreter(settings)
self.assertEqual(sys.executable, p)
if sys.platform == "win32":
with mock.patch("spine_engine.utils.helpers.is_frozen") as mock_helpers_is_frozen:
mock_helpers_is_frozen.return_value = True
p = resolve_python_interpreter(settings)
self.assertIsNone(p) # This is None only on Win. # FIXME: Find a way to mock is_frozen() in config.py
mock_helpers_is_frozen.assert_called()


class TestAppSettings:
def __init__(self, test_path):
self.test_path = test_path

def value(self, key):
if key == "appSettings/pythonPath":
return self.test_path


if __name__ == "__main__":
unittest.main()

0 comments on commit d049af2

Please sign in to comment.