You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ctypes.util.find_library does not respect os.add_dll_directory #111104
os.add_dll_directory's documentation states that "This search path is used when resolving dependencies for imported extension modules (the module itself is resolved through sys.path), and also by ctypes.", yet paths added there do not affect ctypes.util.find_library (one can easily verify that the implementation of the latter simply walks PATH:
I do realize that find_library's doc does state "The purpose of the find_library() function is to locate a library in a way similar to what the compiler or runtime loader does (on platforms with several versions of a shared library the most recent should be loaded), while the ctypes library loaders act like when a program is run, and call the runtime loader directly." which might justify the difference in behavior; still, it would be nice if ctypes provided a function (or changed find_library) to return the path of the library that would be loaded while respecting add_dll_directory (right now a workaround would be to call GetModuleFileNameEx ourselves on the handle returned by WinDLL).
Using Python 3.12.6 and Windows 11 64-bit, I am also struggling with ctypes.util.find_library() not finding DLLs in directories added by os.add_dll_directory(), but will locate them if the full path to the DLL is provided to find_library(). Unfortunately this breaks an imported module that uses find_library() with the DLL name to locate a DLL.
Test folder structure:
tester.py:
importosfrompathlibimportPathfromctypes.utilimportfind_library# Add the directory this script is in (also contains vxlapi64.dll)add_obj=os.add_dll_directory(os.path.dirname(__file__))
# Check for library using DLL nameiffind_library("vxlapi64"):
print("vxlapi64.dll from add_dll_directory found")
else:
print("vxlapi64.dll from add_dll_directory NOT FOUND")
# Check for library using DLL pathiffind_library(os.path.dirname(__file__) +"/vxlapi64"):
print("vxlapi64.dll abs path found")
else:
print("vxlapi64.dll abs path NOT FOUND")
Output of python tester.py:
vxlapi64.dll from add_dll_directory NOT FOUND
vxlapi64.dll abs path found
It is now soft-deprecated. It cannot be made to work correctly and consistently on all platforms, and any change runs a chance to break whatever workarounds people already have in place.
I'm prioritizing stability over new features, and encourage solving this outside ctypes itself.
Activity
davidva-ecco commentedon Apr 9, 2025
Did you ever find a solution to this?
Using Python 3.12.6 and Windows 11 64-bit, I am also struggling with
ctypes.util.find_library()not finding DLLs in directories added byos.add_dll_directory(), but will locate them if the full path to the DLL is provided tofind_library(). Unfortunately this breaks an imported module that usesfind_library()with the DLL name to locate a DLL.Test folder structure:
tester.py:
Output of
python tester.py:encukou commentedon Feb 23, 2026
I'm proposing to soft-deprecate
ctypes.util.find_library: https://discuss.python.org/t/106232encukou commentedon Mar 20, 2026
It is now soft-deprecated. It cannot be made to work correctly and consistently on all platforms, and any change runs a chance to break whatever workarounds people already have in place.
I'm prioritizing stability over new features, and encourage solving this outside ctypes itself.