如何从 C# 运行 Python 脚本? [英] How do I run a Python script from C#?
问题描述
这样的问题以前也有人不同程度的问过,但我觉得回答的不够简洁,所以又问了一遍.
This sort of question has been asked before in varying degrees, but I feel it has not been answered in a concise way and so I ask it again.
我想用 Python 运行一个脚本.假设是这样的:
I want to run a script in Python. Let's say it's this:
if __name__ == '__main__':
with open(sys.argv[1], 'r') as f:
s = f.read()
print s
获取文件位置,读取它,然后打印其内容.没那么复杂.
Which gets a file location, reads it, then prints its contents. Not so complicated.
好的,那么我如何在 C# 中运行它?
Okay, so how do I run this in C#?
这就是我现在所拥有的:
This is what I have now:
private void run_cmd(string cmd, string args)
{
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = cmd;
start.Arguments = args;
start.UseShellExecute = false;
start.RedirectStandardOutput = true;
using (Process process = Process.Start(start))
{
using (StreamReader reader = process.StandardOutput)
{
string result = reader.ReadToEnd();
Console.Write(result);
}
}
}
当我将 code.py
位置作为 cmd
传递,将 filename
位置作为 args
传递时,它不会工作.有人告诉我应该将 python.exe
作为 cmd
传递,然后将 code.py filename
作为 args
.
When I pass the code.py
location as cmd
and the filename
location as args
it doesn't work. I was told I should pass python.exe
as the cmd
, and then code.py filename
as the args
.
我已经找了一段时间了,只能找到建议使用 IronPython 之类的人.但是一定有办法从C#调用Python脚本.
I have been looking for a while now and can only find people suggesting to use IronPython or such. But there must be a way to call a Python script from C#.
一些说明:
我需要从 C# 运行它,我需要捕获输出,我不能使用 IronPython 或其他任何东西.无论你有什么黑客都可以.
I need to run it from C#, I need to capture the output, and I can't use IronPython or anything else. Whatever hack you have will be fine.
P.S.:我运行的实际 Python 代码比这复杂得多,它返回我在 C# 中需要的输出,并且 C# 代码会不断调用 Python 代码.
P.S.: The actual Python code I'm running is much more complex than this, and it returns output which I need in C#, and the C# code will be constantly calling the Python code.
假设这是我的代码:
private void get_vals()
{
for (int i = 0; i < 100; i++)
{
run_cmd("code.py", i);
}
}
推荐答案
它不工作的原因是因为你有 UseShellExecute = false
.
The reason it isn't working is because you have UseShellExecute = false
.
如果您不使用 shell,则必须提供 python 可执行文件的完整路径作为 FileName
,并构建 Arguments
字符串以提供您的脚本和您要读取的文件.
If you don't use the shell, you will have to supply the complete path to the python executable as FileName
, and build the Arguments
string to supply both your script and the file you want to read.
另请注意,除非UseShellExecute = false
,否则您不能RedirectStandardOutput
.
Also note, that you can't RedirectStandardOutput
unless UseShellExecute = false
.
我不太确定应该如何为 python 设置参数字符串的格式,但您需要这样的东西:
I'm not quite sure how the argument string should be formatted for python, but you will need something like this:
private void run_cmd(string cmd, string args)
{
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = "my/full/path/to/python.exe";
start.Arguments = string.Format("{0} {1}", cmd, args);
start.UseShellExecute = false;
start.RedirectStandardOutput = true;
using(Process process = Process.Start(start))
{
using(StreamReader reader = process.StandardOutput)
{
string result = reader.ReadToEnd();
Console.Write(result);
}
}
}
这篇关于如何从 C# 运行 Python 脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!