从标准 Python 调用 IronPython

pyC*_*hon 5 .net python ironpython interop python.net

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

到目前为止,我尝试过编译 IronPython dll,但无法在 Python 中正确加载它。

我尝试让 IronPython n 可从 Python 调用

foo.py

def foo():
    print 'hello world'
Run Code Online (Sandbox Code Playgroud)

编译ipy.py

import clr
clr.CompileModules("foo.dll", "foo.py")
Run Code Online (Sandbox Code Playgroud)

我尝试从 Python 调用 Iron Python

call_ipy_from_py1.py

import ctypes
dll = ctypes.WindDLL('foo.dll')
dll.foo() # AttributeError: function 'foo' not found
Run Code Online (Sandbox Code Playgroud)

call_ipy_from_py2.py

import ctypes
dll = ctypes.cdll.LoadLibrary('foo.dll')
dll.foo() # AttributeError: function 'foo' not found
Run Code Online (Sandbox Code Playgroud)

fil*_*mor 3

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

import clr # Imports Python.Runtime.dll
import foo # Imports foo.dll
foo.foo()
Run Code Online (Sandbox Code Playgroud)

我不知道你所说的“Python .net 目前没有的最新 CLR”是什么意思。如果您需要使用 CPython 3,您可以使用此处提供的版本:http://www.lfd.uci.edu/~gohlke/pythonlibs/#pythonnet ,我将其与 .NET 4.5.1 库一起使用,没有任何问题。

  • 啊,我现在可以正常工作了,它与我的 Python 根目录中的另一个 clr.pyd 冲突 (2认同)