c# - Visual Studio 2017 中的 Pythonnet

标签 c# python .net visual-studio python.net

我尝试在 Microsoft Visual Studio 2017 社区中以窗口形式使用 Pythonnet 运行 Visbrain,但我遇到了以下问题。

环境

1) Pythonnet 版本:v2.3.0

我从 Visual Studio 2017 Community 的“Manage Nuget-Package”下载了这个。

2) Python 版本:Python 3.6.5 (Amaconda 3)

环境中已经pip install pythonnet

3) 操作系统:Window 7,64 位

4) .net 框架:4.0

===

尝试运行以下代码:

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

调试时

来自 program.cs

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

错误信息:

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.

堆栈跟踪:

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

想请教一下,这是什么问题,如何解决。谢谢。

最佳答案

检查您对“Python.Runtime”的引用

它可能指向 32 位版本,而您正在使用 64 位版本

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

改成

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

并且该应用程序应该可以运行。

关于c# - Visual Studio 2017 中的 Pythonnet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51845263/

相关文章:

c# - SqlConnection 类中的默认网络协议(protocol)是什么

c# - 如何在.net core中执行api版本控制?

c# - 如何在没有 Visual Studio 的情况下使用自定义脚本添加/更新引用?

python - 具有特定格式的子字符串提取

python - 如何在应用程序退出时正确终止线程

python - Gspread 访问 google 电子表格 : HttpAccessTokenRefreshError, 无效的 JWT

.net - 如何更改 SQL 中的列属性?

c# - 服务器客户端发送/接收多个客户端

c# - 从ViewModel(MVVM)添加到XAML WPF

c# - 需要从 Xamarin 中的选取器中选择至少一项