从标准 Python 调用 IronPython

Call IronPython from standard Python

如何从 Python 中调用 IronPython 函数?有没有一种简单的方法来连接两者。我需要 IronPython 中不可用的一整套适当 Python 库和 Python .net 目前没有的最新 CLR 的灵活性。

到目前为止,我尝试编译一个 IronPython dll,但我无法在 Python.

中正确加载它

我尝试让 IronPython 可以从 Python

调用

foo.py

def foo():
    print 'hello world'

compile_ipy.py

import clr
clr.CompileModules("foo.dll", "foo.py")

我尝试从 Python

呼叫 Iron Python

call_ipy_from_py1.py

import ctypes
dll = ctypes.WindDLL('foo.dll')
dll.foo() # AttributeError: function 'foo' not found

call_ipy_from_py2.py

import ctypes
dll = ctypes.cdll.LoadLibrary('foo.dll')
dll.foo() # AttributeError: function 'foo' not found

.NET DLL 与 C DLL(您可以通过 ctypes 使用)不同。但是,您可以使用 Python.NET 库来调用函数,如下所示:

import clr # Imports Python.Runtime.dll
import foo # Imports foo.dll
foo.foo()

我不知道你所说的 "latest CLR which Python .net does not currently have" 是什么意思。您可以使用此处提供的版本:http://www.lfd.uci.edu/~gohlke/pythonlibs/#pythonnet 如果您需要使用 CPython 3,我将它与 .NET 4.5.1 库一起使用没有问题。