Discussion:
Python - C# interoperability
"Martin v. Löwis"
2007-01-09 22:46:41 UTC
Permalink
Is there an easy way to compile a Python class (or set of classes) into
a .DLL that a C# program can call? Or otherwise to use an existing
library of Python classes from a C# program as seamlessly as possible?
You should take a look at IronPython, which supports that kind of thing.

Regards,
Martin
Luis M. González
2007-01-10 03:50:57 UTC
Permalink
Is there an easy way to compile a Python class (or set of classes) into
a .DLL that a C# program can call? Or otherwise to use an existing
library of Python classes from a C# program as seamlessly as possible?
I'm affraid this is not possible.
Ironpython (the .NET python implementation) can consume assemblies
written in other statically typed languages, such as c#, but not the
other way around.
This is because Ironpython is still a dynamic language, and the lack of
type information makes it impossible to be compiled as c#.

For the time being, if you really need to write reusable assemblies
that could be consumed from other .NET languages, you shouldn't use
Ironpython.
You should use c#, vb.net or any other static language implementation
for .NET.

If you want a more "pythonic" alternative, you could use Boo (
http://boo.codehaus.org ).
It's a static language with a python-like syntax, and it's very easy to
pick up if you already know python.

For consuming Boo assemblies from Ironpython, you should compile them
as .DLL, and place them into a "DLLs" folder in your Ironpython root
directory (where ipy.exe is located).
Then you simply import them as you would with any other python module.
Make sure to add also the boo assembly. If you are using c# instead,
you don't have to add anything else.

Hope this helps,
Luis
mc
2007-01-10 21:39:10 UTC
Permalink
Thanks to all who responded. It appears that there may be a solution
as follows:

use jythonc to turn Python program into Java bytecode

use Microsoft's jbimp to turn Java bytecode into .NET DLL

It sounds roundabout, but I am investigating.
Larry Bates
2007-01-11 18:13:09 UTC
Permalink
Is there an easy way to compile a Python class (or set of classes) into
a .DLL that a C# program can call? Or otherwise to use an existing
library of Python classes from a C# program as seamlessly as possible?
You can write COM objects that can be called from C# (or basically ANY
other language that can call COM objects) quite easily and compile them
with py2exe for distribution without python itself.

-Larry Bates
mc
2007-01-09 21:30:01 UTC
Permalink
Is there an easy way to compile a Python class (or set of classes) into
a .DLL that a C# program can call? Or otherwise to use an existing
library of Python classes from a C# program as seamlessly as possible?
sturlamolden
2007-01-10 22:59:07 UTC
Permalink
Is there an easy way to compile a Python class (or set of classes) into
a .DLL that a C# program can call? Or otherwise to use an existing
library of Python classes from a C# program as seamlessly as possible?
One way is to use IronPython if you don't need modules written for
CPython.

Another option is to use a COM wrapper, e.g. using win32com in Python.

A third option is to embed a Python interpreter in your C# app.
Bruno Desthuilliers
2007-01-09 23:14:48 UTC
Permalink
Is there an easy way to compile a Python class (or set of classes) into
a .DLL that a C# program can call? Or otherwise to use an existing
library of Python classes from a C# program as seamlessly as possible?
I can't tell if that'll do, but have you looked at IronPython ?

Loading...