Skip to content

Commit 5f2a0df

Browse files
authored
Fix PyInstaller packaging: .version file not found error (#4916)
When PaddleX is packaged with PyInstaller, the .version file cannot be located because __file__ points to the temporary extraction directory (_MEIxxxxxx) but the file path resolution doesn't account for the PyInstaller bundle structure. This fix: - Adds _get_package_dir() helper that detects PyInstaller environment using sys.frozen and sys._MEIPASS attributes - Returns correct path for both normal and frozen (PyInstaller) execution - Adds fallback to "unknown" version if file is still not found Users must still include the .version file in their PyInstaller bundle using --add-data or datas in spec file.
1 parent 2771144 commit 5f2a0df

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

paddlex/version.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,30 @@
1414

1515

1616
import os
17+
import sys
1718

1819
__all__ = ["get_pdx_version", "get_version_dict", "show_versions"]
1920

2021

22+
def _get_package_dir():
23+
"""Get the paddlex package directory, compatible with PyInstaller."""
24+
# When running in a PyInstaller bundle, sys._MEIPASS points to the
25+
# temporary folder where the bundled files are extracted
26+
if getattr(sys, "frozen", False) and hasattr(sys, "_MEIPASS"):
27+
return os.path.join(sys._MEIPASS, "paddlex")
28+
return os.path.dirname(__file__)
29+
30+
2131
def get_pdx_version():
2232
"""get_pdx_version"""
23-
with open(
24-
os.path.join(os.path.dirname(__file__), ".version"), "r", encoding="ascii"
25-
) as fv:
26-
ver = fv.read().rstrip()
33+
version_file = os.path.join(_get_package_dir(), ".version")
34+
try:
35+
with open(version_file, "r", encoding="ascii") as fv:
36+
ver = fv.read().rstrip()
37+
except FileNotFoundError:
38+
# Fallback version if .version file is not found (e.g., in some
39+
# PyInstaller configurations where the file wasn't included)
40+
ver = "unknown"
2741
return ver
2842

2943

0 commit comments

Comments
 (0)