执行包含日志的python脚本时,popen返回错误

2024-05-17 05:03:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个python脚本,其中一个使用子流程执行另一个,请参见以下内容:

main.py

import subprocess

command = ['python', 'logging_test.py']
proc1 = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

out, err = proc1.communicate()

print('Output returned from command: {}'.format(out))
print('Error returned from command: {}'.format(err))

logging_test.py

import logging

logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger('log')
logger.info('hello')

运行main.py时,我会将其作为输出:

Output returned from command:
Error returned from command: INFO:log:hello

我希望日志消息由stdout返回,而不是由stderr返回。。。有人知道为什么它会作为错误返回吗


Tags: frompytestimportmainloggingstderrstdout