4

How can I call an IronPython function from within Python? Is there an easy way to interface the two. I need the flexibility of both a full set of proper Python libraries that are not available in IronPython and the latest CLR which Python .net does not currently have.

What I've tried so far is to compile a IronPython dll but I can't get it to be loaded properly within Python.

My attempt to make IronPython n callable from Python

foo.py

def foo():
    print 'hello world'

compile_ipy.py

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

My attempt's to call Iron Python From 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
4

1 回答 1

3

.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 库一起使用没有问题。

于 2015-01-06T15:10:36.557 回答