'PermissionError: [WinError 32] when importing the requests module

When I run the command import requests, I get this:

Traceback (most recent call last):
  File "C:\Users\user1\AppData\Local\Programs\Python\Python310\lib\importlib\_common.py", line 89, in _tempfile
    os.write(fd, reader())
  File "C:\Users\user1\AppData\Local\Programs\Python\Python310\lib\importlib\abc.py", line 371, in read_bytes
    with self.open('rb') as strm:
  File "C:\Users\user1\AppData\Local\Programs\Python\Python310\lib\importlib\_adapters.py", line 54, in open
    raise ValueError()
ValueError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\user1\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\__init__.py", line 118, in <module>
    from . import utils
  File "C:\Users\user1\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\utils.py", line 25, in <module>
    from . import certs
  File "C:\Users\user1\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\certs.py", line 15, in <module>
    from certifi import where
  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 "C:\Users\user1\AppData\Local\Programs\Python\Python310\lib\site-packages\wrapt\importer.py", line 170, in exec_module
    notify_module_loaded(module)
  File "C:\Users\user1\AppData\Local\Programs\Python\Python310\lib\site-packages\wrapt\decorators.py", line 470, in _synchronized
    return wrapped(*args, **kwargs)
  File "C:\Users\user1\AppData\Local\Programs\Python\Python310\lib\site-packages\wrapt\importer.py", line 136, in notify_module_loaded
    hook(module)
  File "C:\Users\user1\AppData\Local\Programs\Python\Python310\lib\site-packages\certifi_win32\wrapt_certifi.py", line 20, in apply_patches
    certifi_win32.wincerts.CERTIFI_PEM = certifi.where()
  File "C:\Users\user1\AppData\Local\Programs\Python\Python310\lib\site-packages\certifi\core.py", line 37, in where
    _CACERT_PATH = str(_CACERT_CTX.__enter__())
  File "C:\Users\user1\AppData\Local\Programs\Python\Python310\lib\contextlib.py", line 135, in __enter__
    return next(self.gen)
  File "C:\Users\user1\AppData\Local\Programs\Python\Python310\lib\importlib\_common.py", line 95, in _tempfile
    os.remove(raw_path)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\Users\\user1\\AppData\\Local\\Temp\\tmpz9h1wrjd'

If I delete the file at the bottom of the error, it generates a new random file and the new file comes up in the error code.



Solution 1:[1]

Fixed, baruch Hashem! I:

  1. Uninstalled python
  2. Deleted the Lib folder in C:/User/user1/AppData/Local/Programs/Python/Python39 and C:/User/user1/AppData/Local/Programs/Python/Python310
  3. Re-installed python.

It works!

Solution 2:[2]

No need to delete and reinstall Python completely when you are using virtual environments.

This is very useful to keep python environment separated from one project to another.

Just (re)create a new Python venv. Tested successfully. You can use Anaconda Navigator on Windows to do that easily.

On Linux, just run the following commands of a terminal: conda create --name py35 python=3.5 (here we specify the name of the venv to be py35 and we select Python 3.5 as default Python in the venv. Then run source activate py34 to activate the venv and source deactivate to go back to default one. More commands and info here | cheatsheet.

More info here: Creation of virtual environments

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kovy Jacob
Solution 2 Onyr