我试图通过使用 subproccess.popen 调用从一个程序 (program1) 运行代码。程序 2(被调用的那个)不显示记录器语句。在下面的示例代码中,程序 1 调用程序 2 并为每个代码设置日志记录。程序1.pyimport subprocessimport loggingimport sysdef configure_logging(name): logger = logging.getLogger(name) logger.setLevel(logging.DEBUG) formatter = logging.Formatter("%(asctime)s:%(name)s:%(message)s") stream_handler = logging.StreamHandler() stream_handler.setFormatter(formatter) logger.addHandler(stream_handler) return loggerlogger = configure_logging(__name__) python_path = "Path/to/specific/pythonexe"py_path = "Path/to/program2.py" cmd = "{} {}".format(python_path,py_path) logger.info("Cmd: {}".format(cmd)) cmd = cmd.split() proc = subprocess.Popen(cmd,stdout=subprocess.PIPE,stderr=subprocess.PIPE,universal_newlines=True)程序2.pyimport loggingdef logstuff(name): logger = logging.getLogger(name) logger.setLevel(logging.DEBUG) formatter = logging.Formatter("%(asctime)s:%(name)s:%(message)s") stream_handler = logging.StreamHandler() stream_handler.setFormatter(formatter) logger.addHandler(stream_handler) return loggerdef main(): logger.info("hi") print("hi2")if __name__ == "__main__": logger = logstuff(__name__) main()使用上面的设置 program1 调用 program2 但 logger.info 语句没有打印出来,只有 print() 语句打印出来。我无法导入 program2,因为它依赖于封装在 anaconda 环境中的特定 python 版本。我想要的输出是将 logger.info 和任何后续记录器调用打印到终端,那么我将如何实现这一目标?
添加回答
举报
0/150
提交
取消