4
votes

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
1
Can you give a simple example? - Peter Wood
@PeterWood Let's start with calling a function that print's hello world in IronPython from Python, I'll include whati've tried as well. - pyCthon

1 Answers

3
votes

.NET DLLs are not the same as C DLLs (which you can use via ctypes). You can, however, use the Python.NET library to call the function like this:

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

I don't know what you mean by "latest CLR which Python .net does not currently have". You can use the version provided here: http://www.lfd.uci.edu/~gohlke/pythonlibs/#pythonnet if you need to use CPython 3, I'm using it with .NET 4.5.1 libraries without issues.