Title: Error with pip version 22.3.1 and Python version 3.10
Tags: python,python-3.x,pip

Question: I recently came across this error while using "pip install" with python version 3.10 and pip version 22.3.1:
ERROR: Exception:
Traceback (most recent call last):
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2800.0_x64__qbz5n2kfra8p0\lib\site-packages\pip\_internal\cli\base_command.py", line 160, in exc_logging_wrapper
    status = run_func(*args)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2800.0_x64__qbz5n2kfra8p0\lib\site-packages\pip\_internal\cli\req_command.py", line 247, in wrapper
    return func(self, options, args)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2800.0_x64__qbz5n2kfra8p0\lib\site-packages\pip\_internal\commands\download.py", line 103, in run
    build_tracker = self.enter_context(get_build_tracker())
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2800.0_x64__qbz5n2kfra8p0\lib\site-packages\pip\_internal\cli\command_context.py", line 27, in enter_context
    return self._main_context.enter_context(context_provider)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2800.0_x64__qbz5n2kfra8p0\lib\contextlib.py", line 492, in enter_context
    result = _cm_type.__enter__(cm)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2800.0_x64__qbz5n2kfra8p0\lib\contextlib.py", line 135, in __enter__
    return next(self.gen)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2800.0_x64__qbz5n2kfra8p0\lib\site-packages\pip\_internal\operations\build\build_tracker.py", line 46, in get_build_tracker
    root = ctx.enter_context(TempDirectory(kind="build-tracker")).path
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2800.0_x64__qbz5n2kfra8p0\lib\site-packages\pip\_internal\utils\temp_dir.py", line 125, in __init__
    path = self._create(kind)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2800.0_x64__qbz5n2kfra8p0\lib\site-packages\pip\_internal\utils\temp_dir.py", line 164, in _create
    path = os.path.realpath(tempfile.mkdtemp(prefix=f"pip-{kind}-"))
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2800.0_x64__qbz5n2kfra8p0\lib\tempfile.py", line 357, in mkdtemp
    prefix, suffix, dir, output_type = _sanitize_params(prefix, suffix, dir)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2800.0_x64__qbz5n2kfra8p0\lib\tempfile.py", line 126, in _sanitize_params
    dir = gettempdir()
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2800.0_x64__qbz5n2kfra8p0\lib\tempfile.py", line 299, in gettempdir
    return _os.fsdecode(_gettempdir())
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2800.0_x64__qbz5n2kfra8p0\lib\tempfile.py", line 292, in _gettempdir
    tempdir = _get_default_tempdir()
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2800.0_x64__qbz5n2kfra8p0\lib\tempfile.py", line 223, in _get_default_tempdir
    raise FileNotFoundError(_errno.ENOENT,
FileNotFoundError: [Errno 2] No usable temporary directory found in ['C:\\Users\\leon\\AppData\\Local\\Temp', 'C:\\Users\\leon\\AppData\\Local\\Temp', 'C:\\Users\\leon\\AppData\\Local\\Temp', 'C:\\windows\\Temp', 'c:\\temp', 'c:\\tmp', '\\temp', '\\tmp', 'C:\\Users\\leon']
WARNING: There was an error checking the latest version of pip.

Before that there was a acess error with the console history which I had been able to solve, but no mater what I try this error always comes up. I also tried reinstalling python 3.10 and I also tried it with python 3.11 but it's always this error when using pip install. There also was this weird error in Pycharm where it couldn't set upt the virtual env but this is also fixed aready.
Thanks in advance.

Answer: If you read the code for tempfile.py shown in the trace and particulary: _get_default_tempdir() implementation, you will see that the code does following:

get the list of all possible temp directory locations (eg, this list is shown in the actual Exception)
Iterate the list it got
Tries to write a small random file into a given directory
If that works, return the directory name to be used as temporary path.
If not, iterate the rest of the list from 2.
If the list gets iterated to the end, you will get the exception you are now seeing.

So, essentially, your pip install will try to write to bunch of different temporary locations but each one of those fail.
This is most likely that each of those locations, your user does not have write access or your filesystem is full, or there could be some AV tool that blocks writes to these locations or some other reason.
Do check these directories:

C:\Users\leon\AppData\Local\Temp
C:\Users\leon\AppData\Local\Temp
C:\Users\leon\AppData\Local\Temp
C:\windows\Temp
c:\temp
c:\tmp
C:\Users\leon

OR before you run pip, set TMP and TEMP environment variables to point to location where you can write to.