Precautions and error handling when calling .NET DLL from python using pythonnet

pythonnet --What is Python.NET?

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

How to use

This article was very helpful for how to use it. How to call .NET from Python and vice versa

Here is a brief introduction.

Installation method

pip install pythonnet

Example of use

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.

Precautions and what to do when an error occurs

Here are some points I stumbled upon when using pythonnet and how to target them.

Do not write .dll

** 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')

Allow security

** 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

image.png

Match 64bit / 32bit with DLL and Python

** 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

Precautions and error handling when calling .NET DLL from python using pythonnet
Precautions when using phantomjs from python
Precautions when using pit in Python
Precautions when using codecs and pandas
Precautions when using six with Python 2.5
Error notification method when calling python from windows GUI or winsound.Beep
From Python to using MeCab (and CaboCha)
How to deal with OAuth2 error when using Google APIs from Python
Precautions when using OpenCV from Power Automate Desktop
Python Error Handling
Error that occurred in OpenCV3 and its solution Precautions when using OpenCV3 on Mac
I tried to execute Python code from .Net using Pythonnet (Hallo World edition)
Access order of multidimensional arrays when calling Fortran subroutines from Python (ctypeslib and f2py)
Error due to conflict between python when using gurobi
Tips and precautions when porting MATLAB programs to Python
Installation method when using RealSense from Python (pyenv edition)
Error when executing Python commands without using Anaconda Prompt
Get files from Linux using paramiko and scp [Python]
Precautions when handling Luigi
Precautions when using Chainer
Precautions when inputting from CSV with Python and outputting to json to make it an exe
Convert from Pandas DataFrame to System.Data.DataTable using Python for .NET
Visualize plant activity from space using satellite data and Python
Predict gender from name using Gender API and Pykakasi in Python
[Python] Random data extraction / combination from DataFrame using random and pandas
Shoot time-lapse from a PC camera using Python and OpenCV
Flatten using Python yield from
Error handling when installing mecab-python
Error when playing with python
When using if and when using while
How to get followers and followers from python using the Mastodon API
Error and solution when installing python3 with homebrew on mac (catalina 10.15)
Precautions when passing def to sorted and groupby functions in Python? ??
Error log output method when using Bottle framework on GAE / Python