从C#运行Python程序的两种方法
一段时间以前,我们创建了一个如何使用的示例
来自c的python
,既是一个单独的过程,也是您的方式
可以将其嵌入并作为C程序的一部分运行。然后我们意识到;也许人们不再编写C程序了,
所以我们在Java中做了相同的例子
。在查看了Bytes.com论坛之后,我们开始怀疑人们可能使用C#而不是Java和C。
因此,在这里,我们关闭循环并提供C#版本!
安装
我们使用Windows 7计算机,安装了Python 2.7。由于我们要从命令行打电话给Python,所以
添加此Python安装的路径。
通常,将Python安装放置在:
C:\ Python27
然后,为使Python从命令行访问,我们转到控制面板,系统,高级系统设置,然后单击此操作。在底部
这将有一个"环境变量"按钮。单击此,并找到一个名为"路径"的变量。如果它
不是在那里,然后添加
C:\ Python27
。如果存在"路径",我们可以说
C:\ Python27
首先,但要确保它以"";
在我们的Windows 7中,安装了Visual Studio 10 Express。如果不存在这样的工具,
下载
并安装它。
过程方法
我们的想法是以下内容;我们将其定义为字符串。该程序将做两件事。从命令行中获取两个数字,然后将数字添加在一起,然后将它们打印到屏幕上。
下面的程序将首先将Python程序保存到磁盘(从C#)中,然后将其从C#执行Python,作为我们刚刚保存为文件的程序的过程。
- 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
- }
- }
- }
Ironpython方法
有一个很酷的项目称为
Ironpython
。就像我们在其中讨论的Jython一样
上一篇文章
,这是Python的一个版本,是.NET的一部分。
从这里下载:
http://ironpython.net/
安装它,并记住其安装位置的目录(我的计算机上是:
C:\ Program Files \ Ironpython 2.7
)。
在Visual Studio中启动新的控制台应用程序,右键单击右侧的项目,然后选择"添加参考..."。
在Ironpython目录中添加所有.dll文件。
现在,您准备在python程序中创建字符串,在Ironpython中执行它们,并基本上消除了对外部程序,临时文件和进程的需求。与
爪哇
和
C示例
,这很简单。
- 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
- }
- }
- }
明智的执行时间,Ironpython似乎比使用过程方法要慢一些。但是话又说回来,如果我们正在查看Ironpython网页,那么对Ironpython的最后更新似乎已经超过一年了!
评论已关闭