Solutions to 'RuntimeError: Parent directory does not exist' Error When Saving Models in PyTorch
Error
When saving a model or weights in PyTorch, you may encounter the following error, even though the path definitely exists.
>>> print("Is exists path?: ", os.path.exists(directory))
Is exists path?: True
>>> torch.save(model.state_dict(), directory + 'weights.pt')
RuntimeError: Parent directory _____ does not exist.
Solution
In my case, the file path contained the special character deltaΔ, and removing it solved the problem. However, this is not always the cause of the error. Among three computers, only one experienced this issue, suggesting that the problem is not solely due to the character ‘Δ’, but also depends on the environment of each computer. Whether the model was loaded on a CPU or GPU was irrelevant to the error. Based on the results below, it seems that the difference might be between pytorch and pytorch-cuda.
Environment
Computer environment with the error:
- OS: Windows11
- Version: Python 3.9.13, torch==1.13.1
Computer environments without the error:
OS: Windows11
Version: Python 3.9.2, torch==1.8.1+cu111
OS: Windows10
Version: Python 3.8.4, torch==1.9.0+cu111