structlog 可以与pythonw一起使用以仅登录文件吗?
使用 pythonw 运行 python 代码时导入 structlog 失败,因为sys.stdout
is None
. 调用时
在文件中引发错误。structlog/_config.py
sys.stdout.isatty()
这是一个揭示问题的简单测试。
# python try_import_structlog.pyw --> success
# pythonw try_import_structlog.pyw --> fail
with open("result.txt", "a") as file:
try:
import structlog
except Exception as error:
file.write(f"import failed: {error}")
else:
file.write("import succeeded")