Windows 10升级pip时出现“访问被拒绝”的错误提示

9

我已经完成了 Windows 10 的全新安装,并安装了 Python、Cygwin 和一个改进的控制台 ConEmu。在安装 Python 3.4.3 后,我执行了以下操作:

pip install -U pip

然后遇到了这个错误。

File "C:\Anwendungsentwicklung\Python34\lib\site-packages\pip\utils\__init__.py", line 70, in rmtree_errorhandler
  os.makedirs(path)
PermissionError: [WinError 5] Zugriff verweigert:  'C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\pip-dxm8d3xg-uninstall\\anwendungsentwicklung\\python34\\scripts\\pip.exe'

我使用预定义的管理员帐户登录,临时目录和Python安装目录(C:\Anwendungsentwicklung\Python34)都具有完全访问权限。

我已经尝试设置不同的权限,但Windows不允许我这样做。我甚至在安全选项卡中添加了“每个人”,但这并没有帮助,尽管我记得在Windows 7中使用这种“技巧”是有效的。这一定是Windows 10的问题。有人可以帮忙吗?

enter image description here


这是完整的跟踪信息:

  Exception:
Traceback (most recent call last):
File "c:\anwendungsentwicklung\python34\lib\shutil.py", line 372, in  _rmtree_unsafe
  os.unlink(fullname)
PermissionError: [WinError 5] Zugriff verweigert:  'C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\pip-k7g0hd6t- uninstall\\anwendungsentwicklung\\python34\\scripts\\pip.exe'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "c:\anwendungsentwicklung\python34\lib\site-packages\pip\basecommand.py", line 232, in main
  logger.critical('Operation cancelled by user')
File "c:\anwendungsentwicklung\python34\lib\site-packages\pip\commands\install.py", line 347, in run
  ensure_dir(options.target_dir)
