升級pip報錯ERROR: Could not install packages due to an OSError: [WinError 5]

EdisonYao發表於2023-03-29

今天在安裝python第三方庫時,提示pip需要升級,沒有多想直接升級,結果報錯:

1 ERROR: Could not install packages due to an OSError: [WinError 5] 拒絕訪問。: 'd:\\python\\scripts\\pip.exe'
2 Consider using the `--user` option or check the permissions.

一、報錯處理 

從報錯資訊看,大概是許可權問題,於是使用管理員身份執行CMD,再次升級依舊報錯。

D:\Python\Lib\site-packages>pip install --upgrade pip
Requirement already satisfied: pip in d:\python\lib\site-packages (21.1.1)
Collecting pip
  Downloading pip-23.0.1-py3-none-any.whl (2.1 MB)
     |████████████████████████████████| 2.1 MB 2.2 MB/s
Installing collected packages: pip
  Attempting uninstall: pip
    Found existing installation: pip 21.1.1
    Uninstalling pip-21.1.1:
ERROR: Could not install packages due to an OSError: [WinError 5] 拒絕訪問。: 'd:\\python\\scripts\\pip.exe'
Consider using the `--user` option or check the permissions.

升級時PyCharm未關閉,聯想到之前處理過的報錯,檢視了 D:\Python\Lib\site-packages 目錄,果然pip

相關的兩個資料夾已經不在了。此時執行命令報錯:

D:\Python\Lib\site-packages>pip install --upgrade pip
Traceback (most recent call last):
  File "d:\python\lib\runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "d:\python\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "D:\Python\Scripts\pip.exe\__main__.py", line 4, in <module>
ModuleNotFoundError: No module named 'pip'

最終解決方法,先執行 python -m ensurepip 命令,再升級pip。

D:\Python\Lib\site-packages>python -m ensurepip
WARNING: Ignoring invalid distribution -ip (d:\python\lib\site-packages)
WARNING: Ignoring invalid distribution -ip (d:\python\lib\site-packages)
Looking in links: c:\Users\EDISON~1\AppData\Local\Temp\tmphubo0umq
Requirement already satisfied: setuptools in d:\python\lib\site-packages (56.0.0)
Processing c:\users\edisonyao\appdata\local\temp\tmphubo0umq\pip-21.1.1-py3-none-any.whl
WARNING: Ignoring invalid distribution -ip (d:\python\lib\site-packages)
Installing collected packages: pip
WARNING: Ignoring invalid distribution -ip (d:\python\lib\site-packages)
Successfully installed pip-21.1.1
D:\Python\Lib\site-packages>python -m pip install --upgrade pip
WARNING: Ignoring invalid distribution -ip (d:\python\lib\site-packages)
WARNING: Ignoring invalid distribution -ip (d:\python\lib\site-packages)
Requirement already satisfied: pip in d:\python\lib\site-packages (21.1.1)
Collecting pip
  Using cached pip-23.0.1-py3-none-any.whl (2.1 MB)
WARNING: Ignoring invalid distribution -ip (d:\python\lib\site-packages)
Installing collected packages: pip
  Attempting uninstall: pip
    WARNING: Ignoring invalid distribution -ip (d:\python\lib\site-packages)
    Found existing installation: pip 21.1.1
    Uninstalling pip-21.1.1:
      Successfully uninstalled pip-21.1.1
WARNING: Ignoring invalid distribution -ip (d:\python\lib\site-packages)
Successfully installed pip-23.0.1
WARNING: Ignoring invalid distribution -ip (d:\python\lib\site-packages)
WARNING: Ignoring invalid distribution -ip (d:\python\lib\site-packages)
WARNING: Ignoring invalid distribution -ip (d:\python\lib\site-packages)

二、命令釋義

  ensurepip 包為在已有的Python安裝例項或虛擬環境中引導 pip 安裝器提供了支援。需要使用引導才能使

用pip的這一事實也正好反映了 pip 是一個獨立的專案,有其自己的釋出週期,其最新版本隨CPython直譯器的

維護版本和新特性版本一同捆綁。

  在大多數情況下,Python的終端使用者不需要直接呼叫這個模組( pip 預設應該已被引導),不過,如果

在安裝Python(或建立虛擬環境)時跳過了安裝 pip 步驟,或者使用過程中解除安裝了 pip ,則需要使用這個模組。

  最常用的呼叫命令: python -m ensurepip ,其含義是在未安裝pip的情況下安裝pip,若已安裝則不做任

何處理。

相關文章