2

我正在尝试使用 pythonnet 使用来自 C# 的嵌入式 python 解释器(在https://github.com/renshawbay/pythonnet找到的 python3 兼容版本)

我的解释器位于 D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime 并具有来自 python 发行版的“Lib”和“Libs”文件夹。

我已经使用以下代码进行了测试:

   <!-- language: c# -->
   PythonEngine.PythonHome = @"D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime";
   PythonEngine.ProgramName = "PythonRuntime";
   PythonEngine.Initialize();
   using (Py.GIL())
   {
      PythonEngine.RunSimpleString("print(1)");
   }

但是,它不起作用。我得到一个“SystemError:PyEvalCodeEx:NULL globals”。每次我尝试从 python 获取对象时,代码都会失败。

我究竟做错了什么?

4

1 回答 1

1

我想我找到了答案。如果我添加对 pythonnet 提供的“clr”模块的引用,它确实有效

PythonEngine.PythonHome = @"D:\src\scratch\TestPythonNet\TestPythonNet\PythonRuntime";
PythonEngine.ProgramName = "PythonRuntime";
PythonEngine.Initialize();
// ==>
PyObject a = PythonEngine.ImportModule("clr");
using (Py.GIL())
{
    PythonEngine.RunSimpleString("print(1)");
}
于 2014-08-13T16:36:34.443 回答