From d525216101b8a18d0228943aaff239563dae46f2 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Tue, 30 Oct 2018 13:11:55 -0700 Subject: [PATCH] handle $PATH not being set in python library Fixes #3844 --- python-package/xgboost/core.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/python-package/xgboost/core.py b/python-package/xgboost/core.py index 5494b49a9993..62eec627046b 100644 --- a/python-package/xgboost/core.py +++ b/python-package/xgboost/core.py @@ -117,18 +117,23 @@ def _load_lib(): lib_paths = find_lib_path() if len(lib_paths) == 0: return None - pathBackup = os.environ['PATH'] + try: + pathBackup = os.environ['PATH'].split(os.pathsep) + except KeyError: + pathBackup = [] lib_success = False os_error_list = [] for lib_path in lib_paths: try: # needed when the lib is linked with non-system-available dependencies - os.environ['PATH'] = pathBackup + os.pathsep + os.path.dirname(lib_path) + os.environ['PATH'] = os.pathsep.join(pathBackup + [os.path.dirname(lib_path)]) lib = ctypes.cdll.LoadLibrary(lib_path) lib_success = True except OSError as e: os_error_list.append(str(e)) continue + finally: + os.environ['PATH'] = os.pathsep.join(pathBackup) if not lib_success: libname = os.path.basename(lib_paths[0]) raise XGBoostError(