From 9e6e4e8d21758539b3a9c1aef763d310f91963f1 Mon Sep 17 00:00:00 2001 From: Shyamnath Premnadh Date: Wed, 3 Jan 2024 10:09:44 +0100 Subject: Fix pyside6-designer crash with pyenv on Unix - With pyenv installed Python, we need to explicitly add the full Python library path to LD_PRELOAD(Linux) and DYLD_INSERT_LIBRARIES(macOS). Otherwise, these libraries are searched in the default search paths of ld and dyld, and it won't be able to find them. - for macOS, the python installed with pyenv won't be a framework build unless explicitly specified. Thus we use the same approach for linux by looking for the Python library inside the path in `LIBDIR`. In the case of a framework build with pyenv, 'PYTHONFRAMEWORKPREFIX' will not be empty and hence no explcit changes are required. Fixes: PYSIDE-2568 Pick-to: 6.2 6.5 6.6 Change-Id: I94815b721acb85fa0b7f28d1bbb00a49717120c3 Reviewed-by: Friedemann Kleint Reviewed-by: Cristian Maureira-Fredes --- sources/pyside-tools/pyside_tool.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) (limited to 'sources/pyside-tools') diff --git a/sources/pyside-tools/pyside_tool.py b/sources/pyside-tools/pyside_tool.py index 280c9ec3b..84bd61986 100644 --- a/sources/pyside-tools/pyside_tool.py +++ b/sources/pyside-tools/pyside_tool.py @@ -15,6 +15,17 @@ import PySide6 as ref_mod VIRTUAL_ENV = "VIRTUAL_ENV" +def is_pyenv_python(): + pyenv_root = os.environ.get("PYENV_ROOT") + + if pyenv_root: + resolved_exe = Path(sys.executable).resolve() + if str(resolved_exe).startswith(pyenv_root): + return True + + return False + + def is_virtual_env(): return sys.prefix != sys.base_prefix @@ -147,11 +158,23 @@ def designer(): # Determine library name (examples/utils/pyside_config.py) version = f'{major_version}.{minor_version}' library_name = f'libpython{version}{sys.abiflags}.so' + if is_pyenv_python(): + library_name = str(Path(sysconfig.get_config_var('LIBDIR')) / library_name) os.environ['LD_PRELOAD'] = library_name elif sys.platform == 'darwin': library_name = sysconfig.get_config_var("LDLIBRARY") framework_prefix = sysconfig.get_config_var("PYTHONFRAMEWORKPREFIX") - lib_path = os.fspath(Path(framework_prefix) / library_name) + lib_path = None + if framework_prefix: + lib_path = os.fspath(Path(framework_prefix) / library_name) + elif is_pyenv_python(): + lib_path = str(Path(sysconfig.get_config_var('LIBDIR')) / library_name) + else: + # ideally this should never be reached because the system Python and Python installed + # from python.org are all framework builds + print("Unable to find Python library directory. Use a framework build of Python.", + file=sys.stderr) + sys.exit(0) os.environ['DYLD_INSERT_LIBRARIES'] = lib_path elif sys.platform == 'win32': # Find Python DLLs from the base installation -- cgit v1.2.3