I use a proprietary Python package. Within this Python package the following DLL load command fails.
scripting_api = ctypes.CDLL("scripting_api_interface")
Could not find module 'scripting_api_interface' (or one of its dependencies). Try using the full path with constructor syntax.
I know the path to the DLL scripting_api-interface.dll and added within my Python code the following DLL path.
os.environ['PATH'] = 'L:\win64' + os.pathsep
But still loading the DLL will fail. I created a test environment where I used the following command.
scripting_api = ctypes.CDLL("L:\win64\scripting_api_interface.dll")
Which works as expected. But I can't change the DLL call, because it is provided by the mentioned Python package. Are there any other options to get this running?
Call CDLL
with the version that works before importing the package. Once the DLL is loaded, additional loads are ignored.
Example (Win11 x64)...
test.c (simple DLL source, MSVC: cl /LD test.c):
__declspec(dllexport)
void func() {}
With test.dll in the current directory, loading will fail without an explicit path for DLLs not in the "standard" search path. The current directory is not part of the search path for security reasons.
>>> import ctypes as ct
>>> dll = ct.CDLL('test')
Traceback (most recent call last):
File "<python-input-1>", line 1, in <module>
dll = ct.CDLL('test')
File "C:\dev\Python313\Lib\ctypes\__init__.py", line 390, in __init__
self._handle = _dlopen(self._name, mode)
~~~~~~~^^^^^^^^^^^^^^^^^^
FileNotFoundError: Could not find module 'test' (or one of its dependencies). Try using the full path with constructor syntax.
>>> dll = ct.CDLL('./test') # explicit relative path works
>>> dll = ct.CDLL('test') # now without path works because already loaded
>>>
So in your case the following should work:
import ctypes as ct
ct.CDLL('L:/win64/scripting_api_interface.dll')
import your_package