File "c:\anwendungsentwicklung\python34\lib\site-packages\pip\req\req_set.py", line 560, in install
  missing_requested = sorted(
File "c:\anwendungsentwicklung\python34\lib\site-packages\pip\req\req_install.py", line 677, in commit_uninstall
  logger.debug(
File "c:\anwendungsentwicklung\python34\lib\site-packages\pip\req\req_uninstall.py", line 153, in commit
  self.save_dir = None
File "c:\anwendungsentwicklung\python34\lib\site-packages\pip\utils\__init__.py", line 58, in rmtree
  SUPPORTED_EXTENSIONS = ZIP_EXTENSIONS + TAR_EXTENSIONS
File "c:\anwendungsentwicklung\python34\lib\shutil.py", line 484, in rmtree
  return _rmtree_unsafe(path, onerror)
File "c:\anwendungsentwicklung\python34\lib\shutil.py", line 368, in _rmtree_unsafe
  _rmtree_unsafe(fullname, onerror)
File "c:\anwendungsentwicklung\python34\lib\shutil.py", line 368, in _rmtree_unsafe
  _rmtree_unsafe(fullname, onerror)
File "c:\anwendungsentwicklung\python34\lib\shutil.py", line 368, in _rmtree_unsafe
  _rmtree_unsafe(fullname, onerror)
File "c:\anwendungsentwicklung\python34\lib\shutil.py", line 376, in _rmtree_unsafe
  print(fullname)
File "c:\anwendungsentwicklung\python34\lib\site-packages\pip\utils\__init__.py", line 70, in rmtree_errorhandler
  try:
PermissionError: [WinError 5] Zugriff verweigert: 'C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\pip-k7g0hd6t-uninstall\\anwendungsentwicklung\\python34\\scripts\\pip.exe'

现在我在 "c:\anwendungsentwicklung\python34\lib\shutil.py" 中添加了一个断点:

# version vulnerable to race conditions
def _rmtree_unsafe(path, onerror):
try:
    if os.path.islink(path):
        # symlinks to directories are forbidden, see bug #1669
        raise OSError("Cannot call rmtree on a symbolic link")
except OSError:
    onerror(os.path.islink, path, sys.exc_info())
    # can't continue even if onerror hook returns
    return
names = []
try:
    names = os.listdir(path)
except OSError:
    onerror(os.listdir, path, sys.exc_info())
for name in names:
    fullname = os.path.join(path, name)
    try:
        mode = os.lstat(fullname).st_mode
    except OSError:
        mode = 0
    if stat.S_ISDIR(mode):
        _rmtree_unsafe(fullname, onerror)
    else:
        try:
            #import pdb
            os.unlink(fullname)
            #pdb.set_trace()
        except OSError:
            import pdb; pdb.set_trace()
            print(fullname)
            import getpass
            print(getpass.getuser())
            onerror(os.unlink, fullname, sys.exc_info())
try:
    os.rmdir(path)
except OSError:
    onerror(os.rmdir, path, sys.exc_info())

当我执行时

os.unlink(fullname) # 'C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\pip-k7g0hd6t- uninstall\\anwendungsentwicklung\\python34\\scripts\\pip.exe'

我遇到了这个PermissionError错误,因此我使用以下命令检查了该.exe文件的权限:

oct(os.stat(fullname)[ST_MODE])

它返回:'0o100777'

如果我没错的话,这意味着每个人(所有者、组和其他人)都拥有完全权限。

我很沮丧 :/ 有人有主意吗?


你试过以管理员身份运行该命令吗? - poke
这不是你的ConEmu控制台——我刚刚升级了,使用标准的Cygwin控制台也遇到了同样的错误。 - Thane Plummer
是的,我尝试过了,但是问题依旧。 - FalloutBoy
1
@poke,“管理员”帐户(SID S-1-5-21-...-500)默认情况下被禁用,因为它不受UAC的限制。它使用无限制令牌登录,该令牌启用了BUILTIN\Administrators(S-1-5-32-544)组,具有提升的(High)完整性标签(S-1-16-12288)和完整的管理员特权,例如SeTakeOwnershipPrivilege。 - Eryk Sun
如果pip.exe正在运行,则该文件将被内存管理器映射到内存中,直到进程退出或被强制终止之前,该文件无法被删除。 - Eryk Sun
显示剩余2条评论
5个回答

13

此处所讨论的,这是 Windows 的限制。简言之,pip.exe 文件正在使用中,因而被锁定,不能被删除。请使用python -m pip install --upgrade pip


请通过编辑您的问题而不是将其作为答案(它不是答案)来提供额外信息。 - Roland Smith
@Roland Smith 在其他帖子中看到过这个问题,很抱歉。这个问题已经被我解决了。这是一个Windows的限制,目前还没有修复。使用python -m pip install --upgrade pip进行更新。在这里讨论:https://github.com/pypa/pip/issues/1299总结:该文件被锁定/正在使用中,因此无法删除。感谢大家! - FalloutBoy
@RolandSmith 这个问题是“有人可以帮忙吗?”,而这篇文章就是对那个问题的回答。 - Nic Szerman

6

在相同的情况下,我的首选是使用管理员权限启动控制台。您可以从开始菜单中执行此操作,或者如果您正在使用ConEmu / Cmder,则只需以管理员身份运行新实例/选项卡。


只是为了澄清:打开开始菜单,右键单击“命令提示符”,选择“更多”,点击“以管理员身份运行”。 - Don O'Donnell
谢谢。这很有帮助! - Pirate X

1

我也遇到过同样的问题。如果你右键点击运行命令提示符并选择“以管理员身份运行”,那么它会起作用。


1
尝试使用这个。
python -m pip install --user --upgrade pip

0
我们都知道并且曾经为这个错误感到沮丧。虽然这个问题几乎无法解决,但有一个选项,在 Windows 11 Home 上对我有效。Virtualenv pip 升级可能无法正确工作,甚至使用 python -m pip install --upgrade pip 也不行。这对我没有用,所以我在我的系统上尝试了以下方法,检查是否可以在本地的 pip 上升级 pip(而不是虚拟环境)。
PS C:\Windows\System32 >python -m pip install --upgrade pip

Requirement already satisfied: pip in (...)
Collecting pip
... Using cached pip-22.0.2-py3-none-any.whl (2.1 MB)
.. Installing collected packages: pip

(...)

Attempting uninstall: pip
Founded existing installation: pip 21.3.1
Uninstall pip-21.3.1:
... Successfully uninstalled (...)
ERROR: Could not install packages due to an OSError: [WinError 5] Access is Denied: 'C:\\Users\\USER\\AppData\\Local\\Temp\\pip-uninstall-8ob_krif\'
Consider using the '--user' option or check the permissions.
(...)
[WinError 5] Access Denied
(...)

PS C:\Windows\System32 >python -m pip install --upgrade pip


(...) [Summary: it says pip is on the latest version]

总之,当我再次运行命令以检查pip是否实际升级时,错误信息是虚假的,我是正确的。pip已经升级了。它只是说有一个错误,但似乎它实际上已经升级了。也许是因为Python并不知道它实际上已经升级了。有些问题。

总结一下,pip实际上已经升级了,但这个事实并不为Python或pip本身所知。而且这个错误只是虚假的,或者代码中尝试访问另一个目录时出现了错误,需要提权操作。 - Heewoon

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接