From a393d44c5d9a68de01e74463b4a9f5c2f1e3b3db Mon Sep 17 00:00:00 2001 From: KOLANICH Date: Fri, 20 Jul 2018 23:03:44 +0000 Subject: [PATCH] Improved library loading a bit (#3481) * Improved library loading a bit * Fixed indentation. * Fixes according to the discussion * Moved the comment to a separate line. * specified exception type --- python-package/xgboost/core.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/python-package/xgboost/core.py b/python-package/xgboost/core.py index d35533293..f99df9216 100644 --- a/python-package/xgboost/core.py +++ b/python-package/xgboost/core.py @@ -114,10 +114,17 @@ def _get_log_callback_func(): def _load_lib(): """Load xgboost Library.""" - lib_path = find_lib_path() - if len(lib_path) == 0: + lib_paths = find_lib_path() + if len(lib_paths) == 0: return None - lib = ctypes.cdll.LoadLibrary(lib_path[0]) + pathBackup = os.environ['PATH'] + 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) + lib = ctypes.cdll.LoadLibrary(lib_path) + except OSError: + continue lib.XGBGetLastError.restype = ctypes.c_char_p lib.callback = _get_log_callback_func() if lib.XGBRegisterLogCallback(lib.callback) != 0: