Is it possible run python in .NET application?
Asked Answered
S

2

9

In .NET application is possible save C# code in text file or database as string and run dynamically on the fly. This method is useful in many case such as business rule engine or user defined calculation engine and etc. Here is a nice example:

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.CSharp;
using System.CodeDom.Compiler;

class Program
{
    static void Main(string[] args)
    {
        var csc = new CSharpCodeProvider(new Dictionary<string, string>() { { "CompilerVersion", "v3.5" } });
        var parameters = new CompilerParameters(new[] { "mscorlib.dll", "System.Core.dll" }, "foo.exe", true);
        parameters.GenerateExecutable = true;
        CompilerResults results = csc.CompileAssemblyFromSource(parameters,
        @"using System.Linq;
            class Program {
              public static void Main(string[] args) {
                var q = from i in Enumerable.Range(1,100)
                          where i % 2 == 0
                          select i;
              }
            }");
        results.Errors.Cast<CompilerError>().ToList().ForEach(error => Console.WriteLine(error.ErrorText));
    }
}

The class of primary importance here is the CSharpCodeProvider which utilises the compiler to compile code on the fly.

As you know Python is a widely used general-purpose, high-level programming language. Its design philosophy emphasizes code readability, but C# is difficult that python. So it's better use python for dynamic code fragments instead C#.

How to execute python dynamically in C# application?

class Program
{
    static void Main(string[] args)
    {
        var pythonCode = @"
                a=1
                b=2
                c=a+b
                return c";
        //how to execute python code in c# .net
    }
}
Stratfordonavon answered 6/3, 2014 at 11:0 Comment(6)
Have you heard of IronPython?Revival
Could you please share me more detail, I just see it on nuget.Stratfordonavon
I'm also editing the question to include IronPython as a tag since that might bring more useful answers :)Revival
PythonNet is also an option. Better compatibility with other python modules i believe.Lotion
@Lotion is it possible to post simple sample of that?Stratfordonavon
Python for .NET goes the other way - lets you call C# code from Python. If you want to call python from C# natively, then only IronPython can do that.Nagana
R
8

IronPython is an implementation of the Python programming language in .NET (C#). After .NET version 4.0, IronPython's code can be embedded in .NET application with the help of the DLR (Dynamic Language Runtime).

Here's what seems a very reasonable up to date example of how to embed it: http://www.codeproject.com/Articles/602112/Scripting-NET-Applications-with-IronPython.

You can also read the MSDN For Dynamic Language Runtime and its Wikipedia to get additional info on the topic.

Google is also full of tutorials on "How to embed IronPython in .NET".

Hope this helps!

Revival answered 6/3, 2014 at 11:12 Comment(0)
E
0

IronPython is an open-source implementation of the Python programming language which is tightly integrated with the .NET Framework. IronPython can use the .NET Framework and Python libraries, and other .NET languages can use Python code just as easily.

from http://ironpython.net/

and there are tools to integrating with visual studio in their site. and if your aim is to use Reflection and compile python code in run-time (like using of CompileAssemblyFromSource method) you can see this: http://social.msdn.microsoft.com/Forums/vstudio/en-US/9c72c404-eb6c-468b-bdbc-a6a91c55cdfe/c-ironpython-reflection?forum=csharpgeneral

Emersen answered 6/3, 2014 at 11:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.