简体   繁体   English

在C#中运行交互式python脚本

[英]Running an interactive python script in C#

I've searched for a way to do this but have so far not been successful. 我一直在寻找一种实现此目的的方法,但到目前为止尚未成功。

My question is similar to this one: How do I run a Python script from C#? 我的问题与此相似: 如何从C#运行Python脚本?

But in my case, the script goes more like this: 但就我而言,脚本更像这样:

def main():
  i = raw_input("i = ")

I can redirect the output fine by creating a process like the following: 我可以通过创建如下所示的过程来重定向输出结果:

ProcessStartInfo info = new ProcessStartInfo("cmd", "/c python myFile.py");

How would I send input back to it? 如何将输入发送回去?

Please note, the python script can be anything. 请注意,python脚本可以是任何东西。 I don't know when an input might be expected so using a streamwriter to write to the process by setting redirectStandardInput to true doesn't work.. 我不知道何时会期望输入,因此使用streamwriter通过将redirectStandardInput设置为true来写入进程不起作用。

Any help would be great. 任何帮助都会很棒。 Thanks all! 谢谢大家!

You will have to redirect the stdin of your process. 您将不得不重定向过程的stdin Here is a full example of how you can do that: 这是如何执行此操作的完整示例:

http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.redirectstandardinput.aspx http://msdn.microsoft.com/zh-CN/library/system.diagnostics.processstartinfo.redirectstandardinput.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM