Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# .NET interoperabillity with managed Python (CPython) -> any problems?

I am working on a design of an application. The Core should be written in C# but i also want to use some already finished CPython modules (un-managed).

So I am interested in the interoperability (Call CPython method from C# and Call C# from CPython). And if there are problems, because C# runs within the .NET runtime (managed) and CPython directly un-managed.

I already investigated this issue with Google and came out to these solutions:

  • Use IronPython via DLR + "CPython extension" + maybe "IronClad" and call from IronPython the CPython modules and vice versa -> are these modules executed managed or unmanaged ? Are There any problems if i want to use C# classes and methods from CPython ?
  • Use "Python for .NET" -> the same question as above.

What do you think, which way would be better ? or do you have another solution ?

And the last but maybe most important question, did I understand the above mentioned points right, or do I mess up ?

Many thanks in advance !!

like image 836
Tintenfiisch Avatar asked Jan 19 '26 23:01

Tintenfiisch


2 Answers

I think you misunderstand Python. It's an interpreted1 language. You just provide the text source files and the interpreter will execute them.

There is a difference between the language Python and the implementations CPython, IronPython, Jython, PyPy, what have you. Each of them attempts to implement the language Python as accurately as possible, while also adding implementation-specific functionality. This is just like how, say, the C# compiler was written in C++.

For example, any (pure) Python file can be executed by the IronPython interpreter. But if you know that you're going to use IronPython, you can use the special IronPython features that let you into the .NET library.

Now, most Python doesn't use any of the implementation-specific functionality, so it doesn't matter what you use to run it. Some Python does, though.

1Well, it's compiled into .pyc files... but then "compile" isn't really a well-defined term anyway.


Why does this matter to you? Well, you have a bunch of Python source code that you want to use with the .NET framework. If that code doesn't use any of the CPython-specific features -- such as using C extension modules -- then you can just run it in IronPython.

like image 115
Katriel Avatar answered Jan 22 '26 13:01

Katriel


Expose your Python code via COM and call that from C#. Used this avenue (both ways) many times.

like image 36
Joe43634732436 Avatar answered Jan 22 '26 11:01

Joe43634732436



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!