AttributeError: ‘NoneType‘ object has no attribute ‘flush‘

问题描述

当你的项目中使用了transformers库,并且使用 pyinstaller 打包无控制台窗口的程序时就会报这个错。

Traceback (most recent call last):
File "main.py", line 4, in 
File "", line 983, in _find_and_load
File "", line 967, in _find_and_load_unlocked
File "", line 677, in _load_unlocked
File "PyInstaller\loader\pyimod02_importers.py", line 385, in exec_module
File "views\app_layout.py", line 18, in 
File "", line 983, in _find_and_load
File "", line 967, in _find_and_load_unlocked
File "", line 677, in _load_unlocked
File "PyInstaller\loader\pyimod02_importers.py", line 385, in exec_module
File "utils\sentence_transformer_utils.py", line 8, in 
File "", line 983, in _find_and_load
File "", line 967, in _find_and_load_unlocked
File "", line 677, in load_unlocked
File "PyInstaller\loader\pyimod02_importers.py", line 385, in exec_module
File "sentence_transformers_init.py", line 3, in 
File "", line 983, in _find_and_load
File "", line 967, in _find_and_load_unlocked
File "", line 677, in load_unlocked
File "PyInstaller\loader\pyimod02_importers.py", line 385, in exec_module
File "sentence_transformers\datasets_init.py", line 3, in 
File "", line 983, in _find_and_load
File "", line 967, in _find_and_load_unlocked
File "", line 677, in _load_unlocked
File "PyInstaller\loader\pyimod02_importers.py", line 385, in exec_module
File "sentence_transformers\datasets\ParallelSentencesDataset.py", line 4, in 
File "", line 983, in _find_and_load
File "", line 967, in _find_and_load_unlocked
File "", line 677, in _load_unlocked
File "PyInstaller\loader\pyimod02_importers.py", line 385, in exec_module
File "sentence_transformers\SentenceTransformer.py", line 11, in 
File "", line 983, in _find_and_load
File "", line 967, in _find_and_load_unlocked
File "", line 677, in load_unlocked
File "PyInstaller\loader\pyimod02_importers.py", line 385, in exec_module
File "transformers_init.py", line 26, in 
File "", line 983, in _find_and_load
File "", line 967, in _find_and_load_unlocked
File "", line 677, in _load_unlocked
File "PyInstaller\loader\pyimod02_importers.py", line 385, in exec_module
File "transformers\dependency_versions_check.py", line 17, in 
File "", line 983, in _find_and_load
File "", line 967, in _find_and_load_unlocked
File "", line 677, in load_unlocked
File "PyInstaller\loader\pyimod02_importers.py", line 385, in exec_module
File "transformers\utils_init.py", line 30, in 
File "", line 983, in _find_and_load
File "", line 967, in _find_and_load_unlocked
File "", line 677, in _load_unlocked
File "PyInstaller\loader\pyimod02_importers.py", line 385, in exec_module
File "transformers\utils\generic.py", line 29, in 
File "", line 983, in _find_and_load
File "", line 967, in _find_and_load_unlocked
File "", line 677, in _load_unlocked
File "PyInstaller\loader\pyimod02_importers.py", line 385, in exec_module
File "transformers\utils\import_utils.py", line 37, in 
File "transformers\utils\logging.py", line 124, in get_logger
File "transformers\utils\logging.py", line 88, in _configure_library_root_logger
AttributeError: 'NoneType' object has no attribute 'flush'

解决方法

在导入 transformers 库之前添加以下代码

if sys.stdout is None:
    sys.stdout = open(os.devnull, "w")
if sys.stderr is None:
    sys.stderr = open(os.devnull, "w")

参考

https://github.com/huggingface/transformers/issues/24047

你可能感兴趣的:(python,python,pyinstaller)