Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible run python in .NET application?

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
    }
}
like image 977
Hadi Sharifi Avatar asked Mar 06 '14 11:03

Hadi Sharifi


People also ask

Can Python run on .NET framework?

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 .

Can I use Python in asp net?

The end result is that ASP.NET can help you write web applications faster and with less code—and it can do this even better once we add Python to the mix! The IronPython team has released a project enabling the use of IronPython within ASP.NET.

What is Python for net?

Python for.NET provides a powerful application scripting tool for.NET developers. Using this package you can script.NET applications or build entire applications in Python, using.NET services and components written in any language that targets the CLR (C#, VB.NET, F#, C++/CLI).

Can you run Python code in Visual Studio?

A. Python is generally an interpreted language, with which code is run on demand in a suitable Python-capable environment such as Visual Studio and web servers. Visual Studio itself does not at present provide the means to create a stand-alone executable, which essentially means a program with an embedded Python interpreter.

Is it possible to create executables in Python?

However, the Python community supplied different means to create executables as described on StackOverflow. CPython also supports being embedded within a native application, as described on the blog post, Using CPython's embeddable zip file. Visual Studio 2015 and earlier are available at visualstudio.microsoft.com/vs/older-downloads/.

How do I run a python script from a web service?

For example, if you want to run a script from the web service, that is hosted on IIS — be sure that all the script files that need to be executed have the right permissions for the user under which your web service’s ApplicationPool is running. Calling the Python Script Using IronPython Interpreter, Hosted in Your .NET Application.


1 Answers

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!

like image 58
Paulo Bu Avatar answered Oct 16 '22 23:10

Paulo Bu