AttributeError: 'NoneType' object has no attribute 'flush'
Author: akesh1235
Pull request: no

### System Info

**System info**
- `transformers` version: 4.29.2                                                                                      
- Platform: Windows-10-10.0.19045-SP0                                                                                   
- Python version: 3.11.3                                                                                                
- Huggingface_hub version: 0.15.1                                                                                       
- Safetensors version: not installed                                                                                    
- PyTorch version (GPU?): not installed (NA)                                                                            
- Tensorflow version (GPU?): not installed (NA)                                                                         
- Flax version (CPU?/GPU?/TPU?): not installed (NA)                                                                     
- Jax version: not installed                                                                                            
- JaxLib version: not installed                                                                                         
- Using GPU in script?: No                                                                                      
- Using distributed or parallel set-up in script?: <fill in>

**Issue**

**After creating virtual environment and installing requirements.txt, carried out following steps to convert **`.py`** file into **`.exe`** ** using pyinstaller library

**step 1 : `pip install pyinstaller`** 
**step 2 : `pyinstaller --name GrammarCorrector --onefile --windowed new_gram1_Tkinter.py --hidden-import cymem.cymem`**

**Then i got this AttributeError:**
Traceback (most recent call last):
  File "new_gram1_Tkinter.py", line 271, in <module>
  File "new_gram1_Tkinter.py", line 142, in __init__
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "PyInstaller\loader\pyimod02_importers.py", line 352, in exec_module
  File "transformers\__init__.py", line 26, in <module>
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "PyInstaller\loader\pyimod02_importers.py", line 352, in exec_module
  File "transformers\dependency_versions_check.py", line 17, in <module>
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "PyInstaller\loader\pyimod02_importers.py", line 352, in exec_module
  File "transformers\utils\__init__.py", line 30, in <module>
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "PyInstaller\loader\pyimod02_importers.py", line 352, in exec_module
  File "transformers\utils\generic.py", line 29, in <module>
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "PyInstaller\loader\pyimod02_importers.py", line 352, in exec_module
  File "transformers\utils\import_utils.py", line 36, in <module>
  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'**


I raised issue in `pyinstaller `repository, and i got answer as followed below from @bwoodsend who is a maintainer


 **You should be able to get the same error without `PyInstaller `if you run your source code using `pythonw `instead of just `python`.**  **Raise a bug to** **`transformers`** if they have their own **windowed-mode-naive logger**. https://github.com/orgs/pyinstaller/discussions/7689#discussion-5270292



### Who can help?

@sgugger
@ArthurZucker
@LysandreJik

### Information

- [ ] The official example scripts
- [ ] My own modified scripts

### Tasks

- [ ] An officially supported task in the `examples` folder (such as GLUE/SQuAD, ...)
- [ ] My own task or dataset (give details below)

### Reproduction

I want my **`.py`** file in **`.exe`** file and when i am doing that using `pyinstaller `it is giving Attribute error when i asked `pyinstaller `developers on repository they suggested me to raise bug report on `transformers `saying that **if they have their own windowed-mode-naive logger.**

### Expected behavior

i want **`.exe`** file from **`.py`** 

Comments:
cc @LysandreJik maybe

Hello, I have encountered the same problem as you, did you solve it?

Hi! I also encountered this error. I'm building a package with `pyinstaller` which works on MacOS with M2 amd64. Running inside of a Windows VM running Windows 11, this fails with the same error. 

```
File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "PyInstaller\loader\pyimod02_importers.py", line 385, in exec_module
  File "transformers\utils\import_utils.py", line 37, in <module>
    logger = logging.get_logger(__name__)  # pylint: disable=invalid-name
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "transformers\utils\logging.py", line 124, in get_logger
    _configure_library_root_logger()
  File "transformers\utils\logging.py", line 88, in _configure_library_root_logger
    _default_handler.flush = sys.stderr.flush
                             ^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'flush'
```

> 你好！我也遇到了这个错误。我正在构建一个在 MacOS 上使用 M2 amd64 的软件包。在运行 Windows 11 的 Windows VM 中运行，此操作失败并出现相同的错误。`pyinstaller`
> 
> ```
> File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
>   File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
>   File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
>   File "PyInstaller\loader\pyimod02_importers.py", line 385, in exec_module
>   File "transformers\utils\import_utils.py", line 37, in <module>
>     logger = logging.get_logger(__name__)  # pylint: disable=invalid-name
>              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>   File "transformers\utils\logging.py", line 124, in get_logger
>     _configure_library_root_logger()
>   File "transformers\utils\logging.py", line 88, in _configure_library_root_logger
>     _default_handler.flush = sys.stderr.flush
>                              ^^^^^^^^^^^^^^^^
> AttributeError: 'NoneType' object has no attribute 'flush'
> ```
You can add this code before your transformers import
if sys.stdout is None:
    sys.stdout = open(os.devnull, "w")
if sys.stderr is None:
    sys.stderr = open(os.devnull, "w")

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