Fixing PyTorch 1.1 github model load permission error on Windows

Source: Deep Learning on Medium

Fixing PyTorch 1.1 github model load permission error on Windows

While executing sample github model loading on Windows machine, permission error to master.zip file was generated (see error stack trace below).

Using Windows process explorer utility I have identified that python.exe was the file holder.

Investigating further, it was a bug in “_get_cache_or_reload” function of the hub.py module of PyTorch , that keeps this file open.

Fix was pretty simple: Adding “cached_zipfile.close()” line after the “cached_zipfile.extractall(hub_dir)” line:

# Unzip the code and rename the base folder
cached_zipfile.extractall(hub_dir)
# RW Add to fix access issue
cached_zipfile.close()

Error stack trace:

In [4]: en2de = torch.hub.load(‘pytorch/fairseq’, ‘transformer.wmt16.en-de’, tokenizer=’moses’, bpe=’subword_nmt’)
Downloading: “https://github.com/pytorch/fairseq/archive/master.zip” to C:\Users\Ron/.cache\torch\hub\master.zip
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — –
PermissionError Traceback (most recent call last)
<ipython-input-4–44b8fefa6ab9> in <module>()
— → 1 en2de = torch.hub.load(‘pytorch/fairseq’, ‘transformer.wmt16.en-de’, tokenizer=’moses’, bpe=’subword_nmt’)

D:\Projects\Python\Anaconda3\lib\site-packages\torch\hub.py in load(github, model, *args, **kwargs)
330 kwargs.pop(‘force_reload’, None)
331
→ 332 repo_dir = _get_cache_or_reload(github, force_reload)
333
334 sys.path.insert(0, repo_dir)

D:\Projects\Python\Anaconda3\lib\site-packages\torch\hub.py in _get_cache_or_reload(github, force_reload)
167 cached_zipfile.extractall(hub_dir)
168
→ 169 _remove_if_exists(cached_file)
170 _remove_if_exists(repo_dir)
171 shutil.move(extracted_repo, repo_dir) # rename the repo

D:\Projects\Python\Anaconda3\lib\site-packages\torch\hub.py in _remove_if_exists(path)
81 if os.path.exists(path):
82 if os.path.isfile(path):
— -> 83 os.remove(path)
84 else:
85 shutil.rmtree(path)

PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: ‘C:\\Users\\Ron/.cache\\torch\\hub\\master.zip’