有没有一种方法可以保存在使用setup.py安装的程序的安装过程中生成的输出?我想将其写入setup.py脚本本身,而不是在运行setup.py时在终端中
在setup()
调用该函数之前,请重定向sys.stdout
(并重定向sys.stderr
到日志文件。)
确保将stdout
(和stderr
)还原为它们的默认引用,并同时打印日志文件的内容stdout
。
您的setup.py应该如下所示:
from setuptools import setup, find_packages
import sys
stdout = sys.stdout
stderr = sys.stderr
log_file = open('log', 'w')
sys.stdout = log_file
sys.stderr = log_file
setup (
... ,
...
)
# Make sure to close the log file. You could also use with to surround the setup()
# To ensure log file is closed in the event of exception.
log_file.close()
sys.stdout = stdout
sys.stderr = stderr
with open('log', 'r') as log_file:
sys.stdout.write(log_file.read())
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句