如何从 Python 中调用 IronPython 函数?有没有一种简单的方法来连接两者。我需要 IronPython 中不可用的一整套适当的 Python 库和 Python .net 目前没有的最新 CLR 的灵 active 。
到目前为止,我尝试过编译一个 IronPython dll,但我无法在 Python 中正确加载它。
我试图让 IronPython n 可以从 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()
我不知道“Python .net 目前没有的最新 CLR”是什么意思。您可以使用此处提供的版本:http://www.lfd.uci.edu/~gohlke/pythonlibs/#pythonnet如果您需要使用 CPython 3,我将它与 .NET 4.5.1 库一起使用,没有任何问题。
关于python - 从标准 Python 调用 IronPython,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27800327/