Skip to content

Commit

Permalink
Add support for PyInstaller bundle
Browse files Browse the repository at this point in the history
The embedded Python interpreter is in different path in PyInstaller
bundles than in cx_Freeze ones.
  • Loading branch information
soininen committed Feb 22, 2024
1 parent c14e57b commit 026918b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
30 changes: 28 additions & 2 deletions spine_engine/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,38 @@ def _executable(name):
JULIA_EXECUTABLE = _executable("julia")

# Python
def is_frozen():
"""Checks if we are currently running as frozen bundle.
Returns:
bool: True if we are frozen, False otherwise
"""
return getattr(sys, "frozen", False)


def is_pyinstaller_bundle():
"""Checks if we are in a PyInstaller bundle.
Returns:
bool: True if the current bundle has been build by PyInstaller, False otherwise
"""
return hasattr(sys, "_MEIPASS")


PYTHON_EXECUTABLE = _executable("python" if _on_windows else "python3")
_frozen = getattr(sys, "frozen", False)
_path_to_executable = os.path.dirname(sys.executable if _frozen else __file__)
APPLICATION_PATH = os.path.realpath(_path_to_executable)
# Experimental Python interpreter shipped with Spine Toolbox installation bundle
EMBEDDED_PYTHON = os.path.join(APPLICATION_PATH, "tools", "python.exe")
# Python interpreter shipped with bundle
BUNDLE_DIR = "Python"
if is_frozen():
if is_pyinstaller_bundle():
EMBEDDED_PYTHON = os.path.join(sys._MEIPASS, BUNDLE_DIR, PYTHON_EXECUTABLE)
else:
EMBEDDED_PYTHON = os.path.join(APPLICATION_PATH, BUNDLE_DIR, PYTHON_EXECUTABLE)
else:
EMBEDDED_PYTHON = None


# Tool output directory name
TOOL_OUTPUT_DIR = "output"
4 changes: 2 additions & 2 deletions spine_engine/utils/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import networkx
from jupyter_client.kernelspec import find_kernel_specs
from spinedb_api.spine_io.gdx_utils import find_gams_directory
from ..config import PYTHON_EXECUTABLE, JULIA_EXECUTABLE, GAMS_EXECUTABLE, EMBEDDED_PYTHON
from ..config import is_frozen, PYTHON_EXECUTABLE, JULIA_EXECUTABLE, GAMS_EXECUTABLE, EMBEDDED_PYTHON


@unique
Expand Down Expand Up @@ -120,7 +120,7 @@ def resolve_python_interpreter(python_path):
"""
if python_path != "":
return python_path
if not getattr(sys, "frozen", False):
if not is_frozen():
return sys.executable # Use current Python
# We are frozen
path = resolve_executable_from_path(PYTHON_EXECUTABLE)
Expand Down

0 comments on commit 026918b

Please sign in to comment.