Import of DLL with pythonnet

MJA picture MJA · Apr 24, 2018 · Viewed 9.2k times · Source

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)

Answer

Pavel M. picture Pavel M. · Jul 26, 2018

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)