Click here to Skip to main content
12,554,786 members (56,200 online)
Click here to Skip to main content
Add your own
alternative version


6 bookmarked

Step by step guidance of calling Iron Python Function From C# 4.0

, 26 Oct 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
A short demo as how to call a method written in IronPython 2.6 and making a dynamic invocation to the method from C# environment.


IronPython is a scripting language. It is am implementation of the python program language.Written in C#,it is tightly integrated with the .NET Framework.

This small article will demonstrate step by step as to how we can call IronPython 2.6 function from C#4.0 using the dynamic keyword

Using the code

Step 1: Download the latest version of Iron Python from Code Plex

Step 2: Install the software.

Step 3: For this demo we are using python file located inside the Tutorial Folder


The file has two methods

a) Add

b) Factorial.

For this demo purpose, let us only invoke the Add method that performs addition of two numbers being supplied as parameters to the method.

Step 4: We are using a console application for the demo purpose. The very first thing we need to do is to add the dlls as shown under


Step 5: The function invocation happens in just three steps as shown below

class Program
        static void Main(string[] args)
            //Step 1: 
            //Creating a new script runtime             
            var ironPythonRuntime = Python.CreateRuntime();

                //Step 2:
                //Load the Iron Python file/script into the memory
                //Should be resolve at runtime
                dynamic loadIPython = ironPythonRuntime.UseFile("");

                //Step 3:
                //Invoke the method and print the result
                string.Format("Addition result from IronPython method for  {0} and {1} is {2}", 
                                  100,200, loadIPython.add(100, 200))
            catch (FileNotFoundException ex)

The very first line

var ironPythonRuntime = Python.CreateRuntime();

loads the Iron Python libraries along with the DLR code needed to execute the python script.

Second line

dynamic loadIPython = ironPythonRuntime.UseFile("</span />"</span />

loads the python script into memory. The significance of the dynamic variable(loadIPython) here is being the fact that Python calls are resolve at runtime.

The last line i.e.

string.Format("</span />Addition result from IronPython method for {0} 
and {1} is {2}"</span />

Is simply to invoke the Python method and to display the result.

Step 6: And here is the output



Iron Python


In this short tutorial we have seen how to invoke IronPython methods from C# 4.0.

Comments on the topic are highly appreciated for the improvement of the topic.

Thanks for reading the article.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

You may also be interested in...


Comments and Discussions

QuestionGood for newbie Pin
Liqun He6-Mar-14 8:55
memberLiqun He6-Mar-14 8:55 
GeneralMy vote of 1 Pin
Prajnan Das10-Oct-12 1:14
memberPrajnan Das10-Oct-12 1:14 
GeneralMy vote of 1 Pin
Batzen28-Oct-10 1:51
memberBatzen28-Oct-10 1:51 
GeneralMy vote of 1 Pin
SledgeHammer0127-Oct-10 7:21
memberSledgeHammer0127-Oct-10 7:21 
GeneralMy vote of 1 Pin
Argyle4Ever27-Oct-10 4:26
memberArgyle4Ever27-Oct-10 4:26 
GeneralMy vote of 1 Pin
César de Souza27-Oct-10 1:55
memberCésar de Souza27-Oct-10 1:55 
GeneralMy vote of 1 Pin
Keith Barrow26-Oct-10 7:10
memberKeith Barrow26-Oct-10 7:10 
GeneralMy vote of 1 Pin
tinko10126-Oct-10 2:38
membertinko10126-Oct-10 2:38 
GeneralMy vote of 1 Pin
JF201526-Oct-10 2:30
memberJF201526-Oct-10 2:30 
GeneralMy vote of 1 Pin
Nagy Vilmos26-Oct-10 2:26
memberNagy Vilmos26-Oct-10 2:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161021.1 | Last Updated 27 Oct 2010
Article Copyright 2010 by Niladri_Biswas
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid