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)
Yes, it requires that you write some C++ code and have a C++ compiler, but you don't actually need to learn C++ to use it, and you can get a free (as in beer) C++ compiler from Microsoft. Save this answer. Show activity on this post. If the DLL is of type COM library, then you can use pythonnet.
To import C# DLL files to Python, install Python.NET so that Python can communicate with . NET Common Language Runtime. The package can be installed via “pip install pythonnet” or from their github page: https://github.com/pythonnet/pythonnet.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With