23

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

Xeun
  • 1,089
  • 1
  • 11
  • 20

3 Answers3

19

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.

schoetbi
  • 12,009
  • 10
  • 54
  • 72
cdjc
  • 1,039
  • 12
  • 24
  • 1
    The main potential issue with IronPython is if these scripts use any C extensions - if they're just pure Python + stdlib they should be fine. – Jeff Hardy Apr 18 '14 at 14:02
  • The scripts can use C extensions thats ture. Is there any issue with this? – Xeun May 06 '14 at 06:47
  • 5
    I am currently trying to figure out how to merge some numpy/matplotlib in a c# project, to simplify displaying some data. Ironpython seem to have fallen way behind, as those libraries do not appear to be available. – Bovaz May 01 '15 at 22:38
  • 2
    @Bovaz you can try pythonnet for C-API extensions and newer Python versions – denfromufa Mar 30 '17 at 12:12
10

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/

FrankyHollywood
  • 1,497
  • 19
  • 18
  • 1
    Uolot came with another solution btw, compile your python with p2exe and call that from .NET https://stackoverflow.com/questions/1371994/importing-external-module-in-ironpython – FrankyHollywood Oct 10 '17 at 09:39
  • Sorry mate I down-voted your answer by mistake (a mouse twitch really) and didn't discover in 5 min and I'm unable to undo it now – Lionet Chen Jun 25 '19 at 07:21
  • no worries, the answer is not having a high-score anyway :) thx for letting me know! – FrankyHollywood Jun 28 '19 at 10:45
  • second link is really useful. But if I'm building a winform/wpf desktop application, than do I need to install ironpython in all my client systems? I mean whoever will be using my application, do they need to install ironpython in their system as well? – StackUseR Mar 22 '21 at 12:30
5

You can use IronPython's ScriptScope.GetVariable to get the actual function and then you can call it like a C# function. Use it like this:

C# code:

var var1, var2 = ...
ScriptEngine engine = Python.CreateEngine();
ScriptScope scope = engine.CreateScope();
engine.ExecuteFile(@"C:\test.py", scope);
dynamic testFunction = scope.GetVariable("test_func");
var result = testFunction(var1,var2);

Python code:

def test_func(var1,var2):
    ...do something...

Took me a while to finally figure it out, and it's quite simple.. And IronPython is free and pretty easy to use. Hope this helps :)

Liron Achdut
  • 881
  • 10
  • 12