5

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.

2
  • 2
    You are probably calling from 32-bit .NET process, but your Python 3.6 is 64-bit. Try to run nPython.exe from pythonnet solution. Commented Jun 7, 2017 at 15:53
  • @denfromufa Thank you! I've just forgot to change Platform target in my project from Any CPU to x64. Commented Jun 7, 2017 at 16:11

1 Answer 1

7

As denfromufa said, I was just calling from a 32-bit .NET process.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.