IronPython and C#

What is IronPython

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

In this post, I will show you how to call the Python method in c#.

Create a new Console application and visual studio
Right click on the project and click on Manage Nuget Packages and search for IronPython**

IronPython and C#

After installing IronPython import following namespaces
using Microsoft.Scripting.Hosting;

Right click on project and add new file** and add following python code**

def  add(a,b):  
return  a+b 

Open Program.cs file and add following code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Scripting.Hosting;
namespace IronPython_Tut1
    class Program
    static void Main(string[] args)
    //Create runtime
            ScriptRuntime runtime = IronPython.Hosting.Python.CreateRuntime();
            dynamic scope = runtime.ExecuteFile(@"");
    //call the python method
            Console.WriteLine("Sum={0}", scope.add(1, 2));
Next Post Previous Post
No Comment
Add Comment
comment url