A library that allows you to call DLLs for .NET from Python. It is MIT licensed and anyone can use it free of charge. https://github.com/pythonnet/pythonnet
This article was very helpful for how to use it. How to call .NET from Python and vice versa
Here is a brief introduction.
pip install pythonnet
If you want to use ABCLib.dll in the same directory as test.py.
If it is in another directory, add the path as appropriate. clr.AddReference ('./DEF/ABCLib')
etc.
test.py
import clr
clr.AddReference('ABCLib')
from ABCTools import ABCLib
abc = ABCLib()
clr
seems to be an abbreviation for Common Language Runtime.
Here are some points I stumbled upon when using pythonnet and how to target them.
** Error: System.IO.FileNotFoundException: Unable to find assembly'ABCLib.dll'. **
I get this error if I write the extension'.dll' when doing clr.AddReference
.
Let's delete the'.dll'.
#Example of error
clr.AddReference('ABCLib.dll')
#Example where no error occurs
clr.AddReference('ABCLib')
** Error: System.IO.FileNotFoundException: Unable to find assembly'ABCLib'. ** If the dll is obtained from the outside, access to the file may be blocked. Try setting to allow access. Right-click on the dll file → Properties → General → Check "Allow" at the bottom → OK
** Error: System.IO.FileNotFoundException: Could not load file or assembly'ABCLib, Version = ~~~~~~~~, Culture = neutral, PublicKeyToken = null', or one of its dependencies. The specified file could not be found. ** **
It could not be read unless the DLL and Python bits match.
--How to check Python bits
python
import sys
print(sys.version)
output
3.7.6 (tags/v3.7.6:43364a7ae0, Dec 19 2019, 00:42:30) [MSC v.1916 64 bit (AMD64)]
--How to check the bit of DLL I referred to this article. How to check if EXE or DLL is 32bit or 64bit -Qiita
Recommended Posts