从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,作为我们刚刚保存为文件的程序的过程。

选择 | 换行 | 行号
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Diagnostics; // Process
  6. using System.IO; // StreamWriter
  7. namespace ConsoleApplication1
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             // the python program as a string. Note '@' which allow us to have a multiline string
  14.             String prg = @"import sys
  15. x = int(sys.argv[1])
  16. y = int(sys.argv[2])
  17. print x+y";
  18.             StreamWriter sw = new StreamWriter("c:\\kudos\\test2.py");
  19.             sw.Write(prg); // write this program to a file
  20.             sw.Close();
  21.  
  22.             int a = 2;
  23.             int b = 2;
  24.  
  25.             Process p = new Process(); // create process (i.e., the python program
  26.             p.StartInfo.FileName = "python.exe";
  27.             p.StartInfo.RedirectStandardOutput = true;
  28.             p.StartInfo.UseShellExecute = false; // make sure we can read the output from stdout
  29.             p.StartInfo.Arguments = "c:\\kudos\\test2.py "+a+" "+b; // start the python program with two parameters
  30.             p.Start(); // start the process (the python program)
  31.             StreamReader s = p.StandardOutput; 
  32.             String output = s.ReadToEnd();
  33.             string []r = output.Split(new char[]{' '}); // get the parameter
  34.             Console.WriteLine(r[0]);
  35.             p.WaitForExit();
  36.  
  37.             Console.ReadLine(); // wait for a key press
  38.         }
  39.     }
  40. }
  41.  

Ironpython方法
有一个很酷的项目称为
Ironpython
。就像我们在其中讨论的Jython一样
上一篇文章
,这是Python的一个版本,是.NET的一部分。
从这里下载:
http://ironpython.net/
安装它,并记住其安装位置的目录(我的计算机上是:
C:\ Program Files \ Ironpython 2.7
)。
在Visual Studio中启动新的控制台应用程序,右键单击右侧的项目,然后选择"添加参考..."。
在Ironpython目录中添加所有.dll文件。
现在,您准备在python程序中创建字符串,在Ironpython中执行它们,并基本上消除了对外部程序,临时文件和进程的需求。与
爪哇

C示例
,这很简单。

选择 | 换行 | 行号
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using IronPython.Hosting; // make us use Python
  6.  
  7. namespace ConsoleApplication2
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.  
  14.             int a = 1;
  15.             int b = 2;
  16.  
  17.             Microsoft.Scripting.Hosting.ScriptEngine py = Python.CreateEngine(); // allow us to run ironpython programs
  18.             Microsoft.Scripting.Hosting.ScriptScope s = py.CreateScope(); // you need this to get the variables
  19.             py.Execute("x = "+a+"+"+b,s); // this is your python program
  20.             Console.WriteLine(s.GetVariable("x")); // get the variable from the python program
  21.             Console.ReadLine(); // wait for the user to press a button
  22.         }
  23.     }
  24. }
  25.  

明智的执行时间,Ironpython似乎比使用过程方法要慢一些。但是话又说回来,如果我们正在查看Ironpython网页,那么对Ironpython的最后更新似乎已经超过一年了!

# 回答1


该过程将如何知道启动c:\\ kudos \\ test2.py,带有参数a和b?

标签: python

评论已关闭