在Pythonnet中运行自述文件

时间:2018-08-15 09:16:33

标签: c# python.net

环境:

  • Pythonnet版本:2.3.0,已在Amaconda3中随pip一起安装
  • Python版本:3.6.6与Anaconda一起使用
  • Visual Studio 2017社区
  • 操作系统:Windows 7,64位

我正在从Pythonnet Wiki运行此示例。

using (Py.GIL())
{
                dynamic np = Py.Import("numpy");
                Console.WriteLine(np.cos(np.pi * 2));

                dynamic sin = np.sin;
                Console.WriteLine(sin(5));

                double c = np.cos(5) + sin(5);
                Console.WriteLine(c);

                dynamic a = np.array(new List<float> { 1, 2, 3 });
                Console.WriteLine(a.dtype);

                dynamic b = np.array(new List<float> { 6, 5, 4 }, dtype: np.int32);
                Console.WriteLine(b.dtype);

                Console.WriteLine(a * b);
                Console.ReadKey();
}

结果

1.0
-0.9589242746631385
-0.675262089199912
object

此处发生错误:

dynamic b = np.array(new List { 6, 5, 4 }, dtype: np.int32);

错误消息:

Python.Runtime.PythonException: 'TypeError : int() argument must be a string, a bytes-like object or a number, not '0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]''

堆栈跟踪:

Python.Runtime.PythonException
  HResult=0x80131500
  Message=TypeError : int() argument must be a string, a bytes-like object or a number, not '0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]'
  Source=Python.Runtime
  StackTrace:

尝试解决方案但未成功:

1)https://github.com/pythonnet/pythonnet/issues/249

2)从master下载zip文件并从Amaconda提示符运行

python setup.py bdist_wheel --xplat
pip install dist\pythonnet-2.4.0.dev0-cp36-cp36m-win64.whl

python setup.py bdist_wheel失败,因为需要导入错误:找不到mt.exe 安装Visual Studio 2017社区时没有重启我的电脑。 因此,我认为Visual Studio 2017安装程序没有为我安装mt.exe。 重新启动PC对我来说不是一个选择。

===

想知道有什么办法可以解决这个问题,谢谢。

1 个答案:

答案 0 :(得分:1)

根据Pythonnet的开发人员,我试图 安装pythonnet的主人。

安装完成后,我成功运行了ReadMe。