Pythonnet in Visual Studio 2017

3

I am trying to run Visbrain using Pythonnet in Microsoft Visual Studio 2017 Community in a window form, but i faced some problem below.

Environment

1) Pythonnet version : v2.3.0

i downloaded this from "Manage Nuget-Package" in Visual Studio 2017 Community.

2) Python version : Python 3.6.5 (Amaconda 3)

Already pip install pythonnet in the environment.

3) Operating System : Window 7, 64 bit

4) .net framework : 4.0

===

trying to run below code :

private static void button_Click(object sender, EventArgs e)
{
    using (Py.GIL())   
    {
         dynamic myVisbrain = Py.Import("Visbrain"); 
    }
}

when debug

from program.cs

Application.Run(new Form1()); // crashes here

error message :

Exception thrown: 'System.BadImageFormatException' in System.Windows.Forms.dll
An unhandled exception of type 'System.BadImageFormatException' occurred in System.Windows.Forms.dll
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.

stack trace :

System.BadImageFormatException
  HResult=0x8007000B
  Message=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.
  Source=WindowsFormsAppJTTH
  StackTrace:
   at WindowsFormsAppJTTH.Form1.button1_Click(Object sender, EventArgs e) in C:\Users\Administrator\source\repos\WindowsFormsAppJTTH\WindowsFormsAppJTTH\Form1.cs:line 219
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at WindowsFormsAppJTTH.Program.Main() in C:\Users\Administrator\source\repos\WindowsFormsAppJTTH\WindowsFormsAppJTTH\Program.cs:line 19

Would like to ask, what is the issue here and how to solve it. Thank You.

c#
python
.net
visual-studio
python.net
asked on Stack Overflow Aug 14, 2018 by STREETKILLER007 • edited Aug 14, 2018 by denfromufa

1 Answer

1

Check your references for "Python.Runtime"

It's probably pointing to the 32bit version and you're on a 64bit build

\packages\pythonnet_py27_dotnet.2.3.0\lib\net40\x86\Python.Runtime.dll

Change it to

\packages\pythonnet_py27_dotnet.2.3.0\lib\net40\x64\Python.Runtime.dll

And the app should work.

answered on Stack Overflow Sep 16, 2019 by Sameer Alibhai

User contributions licensed under CC BY-SA 3.0