Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integration of python in C# Application

I have the following problem: I got an old application which is written in python. This application allows the user to specify small python steps which will be executed, python steps are basically small python scripts, I call them steps because the execution of this application involves other steps like executing something from commandline. These python steps are stored as python code in an xml file.

Now I want to rewrite the application by using C# .NET. Is there a best practise solution to do something like this?

I don't want to call python as external programm and pass the actual python step(script) to the python interpreter - I want something built in. I just came across IronPython and .NET python but I am not quite sure which one to use. I want to achieve some sort of debugging for the small scripts, that is why the call the interpreter solution is not acceptable.

What is more important is that a lot of those scripts already exist. Therefore I have to use a C# implementation of python which has the same syntax as python as well as the same built in libs of python. Is this even possible?

Thanks and Greetings Xeun

like image 605
Xeun Avatar asked Apr 17 '14 09:04

Xeun


People also ask

Can we integrate Python with C?

Any code that you write using any compiled language like C, C++, or Java can be integrated or imported into another Python script. This code is considered as an "extension." A Python extension module is nothing more than a normal C library. On Unix machines, these libraries usually end in .


2 Answers

IronPython is what you want. It compiles to .NET bytecode. You can reasonably easily call python code from another .NET language (and the other way around). IronPython is supported in Visual Studio too, I think.

like image 125
cdjc Avatar answered Sep 20 '22 05:09

cdjc


Python scripts can be executed from C# with IronPython.

    var ipy = Python.CreateRuntime();
    dynamic test = ipy.UseFile("Test.py");
    test.Simple();

See also:

http://putridparrot.com/blog/hosting-ironpython-in-a-c-application/

and

https://blogs.msdn.microsoft.com/charlie/2009/10/25/running-ironpython-scripts-from-a-c-4-0-program/

like image 36
FrankyHollywood Avatar answered Sep 18 '22 05:09

FrankyHollywood