Needful Software

IronPython C# Integration

1. Basics

Introduction

Embedding IronPython in a C# program is well supported. In this tutorial we will show how this can be done.

On this page we will show two very basic examples of how to execute a very simple script that doesn't import any modules. How to execute scripts that use modules is shown on page 2.

Executing a Python script from C#

First example

Let's create a simple example of a C# application executing a Python script. We use Visual Studio 2013 to create a new project using the Visual C# Console Application template. We call our example PythonScriptExecution1. The full example is available from our GitHub repository: IronPythonTutorials/CSharpIntegration/PythonScriptExecution1

Once the project has been created we use the NuGet package manager to add the IronPython package as shown on Figure 1.

Get IronPython with NuGet
Figure 1: Get IronPython with NuGet

This will add the following assemblies to the project:

  • IronPython.2.7.4\lib\Net45\IronPython.dll
  • IronPython.2.7.4\lib\Net45\IronPython.Modules.dll
  • IronPython.2.7.4\lib\Net45\IronPython.SQLite.dll
  • IronPython.2.7.4\lib\Net45\IronPython.Wpf.dll
  • IronPython.2.7.4\lib\Net45\Microsoft.Dynamic.dll
  • IronPython.2.7.4\lib\Net45\Microsoft.Scripting.dll
  • IronPython.2.7.4\lib\Net45\Microsoft.Scripting.AspNet.dll
  • IronPython.2.7.4\lib\Net45\Microsoft.Scripting.Metadata.dll

For this first example we invoke a Python script that will simply print "Hello World!" on the console. To keep it as simple as possible we simply hardcode the Python code inside a string and use CreateScriptSourceFromString to create the Microsoft.Scripting.Hosting.ScriptSource instance from it. As you can see this is really easy to do and only takes 3 lines of code.

 File: Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PythonScriptExecution1
{
    class Program
    {
        static void Main(string[] args)
        {
            Microsoft.Scripting.Hosting.ScriptEngine pythonEngine = 
                IronPython.Hosting.Python.CreateEngine();
            Microsoft.Scripting.Hosting.ScriptSource pythonScript = 
                pythonEngine.CreateScriptSourceFromString("print 'Hello World!'");
            pythonScript.Execute();
        }
    }
}

Running the program generates the following output on the console.

 Console output
Hello World!
Press any key to continue . . .

Second example

The second example is almost identical to the first one but we will use the CreateScriptSourceFromFile function to load the script from a file instead of hardcoding it in a string.Since we put the script in the same directory as the Program.cs file we need to go up two directories when executing the program from Visual Studio. This is why the path to our script is ..\\..\\HelloWorld.py". You could of course just put the script in the same directory as the executable.The code is shown below. When executing the program the output is of course identical to our previous example.

The full example is available from our GitHub repository: IronPythonTutorials/CSharpIntegration/PythonScriptExecution2

 File: Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PythonScriptExecution2
{
    class Program
    {
    static void Main(string[] args)
        {
            Microsoft.Scripting.Hosting.ScriptEngine pythonEngine = 
                IronPython.Hosting.Python.CreateEngine();
            // We execute this script from Visual Studio 
            // so the program will be executed from bin\Debug or bin\Release
            Microsoft.Scripting.Hosting.ScriptSource pythonScript = 
                pythonEngine.CreateScriptSourceFromFile("..\\..\\HelloWorld.py");
            pythonScript.Execute();
        }
    }
}

And here is the Python script.

 File: HelloWorld.py
print 'Hello World!'