How can I call (Iron)Python code from a C# app?

The process is simple, especially in a C#/.NET 4 application where support for dynamic languages have been improved via usage of the dynamic type. But it all ultimately depends on how you intend to use the (Iron)Python code within your application. You could always run ipy.exe as a separate process and pass your source files in so they may be executed. But you probably wanted to hostthem in your C# application. That leaves you with many options.

  1. Add a reference to the IronPython.dll and Microsoft.Scripting.dll assemblies. You’ll usually find them both in your root IronPython installation directory.
  2. Add using IronPython.Hosting; to the top of your source and create an instance of the IronPython scripting engine using Python.CreateEngine().
  3. You have a couple of options from here but basically you’d create a ScriptScope or ScriptSource and store it as a dynamic variable. This allows you to execute it or manipulate the scopes from C# if you choose to do so.

Option 1:

Using CreateScope() to create an empty ScriptScope to use directly in C# code but usable in Python sources. You can think of these as your global variables within an instance of the interpreter.

dynamic scope = engine.CreateScope();
scope.Add = new Func<int, int, int>((x, y) => x + y);
Console.WriteLine(scope.Add(2, 3)); // prints 5

Option 2:

Using Execute() to execute any IronPython code in a string. You can use the overload where you may pass in a ScriptScope to store or use variables defined in the code.

var theScript = @"def PrintMessage():
    		          print 'This is a message!'

// execute the script

// execute and store variables in scope
engine.Execute(@"print Add(2, 3)", scope);
// uses the `Add()` function as defined earlier in the scope

Option 3:

Using ExecuteFile() to execute an IronPython source file. You can use the overload where you may pass in a ScriptScope to store or use variables defined in the code.

// execute the script

// execute and store variables in scope
engine.ExecuteFile(@"C:\path\to\", scope);
// variables and functions defined in the scrip are added to the scope

Option 4:

Using GetBuiltinModule() or the ImportModule() extension method to create a scope containing the variables defined in said module. Modules imported this way must be set in the search paths.

dynamic builtin = engine.GetBuiltinModule();
// you can store variables if you want
dynamic list = builtin.list;
dynamic itertools = engine.ImportModule("itertools");
var numbers = new[] { 1, 1, 2, 3, 6, 2, 2 };
Console.WriteLine(builtin.str(list(itertools.chain(numbers, "foobar"))));
// prints `[1, 1, 2, 3, 6, 2, 2, 'f', 'o', 'o', 'b', 'a', 'r']`

// to add to the search paths
var searchPaths = engine.GetSearchPaths();

// import the module
dynamic myModule = engine.ImportModule("mymodule");

You can do quite a lot hosting Python code in your .NET projects. C# helps bridging that gap easier to deal with. Combining all the options mentioned here, you can do just about anything you want. There’s of course more you can do with the classes found in the IronPython.Hosting namespace, but this should be more than enough to get you started.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s