7

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)

denfromufa
  • 5,610
  • 13
  • 81
  • 138
MJA
  • 357
  • 2
  • 5
  • 10
  • 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`? – Tagc Apr 24 '18 at 09:42
  • Can you try without extension `.dll`? – Austin Apr 24 '18 at 09:47
  • Yes, somehow it works like that (by adding the full path) – MJA Apr 24 '18 at 09:50
  • 1
    You can find some more info on this in my answer https://stackoverflow.com/a/50003112/7919597 You can use `dll_ref = System.Reflection.Assembly.LoadFile(fullPath)` – Joe Apr 26 '18 at 07:17

4 Answers4

5

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)
Pavel M.
  • 610
  • 1
  • 8
  • 24
5

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://learn.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.

koonyook
  • 159
  • 1
  • 8
2

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')

mdk
  • 398
  • 2
  • 8
0

use absolute path to your dll

import clr
clr.AddReference('C:\PathToDllFolder\MyDll.dll')
willhyper
  • 96
  • 2
  • 5
  • Please fix your path on Windows" `clr.AddReference(r'C:\PathToDllFolder\MyDll.dll')` or `clr.AddReference('C:\\PathToDllFolder\\MyDll.dll')`. – Leonardo May 25 '21 at 15:08
  • Or another (and better IMHO) way is to prefix path strings with "r" (meaning raw). This way you don't have to do things like double backslashes. In this example, it would be clr.AddReference( r'C:\PathToDllFolder\MyDll.dll' ) – batpox Jun 26 '22 at 12:43