Import of DLL with pythonnet

13,810

Solution 1

This is how it works for me. Dll sits in '/SDK/dll/some_net64.dll' Note: no .dll extension needed.

import os, sys, clr
dll_dir = './SDK/dll/'
dllname = 'some_net64'
path = r'%s%s' % (dll_dir, dllname)
sys.path.append(os.getcwd())
clr.AddReference(path)

Solution 2

clr.AddReference() is bad at describing the error. A better way to find out why the import fails is to use this.

#use this section of code for troubleshooting
from clr import System
from System import Reflection
full_filename = r'C:\foo\bar\MyDll.dll'
Reflection.Assembly.LoadFile(full_filename)   #this elaborate the error in details

One possibility is that the system knows that your DLL is downloaded from somewhere else (even Dropbox sync counts) and does not allow you to use that DLL file. In that case, you can download a tool from https://docs.microsoft.com/en-us/sysinternals/downloads/streams and run this command to remove all those flags from the DLL file.

stream -d MyDll.dll

After that, the import with clr.AddReference() should work.

Solution 3

In python strings "\" is an escape character. To have truly a backslash character in a python string, you need to add a second one: "\\".

Change sys.path.append('C:\PathToDllFolder') to sys.path.append('C:\\PathToDllFolder').

I am not sure about clr.AddReference('MyDll.dll'), the version without .dll should work: clr.AddReference('MyDll')

Share:
13,810
MJA
Author by

MJA

Updated on June 11, 2022

Comments

  • MJA
    MJA about 2 years

    I am trying to import and use a DLL in python. Therefore I am using pythonnet.

    import sys
    import clr
    
    sys.path.append('C:\PathToDllFolder')
    
    clr.AddReference('MyDll.dll')
    

    However the code yields the following error:

    Traceback (most recent call last):
      File "E:\NET\NET_test.py", line 6, in <module>
        clr.AddReference('MyDll.dll')
    System.IO.FileNotFoundException: Unable to find assembly 'MyDll.dll'.
       bei Python.Runtime.CLRModule.AddReference(String name)
    

    Target runtime of the DLL is: v4.0.30319

    Is there any way to find out why the import is failing and how i can fix it?

    (If necessary i can also provide the DLL)

    • Tagc
      Tagc about 6 years
      Does it make a difference if you omit the sys.path.append call and use the full path to the DLL directly within clr.AddReference?
    • Austin
      Austin about 6 years
      Can you try without extension .dll?
    • MJA
      MJA about 6 years
      Yes, somehow it works like that (by adding the full path)
    • Joe
      Joe about 6 years
      You can find some more info on this in my answer stackoverflow.com/a/50003112/7919597 You can use dll_ref = System.Reflection.Assembly.LoadFile(fullPath)
  • Leonardo
    Leonardo about 3 years
    Please fix your path on Windows" clr.AddReference(r'C:\PathToDllFolder\MyDll.dll') or clr.AddReference('C:\\PathToDllFolder\\MyDll.dll').
  • ishahak
    ishahak almost 3 years
    Genius! how could you know that? this 'streams' trick did the work for me! thank you so much!