简体   繁体   English

运行python脚本的subprocess.Popen()未显示input()提示

[英]subprocess.Popen() running a python script is not showing input() prompt

I'm running a separate python script from my "main" python script using subprocess.Popen(), the reason I use subprocess is that I can capture errors. 我正在使用subprocess.Popen()从我的“主” python脚本运行一个单独的python脚本,原因是我可以捕获错误。 My problem is that if the separate script uses an input("Enter something: ") statement with a prompt (the text in between the quotes) then it (the prompt) will not be shown in the console when running the "main" script that then, in turn, runs the separate script via subprocess.Popen(). 我的问题是,如果单独的脚本使用带有提示(引号之间的文本)的input(“ Enter something:”)语句,则在运行“ main”脚本时,它(提示)将不会显示在控制台中然后依次通过subprocess.Popen()运行单独的脚本。

Let's say the "main" python script looks like this: 假设“主要” python脚本如下所示:

cmd = "python3 tmp.py"

output = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
stout, sterror = output.communicate()

And "tmp.py" looks like this: 并且“ tmp.py”看起来像这样:

text = input("Enter something: ")

When I now run the "main" script the output will just be a blinking cursor where I can enter text, press enter and store the input in the text variable, but the problem is that the prompt ("Enter something: ") will not be shown/is missing! 现在,当我运行“主”脚本时,输出将只是一个闪烁的光标,可以在其中输入文本,按Enter键并将输入存储在text变量中,但是问题是提示(“ Enter something:”)不会显示/丢失! Why is this? 为什么是这样? and how can I fix it? 以及如何解决?

You have redirected stdout to a PIPE . 您已将stdout重定向到PIPE So that is where the text "Enter something: " goes to... 这就是文本“输入内容:”转到...的地方

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

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