Python IDLE 脚本不显示子进程的输出,但 cmd.exe 显示 [英] Python IDLE script does not show output of subprocess but cmd.exe does

查看:26
本文介绍了Python IDLE 脚本不显示子进程的输出,但 cmd.exe 显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Windows 7 上使用 Python 2.7.6 和 IDLE.

我有 2 个 Python 脚本:

script.py:

导入子进程、os、sys打印(你好1")mypath = os.path.abspath(__file__)mydir = os.path.dirname(mypath)start = os.path.join(mydir, "script2.py")subprocess.call([sys.executable, start, "param"])打印(再见1")

和上一个脚本正在调用的 script2.py:

导入系统打印你好 2"打印 (sys.argv[1])打印再见 2"

如果我用 cmd.exe shell 运行 script.py,我会得到预期的结果:

C:\tests>python ./script.py你好 1你好 2参数再见 2再见 1

但是如果我用 IDLE 编辑器打开 script.py 并用 F5 运行它,我会得到这个结果:

<预><代码>>>>================================ 重新开始 ==================================>>>你好 1再见 1>>>

为什么子脚本没有写入IDLE Python shell?

解决方案

您正在运行子进程而没有提供任何 stdout 或 stderr.

在终端中运行时,子进程将继承您的 stdout 和 stderr,因此它打印的任何内容都会与您的输出混合显示.

在 IDLE 中运行时,子进程将继承您的 stdout 和 stderr,但这些不会去任何地方.IDLE 拦截 Python 级别的包装器 sys.stdoutsys.stderr,* 所以你从 Python 中打印到它们的任何东西都将在 GUI 窗口中结束,但任何转到真实标准输出或标准错误——就像你运行的任何继承你的流的子进程的输出一样——只是无处可去.**

最简单的解决方法是从子进程中捕获 stdout 和 stderr 并自己打印它们.例如:

out = subprocess.check_output([sys.executable, start, "param"],stderr=subprocess.STDOUT)打印

* IDLE 比看起来更复杂.它实际上为 GUI 窗口和运行代码运行单独的进程,通过套接字进行通信.IDLE 为你的脚本提供的 sys.stdout(以及其他类似的)不是一个 file 对象,它是一个自定义的类文件对象,它重定向每个 write 通过套接字远程过程调用到 GUI 进程.

** 实际上,如果您从终端启动 IDLE 而不是通过双击其图标,子进程的输出可能会在那里结束.我不确定它在 Windows 上是如何工作的.但无论如何,这对您没有帮助.

I'm using Python 2.7.6 and IDLE on Windows 7.

I have 2 Python scripts:

script.py:

import subprocess, os, sys

print("hello 1")

mypath = os.path.abspath(__file__)
mydir = os.path.dirname(mypath)
start = os.path.join(mydir, "script2.py")

subprocess.call([sys.executable, start, "param"])

print("bye 1")

and script2.py that is being called by the previous script:

import sys

print "hello 2"

print (sys.argv[1])

print "bye 2"

If I run script.py with cmd.exe shell I get the expected result:

C:\tests>python ./script.py
hello 1
hello 2
param
bye 2
bye 1

But if I open script.py with the IDLE editor and run it with F5 I get this result:

>>> ================================ RESTART ================================
>>> 
hello 1
bye 1
>>> 

Why is the sub script not writing to the IDLE Python shell?

解决方案

You're running the subprocess without providing any stdout or stderr.

When run in a terminal, the subprocess will inherit your stdout and stderr, so anything it prints will show up intermingled with your output.

When run in IDLE, the subprocess will also inherit your stdout and stderr, but those don't go anywhere. IDLE intercepts the Python-level wrappers sys.stdout and sys.stderr,* so anything you print to them from within Python will end up in the GUI window, but anything that goes to real stdout or stderr—like the output of any subprocess you run that inherits your streams—just goes nowhere.**

The simplest fix is to capture the stdout and stderr from the subprocess and print them yourself. For example:

out = subprocess.check_output([sys.executable, start, "param"],
                              stderr=subprocess.STDOUT)
print out

* IDLE is more complicated than it looks. It's actually running separate processes for the GUI window and for running your code, communicating over a socket. The sys.stdout (and likewise for the others) that IDLE provides for your script isn't a file object, it's a custom file-like object that redirects each write to the GUI process via remote procedure call over the socket.

** Actually, if you launched IDLE from a terminal rather than by double-clicking its icon, the subprocess's output may end up there. I'm not sure how it works on Windows. But at any rate, that isn't helpful to you.

这篇关于Python IDLE 脚本不显示子进程的输出,但 cmd.exe 显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