diff --git a/PyInstaller/building/build_main.py b/PyInstaller/building/build_main.py index 24b344d58d..4217e5d1bf 100644 --- a/PyInstaller/building/build_main.py +++ b/PyInstaller/building/build_main.py @@ -159,32 +159,37 @@ def find_binary_dependencies(binaries, binding_redirects, import_packages): if compat.is_win: extra_libdirs.append(compat.base_prefix) - # On Windows with python >= 3.8, attempt to track calls to os.add_dll_directory() to obtain DLL search paths that - # are dynamically set during packages' initialization. - if compat.is_win and compat.is_py38: - added_dll_directories = [] - _orig_add_dll_directory = os.add_dll_directory + # On Windows, packages' initialization code might register additional DLL search paths, either by modifying the + # `PATH` environment variable, or (on pythonn >= 3.8) by calling `os.add_dll_directory`. Therefore, we import + # all collected packages, and track changes made to the environment. + if compat.is_win: + # Track changes made via `os.add_dll_directory`. + if compat.is_py38: + added_dll_directories = [] + _orig_add_dll_directory = os.add_dll_directory - def _pyi_add_dll_directory(path): - added_dll_directories.append(path) - return _orig_add_dll_directory(path) + def _pyi_add_dll_directory(path): + added_dll_directories.append(path) + return _orig_add_dll_directory(path) - os.add_dll_directory = _pyi_add_dll_directory + os.add_dll_directory = _pyi_add_dll_directory - # Import collected packages to set up environment - for package in import_packages: - try: - __import__(package) - except Exception: - pass + # Import collected packages to set up environment. + for package in import_packages: + try: + __import__(package) + except Exception: + pass - # Process extra search paths on Windows - if compat.is_win: + # Process extra search paths... # Directories added via os.add_dll_directory() calls. if compat.is_py38: logger.info("Extra DLL search directories (AddDllDirectory): %r", added_dll_directories) extra_libdirs += added_dll_directories + # Restore original function + os.add_dll_directory = _orig_add_dll_directory + # Directories set via PATH environment variable. # NOTE: PATH might contain empty entries that need to be filtered out. path_directories = [directory for directory in os.environ.get("PATH", '').split(os.pathsep) if directory] diff --git a/news/7698.bugfix.rst b/news/7698.bugfix.rst new file mode 100644 index 0000000000..1ca2e5eec1 --- /dev/null +++ b/news/7698.bugfix.rst @@ -0,0 +1,4 @@ +Limit the import of collected packages prior to performing binary +dependency analysis to only Windows, where it is actually useful. +On non-Windows platforms, there is no benefit to it, and it might +cause issues with particular orders of package imports.