我试图在我的.NET应用程序中嵌入Python。Python for .NET:嵌入时出现System.BadImageFormatException
我在Windows 10上使用Python 3.6.1 64位,我已经使用pip从PyPi安装pythonnet 2.3.0。
我引用来自C:\Program Files\Python36\Lib\site-packages
Python.Runtime.dll
和既有Path
和PYTHONHOME
环境变量指向C:\Program Files\Python36\
。
当我在Python中使用.NET时,一切正常。
import clr
from System.Drawing import Point
p = Point(5, 5)
print(p)
打印{X=5,Y=5}
如预期。
但是当我从.NET调用Python时,我得到了System.BadImageFormatException
。例如,调用时Python.Runtime.PythonEngine.Initialize();
:
System.BadImageFormatException:“无法加载文件或组件 'Python.Runtime,版本= 2.3.0.0,文化=中性 公钥=空' 或它的一个依赖。试图用 加载一个格式不正确的程序。'
有什么想法?
在此先感谢。
您可能是从32位.NET进程调用的,但您的Python 3.6是64位的。尝试从pythonnet解决方案运行nPython.exe。 – denfromufa
https://github.com/pythonnet/pythonnet/wiki/Troubleshooting-on-Windows-and-Linux – denfromufa
@denfromufa谢谢!我只是忘了将我的项目中的'Platform target'从'Any CPU'更改为'x64'。 – DfM