Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import of DLL with pythonnet

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)

like image 999
MJA Avatar asked Apr 24 '18 09:04

MJA


People also ask

Can we import DLL in Python?

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.

Can we use C# DLL in Python?

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.


1 Answers

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)
like image 190
Pavel M. Avatar answered Sep 20 '22 23:09

Pavel M.