Repository: huggingface/diffusers

Issue:
AttributeError: 'NoneType' object has no attribute 'flush'
### Describe the bug

I have a python program that I have converted into an exe file using PyInstaller. I have the latest versions of Diffusers and Transformers library which are 0.16.1 and 4.28.1 respectively. When I run the exe file, I get this error: AttributeError: 'NoneType' object has no attribute 'flush'. But when I am running the python file, its working fine.

### Reproduction

Run the exe file,  and an error window will show. More details on the error on the logs I have provided.

### Logs

```shell
Traceback (most recent call last):
  File "app.py", line 36, in <module>
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "PyInstaller\loader\pyimod02_importers.py", line 352, in exec_module
  File "diffusers\__init__.py", line 3, in <module>
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "PyInstaller\loader\pyimod02_importers.py", line 352, in exec_module
  File "diffusers\configuration_utils.py", line 34, in <module>
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "PyInstaller\loader\pyimod02_importers.py", line 352, in exec_module
  File "diffusers\utils\__init__.py", line 21, in <module>
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "PyInstaller\loader\pyimod02_importers.py", line 352, in exec_module
  File "diffusers\utils\accelerate_utils.py", line 20, in <module>
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "PyInstaller\loader\pyimod02_importers.py", line 352, in exec_module
  File "diffusers\utils\import_utils.py", line 38, in <module>
  File "diffusers\utils\logging.py", line 121, in get_logger
  File "diffusers\utils\logging.py", line 85, in _configure_library_root_logger
AttributeError: 'NoneType' object has no attribute 'flush'
```


### System Info

Windows 10


Discussion:
Share the file here as txt
i have a same problem :( looking for someone can help us 
Would be great to have a reproducible code snippet here 
Im trying to use pretrained transformer model from Hugging Face , and apply some libraries to make a GUI window allow users interact to . But when i try to convert Python Script to Exe File , i got error above 

My code :

from transformers import RobertaForSequenceClassification, AutoTokenizer , AutoModelForTokenClassification , pipeline
import tkinter as tk
import torch
from tkinter import filedialog
import nltk
import docx2txt
import pandas as pd 
from PIL import Image, ImageTk
import pandas as pd
import matplotlib.pyplot as plt
import openpyxl
from openpyxl.styles import Font, Color, PatternFill
from openpyxl.styles.colors import WHITE
nltk.download('punkt')

 -Load the model and tokenizer
senti_model = RobertaForSequenceClassification.from_pretrained("wonrax/phobert-base-vietnamese-sentiment")
senti_tokenizer = AutoTokenizer.from_pretrained("wonrax/phobert-base-vietnamese-sentiment", use_fast=False)

 -Load the model and tokenizer segmentation
seg_tokenizer = AutoTokenizer.from_pretrained("NlpHUST/vi-word-segmentation")
seg_model = AutoModelForTokenClassification.from_pretrained("NlpHUST/vi-word-segmentation")
nlp = pipeline("token-classification", model=seg_model, tokenizer=seg_tokenizer)

There is an image i think what cause this issue : 
![image](https://user-images.githubusercontent.com/94973318/236717216-e5c044f3-a9ec-4a58-b172-6a9a05e06161.png)

My warning  file : 
[warn-test.txt](https://github.com/huggingface/diffusers/files/11419681/warn-test.txt)

thanks for helping

I am facing the same problem. It already occurs while executing the line importing sentence_transformers.
found a temporary fix by commenting out the flush code of the diffusers and transformers -> utils -> logging.py
Here is the line from diffusers as an example:
![image](https://github.com/huggingface/diffusers/assets/91329439/9ad04236-d03e-4648-8447-10238548c1dc)

Here is the line from transformers:
![image](https://github.com/huggingface/diffusers/assets/91329439/1ab2ddba-4fac-4c95-9134-324735ecaeb9)

One thing I noticed is that it seems the code is having trouble recognizing the flush method between two libraries, one of which is a built-in for python sys
This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the [contributing guidelines](https://github.com/huggingface/diffusers/blob/main/CONTRIBUTING.md) are likely to be ignored.
if by chance you have this error and you have a Virtualenv, remember to generate the pyinstaller exe from inside the virtual environment, I solved it like this
The problem is that under Windows, in `windowed` mode, there is no console attached. This means that `stdout` and `stderr` are not set. So to fix this, before importing `torch`, check if `stderr` or `stdout` is `none` and redirect it somewhere else. For example, /dev/null if you are not interested in the output:

```python
import os
import sys

# redirect stdout and stderr
if sys.stdout is None or sys.stderr is None:
    sys.stdout = open(os.devnull, 'w')
    sys.stderr = open(os.devnull, 'w') 

# now import torch and flush will work
import torch
```
I had the same problem and cansik gets the W today.

Resolving patch:
diff --git a/src/diffusers/utils/logging.py b/src/diffusers/utils/logging.py
--- a/src/diffusers/utils/logging.py
+++ b/src/diffusers/utils/logging.py
@@ -82,7 +82,9 @@ def _configure_library_root_logger() -> None:
             # This library has already configured the library root logger.
             return
         _default_handler = logging.StreamHandler()  # Set sys.stderr as stream.
-        _default_handler.flush = sys.stderr.flush
+
+        if sys.stderr:  # only if sys.stderr exists, e.g. when not using pythonw in windows
+            _default_handler.flush = sys.stderr.flush
 
         # Apply our default configuration to the library root logger.
         library_root_logger = _get_library_root_logger()


Test patch: