`trainer.train()` cannot rename a folder when run in Jupyter Notebook on Conda on Windows
Author: kwon0408
Pull request: no
Labels: name: solved

### System Info

- `transformers` version: 4.37.1
- Platform: Windows-10-10.0.19045-SP0
- Python version: 3.11.7
- Huggingface_hub version: 0.20.2
- Safetensors version: 0.3.3
- Accelerate version: 0.26.1
- Accelerate config:    not found
- PyTorch version (GPU?): 2.2.0 (True)
- 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?: no
----
- Conda version: 23.7.4
- Jupyter Notebook version: 7.0.7

### Who can help?

_No response_

### Information

- [X] 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

Follow the instructions in [Fine-Tune ViT for Image Classification with 🤗 Transformers](https://huggingface.co/blog/fine-tune-vit), and at the [Train 🚀](https://huggingface.co/blog/fine-tune-vit#train-%F0%9F%9A%80) part, you will encounter an error like below when step 101 is about to start:

```
---------------------------------------------------------------------------
PermissionError                           Traceback (most recent call last)
Cell In[21], line 1
----> 1 train_results = trainer.train()
      2 trainer.save_model()
      3 trainer.log_metrics("train", train_results.metrics)

File ~\anaconda3\envs\GazeTrack\Lib\site-packages\transformers\trainer.py:1539, in Trainer.train(self, resume_from_checkpoint, trial, ignore_keys_for_eval, **kwargs)
   1537         hf_hub_utils.enable_progress_bars()
   1538 else:
-> 1539     return inner_training_loop(
   1540         args=args,
   1541         resume_from_checkpoint=resume_from_checkpoint,
   1542         trial=trial,
   1543         ignore_keys_for_eval=ignore_keys_for_eval,
   1544     )

File ~\anaconda3\envs\GazeTrack\Lib\site-packages\transformers\trainer.py:1929, in Trainer._inner_training_loop(self, batch_size, args, resume_from_checkpoint, trial, ignore_keys_for_eval)
   1926     self.state.epoch = epoch + (step + 1 + steps_skipped) / steps_in_epoch
   1927     self.control = self.callback_handler.on_step_end(args, self.state, self.control)
-> 1929     self._maybe_log_save_evaluate(tr_loss, model, trial, epoch, ignore_keys_for_eval)
   1930 else:
   1931     self.control = self.callback_handler.on_substep_end(args, self.state, self.control)

File ~\anaconda3\envs\GazeTrack\Lib\site-packages\transformers\trainer.py:2300, in Trainer._maybe_log_save_evaluate(self, tr_loss, model, trial, epoch, ignore_keys_for_eval)
   2297         self.lr_scheduler.step(metrics[metric_to_check])
   2299 if self.control.should_save:
-> 2300     self._save_checkpoint(model, trial, metrics=metrics)
   2301     self.control = self.callback_handler.on_save(self.args, self.state, self.control)

File ~\anaconda3\envs\GazeTrack\Lib\site-packages\transformers\trainer.py:2415, in Trainer._save_checkpoint(self, model, trial, metrics)
   2413 if staging_output_dir != output_dir:
   2414     if os.path.exists(staging_output_dir):
-> 2415         os.rename(staging_output_dir, output_dir)
   2417         # Ensure rename completed in cases where os.rename is not atomic
   2418         fd = os.open(output_dir, os.O_RDONLY)

PermissionError: [WinError 5] 액세스가 거부되었습니다: './vit-base-beans\\tmp-checkpoint-100' -> './vit-base-beans\\checkpoint-100'
```
(The Korean phrase in the last line means "Access Denied".)

I tried running Anaconda Powershell with admin privilege, but the result was the same.

The only difference between the blog post and my code is the font path in `show_examples()`, but I don't think this is an important reason.

### Expected behavior

Renaming the folder succeeds and steps after 100 run smoothly.

Comments:
cc @muellerzr 

@kwon0408 please update your transformers version, this was fixed in the next patch that was released ~1 week ago

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/transformers/blob/main/CONTRIBUTING.md) are likely to be ignored.