繁体   English   中英

如何在Visual Studio的输出窗口中显示python subprocess stdout和stderr?

[英]How can I get python subprocess stdout and stderr to show up in Visual Studio's output window?

我切换到Visual Studio的Python工具(VS2012),我试图让我的项目启动并运行。 我遇到的问题是没有在Visual Studio输出窗口中显示的子进程的stdout。 我已经创建了一些示例代码来说明问题。

test_console.py

import os
import subprocess
print 'printed from the main process'
command = 'python '  + os.path.join(os.getcwd(),'test_console_sub.py')
subprocess.call(command)

test_console_sub.py

print 'printed from a subprocess'

执行test_console.py时显示的python控制台正确显示两个文件的输出。

在此输入图像描述

输出窗口缺少子进程print语句

在此输入图像描述

以下是一些相关设置

在此输入图像描述

如何在Visual Studio输出窗口中显示子进程print语句? 理想情况下,输出窗口看起来与python控制台窗口完全相同。

作为一种解决方法(查看AnojiRox的答案),您可以从子进程捕获stdout和stderr,然后从主进程打印它,但由于文档中声明的死锁问题,您需要使用Popen及其communicate方法

import os
import subprocess

print 'printed from the main process'
command = ['python', os.path.join(os.getcwd(), 'test_console_sub.py')]
p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(stdout, stderr) = p.communicate()
print stdout
print stderr

你不能。 我之前搜索过这个,但一无所获。 如果我错了纠正我。

尝试使用文本编辑器和python控制台。

暂无
暂无

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

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