c# - Visual Studio 2017 中的 Pythonnet
问题描述
我正在尝试以窗口形式在 Microsoft Visual Studio 2017 社区中使用 Pythonnet 运行 Visbrain,但我在下面遇到了一些问题。
环境
1)Pythonnet版本:v2.3.0
我从 Visual Studio 2017 社区的“管理 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
该应用程序应该可以工作。
推荐阅读
- node.js - NodeJs Bull 用于后台任务
- javascript - jQuery removeClass 和 addClass 不适用于特定的 CSS 选择器
- android - 在我的应用中实现深色主题;不同活动的工具栏中显示不同的颜色
- python-3.x - 访问 DEAP 中的多个统计信息
- r - 如何为R中地图中的不同区域选择不同的主题
- django - 在 Django 中启动两个 http 服务器
- android - 更改下一个活动后应用程序崩溃问题?
- android - setTextAppearance - 尝试禁用单击时不适用于 MaterialButton
- mysql - 为什么 MariaDB 10.1.38 不创建表空间?
- python - 如果我想基于列来处理数据帧,我可以在函数 read_csv 中使用函数“排序依据”吗?