could embed it and run it as part of your C program. Then we realised; perhaps people don't write C programs anymore,so we made the same example in Java. After looking at the bytes.com forum, we started to suspect that perhaps people use C# instead of Java and C.
So here we close the loop and present a C# version!
Installation
We use a Windows 7 computer, with python 2.7 installed. Since we are going to call Python from the command line,
add the path to this Python installation.
Normally, the Python installation is placed in:
C:\Python27
Then to make Python accessible from the command line, we go to control panel, system, advanced system setting and click on this. At the bottom of
this there is going to be an "Environment Variable" button. Click on this, and locate a variable named "Path". If it
isn't there make it, and add C:\Python27. If "Path" is present, we can put C:\Python27 first, but make sure that it ends with a ";"
In our Windows 7, a Visual Studio 10 Express is installed. If such a tool is not present, download and install it.
Process approach
Our idea is the following; We define the Python program in this as a string. This program is going to do two things; Take two numbers from the command line, then add the numbers together and print them out to screen.
The program below will first save the python program to disk (from C#), then it will be executed Python from C# as a process where the program we just saved as a file will be executed.
Expand|Select|Wrap|Line Numbers
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Diagnostics; // Process
- using System.IO; // StreamWriter
- namespace ConsoleApplication1
- {
- class Program
- {
- static void Main(string[] args)
- {
- // the python program as a string. Note '@' which allow us to have a multiline string
- String prg = @"import sys
- x = int(sys.argv[1])
- y = int(sys.argv[2])
- print x+y";
- StreamWriter sw = new StreamWriter("c:\\kudos\\test2.py");
- sw.Write(prg); // write this program to a file
- sw.Close();
- int a = 2;
- int b = 2;
- Process p = new Process(); // create process (i.e., the python program
- p.StartInfo.FileName = "python.exe";
- p.StartInfo.RedirectStandardOutput = true;
- p.StartInfo.UseShellExecute = false; // make sure we can read the output from stdout
- p.StartInfo.Arguments = "c:\\kudos\\test2.py "+a+" "+b; // start the python program with two parameters
- p.Start(); // start the process (the python program)
- StreamReader s = p.StandardOutput;
- String output = s.ReadToEnd();
- string []r = output.Split(new char[]{' '}); // get the parameter
- Console.WriteLine(r[0]);
- p.WaitForExit();
- Console.ReadLine(); // wait for a key press
- }
- }
- }
There is a cool project which is called IronPython. Just like Jython, which we discussed in the previous article, this is a version of Python that is a part of .net.
Download it from here:
http://ironpython.net/
Install it, and remember the directory where it was installed (which on my computer is : C:\Program Files\IronPython 2.7).
Start up a fresh Console application in Visual Studio, right click on the project to the right and select "Add reference...".
Add all the .dll files in the IronPython directory.
Now you are ready to create strings in C# that is Python programs, execute them in IronPython and basically remove the need for external programs, temporary files and processes. Compared to the Java and the C example, this is kind of simple.
Expand|Select|Wrap|Line Numbers
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using IronPython.Hosting; // make us use Python
- namespace ConsoleApplication2
- {
- class Program
- {
- static void Main(string[] args)
- {
- int a = 1;
- int b = 2;
- Microsoft.Scripting.Hosting.ScriptEngine py = Python.CreateEngine(); // allow us to run ironpython programs
- Microsoft.Scripting.Hosting.ScriptScope s = py.CreateScope(); // you need this to get the variables
- py.Execute("x = "+a+"+"+b,s); // this is your python program
- Console.WriteLine(s.GetVariable("x")); // get the variable from the python program
- Console.ReadLine(); // wait for the user to press a button
- }
- }
- }