Python with .NET 4

DLR stands for Dynamic Language Runtime. Dotnet 4.0 supports dynamic language interaction along with static languages for which CLR extends support. Currently supported languages are Ruby and Python. The Dynamic Language Runtime enables language developers to more easily create dynamic languages for the .NET platform. In addition to being a pluggable back-end for dynamic language compilers, the DLR provides language interop for dynamic operations on objects. The DLR has common hosting APIs for using dynamic languages as libraries or for scripting in your .NET applications.
 
IronPython is an implementation of the Python programming language targeting the .NET Framework and Mono ( A cross-platform open-source Dotnet framework currently targeted for Linux). IronPython is written entirely in C#. Python is implemented on top of the Dynamic Language Runtime (DLR), a library running on top of the Common Language Infrastructure that provides dynamic typing and dynamic method dispatch, among other things, for dynamic languages. Python is mainly used by system administrators for executing scripts that do not have knowledge about .Net. Dotnet has extended support for IronPython in order to resolve this dependency so that programmers and administrators can work in conjunction.
 
The first step to getting started is to actually download IronPython. The project is hosted on CodePlex with all of the source code available. After the IronPython installation, its interpreter and its libraries will be installed in your Program Files directory. The interpreter is installed in ipy.exe, where ipy is pronounced "Eye-Pie." The interpreter remains dormant when you call Python from C#. Dotnet projects which want to use Python need to reference dlls.
 
Sample usage of Python from IronPython console
  1. >>> import System  
  2. >>> from System.Collections import *  
  3. >>> h = Hashtable()  
  4. >>> h["a"] = "IronPython"  
  5. >>> h["b"] = "Tutorial"  
  6. >>> for e in h: print e.Key, ":", e.Value 
Once you install Iron Python, there will be Visual Studio templates available for creating the projects. Creating a sample standalone file is the best option. Create some simple script file as seen below:
 
File Name : Test.py
  1. def Simple():  
  2.      print "Hello from Python"  
  3.      print "Call Dir(): "  
  4.      print dir() 
File Name: pyt.py
  1. def factorial(n):  
  2.      "factorial(n) -> returns factorial of n"  
  3.      if n <= 1return 1  
  4.           return n * factorial(n-1
Place these files in your project folder and set "Build Action" to "Content" and "Copy To Output Directory" to "Copy Always".
 
Now, create another application that will act as the hosting environment. Reference dlls from Program Files \ Python folder namely IronPython.dll, IronPython.Modules.dll, Microsoft.Dynamic.dll, Microsoft. Scripting.dll and Microsoft. Scripting.Debugger.dll.
 
Now that you have your project set up correctly you need to do two things:
  1. Write or find a script containing Python code that you want to run
  2. Write C# code to call the script.
Listing 2 shows a very simple IronPython script that contains a single method called Simple() that writes a few strings to a console window. Save this script to a file called Test.py, and add it to your project, as shown in Figure 2. There are several ways to do this. One is to right-click on your project in the Solution Explorer and choose Add | New Item. Add a text file, and call it Test.py. Click on the new node when it is added to your project, and set its Copy to Output Directory property to Copy Always.
 
Your class file from where you call the Python script will be as seen below.
 
Note: The below code didn't work along with IronPython 2.7. So, I had to set up a 2.6 version, and then it worked fine.
 
There are two examples - the first just call one method from Python script and the second calls method, passes a parameter, and captures the return value.
  1. using System;  
  2. using IronPython.Hosting;  
  3. using Microsoft.Scripting.Hosting;  
  4. using Microsoft.Scripting;  
  5. using Microsoft.Scripting.Runtime;  
  6. using System.IO;  
  7. namespace PythonHost  
  8. {  
  9.     class Program  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.             //// Creates the python runtime   
  14.             ScriptRuntime ipy = Python.CreateRuntime();  
  15.             //// The dynamic keyword is needed   
  16.             //// here because Python is an interpreted   
  17.             //// scripting language where calls are bound   
  18.             //// at run time, not at compile time.   
  19.             //// There is no simple or practical way to   
  20.             //// bind calls to Python at compile time   
  21.             //// because Python is designed to be a   
  22.             //// dynamic language which is resolved   
  23.             //// at runtime.   
  24.             dynamic test1 = ipy.UseFile("Test.py");  
  25.             // Call function   
  26.             test1.Simple();  
  27.             // Sample for passing parameters to Python and getting value   
  28.             dynamic test2 = ipy.UseFile("pyt.py");  
  29.             int p = test2.factorial(5);  
  30.         }  
  31.     }  
If all is well...you should be able to run your application.
 
Also, using the 2.7 version I was able to setup SilverLight runtime in conjunction with Python.
 
I set up the Silverlight class library and copied the same two scripts and added the following code to set up the execution.
  1. using System.Windows.Controls;  
  2. using Silverlight = Microsoft.Scripting.Silverlight;  
  3. namespace SilverlightApplication5  
  4. {  
  5.     public partial class MainPage : UserControl  
  6.     {  
  7.         public MainPage()  
  8.         {  
  9.             InitializeComponent();  
  10.             var runtime = Silverlight.DynamicEngine.CreateRuntime();  
  11.             var python = runtime.GetEngine("python");  
  12.             dynamic script = runtime.UseFile("pyt.py");  
  13.             int p = script.factorial(5);  
  14.             script = runtime.UseFile("Test.py");  
  15.             script.Simple();  
  16.         }  
  17.     }  
For Silverlight with the 2.7 version of IronPython, I didn't face any issues...