I'm trying to embed Python in my .NET application.
I'm using Python 3.6.1 64-bit on Windows 10 and I have installed pythonnet 2.3.0 from PyPi using pip.
I've referenced Python.Runtime.dll
from C:\Program Files\Python36\Lib\site-packages
and have both Path
and PYTHONHOME
environment variables pointed to C:\Program Files\Python36\
.
When I use .NET in Python everything is OK.
import clr
from System.Drawing import Point
p = Point(5, 5)
print(p)
prints {X=5,Y=5}
as expected.
But when I call Python from .NET I get System.BadImageFormatException
. For example, when calling Python.Runtime.PythonEngine.Initialize();
:
System.BadImageFormatException: 'Could not load file or assembly 'Python.Runtime, Version=2.3.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was made to load a program with an incorrect format.'
Any thoughts?
Thanks in advance.
Solution 1 Usually this is related to the difference in 64bit and 32bit DLL builds and processes. Try compiling to 32bit by changing project properties/Build/Platform Target .
This exception is thrown when the file format of a dynamic link library (. dll file) or an executable (.exe file) doesn't conform to the format that the common language runtime expects.
As denfromufa said, I was just calling from a 32-bit .NET process.
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