我创建了一个 python 方法来将数据打印到屏幕和文件中:
def printf(handler, string):
print(string)
sys.stdout.flush()
if handler is not None:
handler.write(str(string))
handler.write("\n")
当我在虚拟环境中运行代码时,它运行得很好。但是,当我使用 pyinstaller 创建的 exe 时,遇到错误:
AttributeError: 'NoneType' object has no attribute 'flush'
对flush方法的调用似乎导致了崩溃,但我不知道如何解决这个问题。 我在网上查找后没有找到解决方案。 有任何想法吗?感谢您的帮助。
谢谢。 塞巴斯蒂安。
Python 文档是这样说的:
Under some conditions stdin, stdout and stderr as well as the original values __stdin__, __stdout__ and __stderr__ can be None. It is usually the case for Windows GUI apps that aren’t connected to a console and Python apps started with pythonw.
这意味着您的 exe 文件没有 stdin、stdout 或 stderr 流。
更多信息:https://docs.python.org/3/library/sys.html?highlight=sys%20stdout#sys.stdout