Import of DLL with pythonnet
Asked Answered
P

4

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)

Padgett answered 24/4, 2018 at 9:40 Comment(4)
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?Uppercase
Can you try without extension .dll?Limpkin
Yes, somehow it works like that (by adding the full path)Padgett
You can find some more info on this in my answer https://mcmap.net/q/587224/-python-for-net-how-to-explicitly-create-instances-of-c-classes-using-different-versions-of-the-same-dll You can use dll_ref = System.Reflection.Assembly.LoadFile(fullPath)Lachus
R
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)
Robertaroberto answered 26/7, 2018 at 1:52 Comment(0)
T
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.

Triparted answered 10/7, 2020 at 14:40 Comment(1)
Genius! how could you know that? this 'streams' trick did the work for me! thank you so much!Plainsman
B
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')

Belmonte answered 16/5, 2018 at 15:59 Comment(0)
G
0

use absolute path to your dll

import clr
clr.AddReference(r'C:\PathToDllFolder\MyDll.dll')
Garibull answered 14/5, 2020 at 3:52 Comment(2)
Please fix your path on Windows" clr.AddReference(r'C:\PathToDllFolder\MyDll.dll') or clr.AddReference('C:\\PathToDllFolder\\MyDll.dll').Sugden
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' )Varioloid

© 2022 - 2024 — McMap. All rights reserved.