保存setup.py生成的安装日志
有没有办法把在用setup.py安装程序时生成的输出保存下来?我想把这些输出写进setup.py脚本里,而不是在我运行setup.py时在终端显示出来。
2 个回答
1
你可以在终端或PowerShell中试试下面的命令:
pyhton.exe setup.py >> logfile.txt
这个命令会把所有的控制台输出都添加到logfile.txt文件里。
3
在调用 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())