我正在尝试从 python 调用 c# 函数。我使用的代码如下:
def solve_it(input_data):
assignment1Object = Assignment1()
return assignment1Object.Assignment1Solution()
from pythonnet import load
load('netfx')
import clr
clr.AddReference('path/to/dll/DiscreteOptimizationSolutions.dll')
from DiscreteOptimizationSolutions import Assignment1
result = solve_it()
print('This script submits the integer: %s\n' % solve_it(''))
但它会抛出错误:
RuntimeError: Failed to initialize Python.Runtime.dll
Failed to initialize pythonnet:
System.InvalidOperationException: This property must be set before runtime is initialized
at Python.Runtime.Runtime.set_PythonDLL(String value)
at Python.Runtime.Loader.Initialize(IntPtr data, Int32 size)
at Python.Runtime.Runtime.set_PythonDLL(String value)
at Python.Runtime.Loader.Initialize(IntPtr data, Int32 size)
我在 Anaconda 上使用 python 版本 3.10.9 和 pythonnet 3.0.1