4

I have some code that is supposed to load a .dll. I'm using Python version 3.0.17114.1 with Visual Studio 2017 Preview. Here is a code snip:

from ctypes.util import find_library
theDll = find_library('DsiLibrary_dll')

theDll comes out "None" which means it can't be found.

Note that a google search said that I should not add .dll:

#from "https://docs.python.org/3/library/ctypes.html"
#name is the library name without any prefix like lib, suffix like .so, .dylib

I checked SO and in one example a guy did use .dll. So I have tried adding .dll but no help. Then I also tried using cdll.find_library but that doesn't work either.

So I added this to be sure the .dll is there:

print ("Current directory:")
os.system("cd")
os.system("dir /s/b DsiLibrary_dll.dll")

The output is this:

Current directory:
L:\DSI (His) Try 5\Windows Apps\PythonSample
L:\DSI (His) Try 5\Windows Apps\PythonSample\DsiLibrary_dll.dll

Which shows that the dll is there.

I'm new at Python but I'm hoping someone can help here.

Eddy
  • 379
  • 1
  • 5
  • 16
  • Is your DLL in a directory that is in %PATH% ? – boardrider May 14 '17 at 12:59
  • It was before I started VS but when I printed the path using os.system("path") it wasn't. Here is a snip of the path before VS: "C:\Program Files (x86)\Common Files\Acronis\TibMounter64;L:\DSI (His) Try 5\Windows Apps\PythonSample". Here is a snip of the path printed from within Python: "C:\Program Files (x86)\Common Files\Acronis\TibMounter64". – Eddy May 14 '17 at 13:35
  • So, solve your `path` problem, and you'd probably solve your OP. – boardrider May 14 '17 at 13:37
  • Shouldn't Python look in the current directory? – Eddy May 14 '17 at 13:45
  • What would I try which is different than I have already? Shouldn't Python look in the current directory? Note that I set the path using System/Environment Variables. Then re-executed VS to be sure it would pick it up. – Eddy May 14 '17 at 13:57
  • So I add this but it didn't help: os.system("path = %path%;."). I think os.system causes a new process which sets the path but upon the process exit it goes back to the Python process and the additional information is lost. – Eddy May 14 '17 at 14:20
  • Solved ... I had added the path to the System Variables. I have now changed it such that it has been added to the User Variables. But I would like the Python program to be independent of the path. Is there a way? – Eddy May 14 '17 at 14:38
  • You might be able to add the path via `sys.path.append("")`. That only works for your current process but might suffice if the path is relatively static/known? (Unsure) Else, the `find_library` function searches the "system search path" according to the docs. So the call would only work if the DLL is on such a path else it is better to supply the full path at 'compile time' rather than search at 'run time'. – E. Körner Apr 12 '20 at 19:10

0 Answers0