Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pip uninstall failing on Windows 10 #8274

Closed
Awful-Leo opened this issue May 20, 2020 · 15 comments · Fixed by #10560
Closed

pip uninstall failing on Windows 10 #8274

Awful-Leo opened this issue May 20, 2020 · 15 comments · Fixed by #10560
Labels
C: uninstall The logic for uninstallation of packages type: bug A confirmed bug or unintended behavior

Comments

@Awful-Leo
Copy link

Environment

  • pip version: latest from Python.org as of 2020-0520-1850
  • Python version: 3.8.3
  • OS:Windows 10

Description

  1. Download the latest version of python (together with pip) from python.org.
  2. update pip in cmd with "pip install pip -U"
  3. pip downloads the latest pip (ver 20.1?), then starts uninstalling my local pip(successful), and tries to install the just-downloaded version. Oops! no permission! Then I open up another cmd window with admin permission, and run "pip install pip -U". No! Because "No module named 'pip'".

Expected behavior

When uninstalling pip, temporarily stores the old pip to a temp folder maybe. If the installing of the newer version is successful, delete the temp file; if not, roll back and reinstall the original version.

How to Reproduce

Follow the above.

@triage-new-issues triage-new-issues bot added the S: needs triage Issues/PRs that need to be triaged label May 20, 2020
@McSinyx
Copy link
Contributor

McSinyx commented May 20, 2020

Hi! IMHO this should be done for all packages, i.e. if install will fail then after uninstall we should have something to revert to. For the permission case only GH-6762 might fix it.

@pradyunsg
Copy link
Member

When uninstalling pip, temporarily stores the old pip to a temp folder maybe. If the installing of the newer version is successful, delete the temp file; if not, roll back and reinstall the original version.

pip uninstall does this already. Please provide the steps for reproductions along with the output of the commands, to help diagnose why that's not happening in your case.

Note that not all of pip's developers have access to the all OSes, so the output of the steps, are especially relevant and helpful. :)

@pradyunsg pradyunsg added C: uninstall The logic for uninstallation of packages S: awaiting response Waiting for a response/more information type: bug A confirmed bug or unintended behavior labels May 20, 2020
@triage-new-issues triage-new-issues bot removed the S: needs triage Issues/PRs that need to be triaged label May 20, 2020
@pfmoore
Copy link
Member

pfmoore commented May 20, 2020

Also,

update pip in cmd with "pip install pip -U"

The recommended way of updating pip on Windows is py -m pip install -U pip. If you do pip install -U pip you will get a permission error because the install can't update the running pip.exe wrapper.

This may or may not be relevant to the issue you're reporting, but it would be helpful if you could update your description of how to reproduce the issue so that it uses the supported approach.

@pradyunsg pradyunsg changed the title let there be a going back of pip update. pip uninstall fails on WIndows 10 May 20, 2020
@Awful-Leo
Copy link
Author

Awful-Leo commented May 22, 2020

OK, I just did it again on the newly updated windows 10 (10.0.19041) with python-3.8.2-amd64.exe.

Here's what happened after the installation from powershell.

PS C:\Users\Leo> pip -V
pip 19.2.3 from c:\program files\python38\lib\site-packages\pip (python 3.8)
PS C:\Users\Leo> pip install pip -U
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting pip
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/43/84/23ed6a1796480a6f1a2d38f2802901d078266bda38388954d01d3f2e821d/pip-20.1.1-py2.py3-none-any.whl (1.5MB)
     |████████████████████████████████| 1.5MB 2.2MB/s
Installing collected packages: pip
  Found existing installation: pip 19.2.3
    Uninstalling pip-19.2.3:
ERROR: Could not install packages due to an EnvironmentError: [WinError 5] 拒绝访问。: 'c:\\program files\\python38\\lib\\site-packages\\pip-19.2.3.dist-info\\entry_points.txt'
Consider using the `--user` option or check the permissions.

WARNING: You are using pip version 19.2.3, however version 20.1.1 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.
PS C:\Users\Leo>

so I opened another powershell window with admin privilege.

PS C:\WINDOWS\system32> pip install pip -U
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting pip
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/43/84/23ed6a1796480a6f1a2d38f2802901d078266bda38388954d01d3f2e821d/pip-20.1.1-py2.py3-none-any.whl (1.5MB)
     |████████████████████████████████| 1.5MB 2.2MB/s
Installing collected packages: pip
  Found existing installation: pip 19.2.3
    Uninstalling pip-19.2.3:
      Successfully uninstalled pip-19.2.3
ERROR: Could not install packages due to an EnvironmentError: [WinError 5] 拒绝访问。: 'C:\\Users\\Leo\\AppData\\Local\\Temp\\pip-uninstall-17mx4r60\\pip.exe'
Consider using the `--user` option or check the permissions.
PS C:\WINDOWS\system32> pip install pip -U
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Requirement already up-to-date: pip in c:\program files\python38\lib\site-packages (20.1.1)

OK...the update worked although there were some errors, and I'm not sure which step I did different from the last one.

I'll try to reproduce the issue again on my other laptop at office next week.

Thank you all again.

@no-response no-response bot removed the S: awaiting response Waiting for a response/more information label May 22, 2020
@pradyunsg pradyunsg changed the title pip uninstall fails on WIndows 10 pip uninstall failing on Windows 10 May 23, 2020
@pradyunsg
Copy link
Member

@pfmoore This looks like the check for "is this affecting pip" needs to be improved, since we've had a change in how pip uninstall works? I'm a fair bit out of my water here though, so I'll defer to you and @uranusjr on this. :)

@uranusjr
Copy link
Member

uranusjr commented May 23, 2020

At this point maybe we should just add a check in install and uninstall, and hard error if pip is listed in the command line. (Clarify: I want to do this for all operating systems so tutorials/articles would be motivated to update.)

@pradyunsg
Copy link
Member

pradyunsg commented May 23, 2020

Uhm... I think I see the issue here:

modifying_pip = pip_req.satisfied_by is None

The conditional here is wrong. :)

@Awful-Leo
Copy link
Author

Just had another try.
OS: Windows 10 (Ver 1607 14393.3686) Enterprise Edition 2016 LTSB

Python:python-3.8.3-amd64.exe(downloaded from python.org)

  1. Install the executable for all users.
  2. Open up powershell (without admin privilege).
    ############################################################
    PS C:\Users\Leo Wang> pip -V
    pip 19.2.3 from d:\program files\python38\lib\site-packages\pip (python 3.8)
    PS C:\Users\Leo Wang> pip install pip -U
    Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
    Collecting pip
    Downloading https://pypi.tuna.tsinghua.edu.cn/packages/43/84/23ed6a1796480a6f1a2d38f2802901d078266bda38388954d01d3f2e821d/pip-20.1.1-py2.py3-none-any.whl (1.5MB)
    |████████████████████████████████| 1.5MB 57kB/s
    Installing collected packages: pip
    Found existing installation: pip 19.2.3
    Uninstalling pip-19.2.3:
    ERROR: Could not install packages due to an EnvironmentError: [WinError 5] 拒绝访问。: 'd:\program files\python38\scripts\pip.exe'
    Consider using the --user option or check the permissions.

PS C:\Users\Leo Wang> pip install pip -U
Traceback (most recent call last):
File "d:\program files\python38\lib\runpy.py", line 194, in _run_module_as_main
return _run_code(code, main_globals, None,
File "d:\program files\python38\lib\runpy.py", line 87, in run_code
exec(code, run_globals)
File "D:\Program Files\Python38\Scripts\pip.exe_main
.py", line 5, in
ModuleNotFoundError: No module named 'pip'
PS C:\Users\Leo Wang>
############################################################

  1. Now the only way to rescue my python env is to download "pip-20.1.1.tar.gz " from the following page: https://pypi.org/project/pip/#files and extract it and re-install pip with python setup.py install.

Pheeeeew! This is exactly what I was trying to let you guys when opening this issue.

@pfmoore, the reason why I'm using this very "pip install pip -U" is that I was trying to update the pip source with a mainland mirror in China, so I referred to the tuna page (https://mirrors.tuna.tsinghua.edu.cn/help/pypi/) which is owned and maintained by Tsinghua University. And it says to change the default mirror to tuna, the right way is to do these:
#######################
pip install pip -U
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
#######################

Anyway, please see if there's anything that can be bettered to avoid future users from stepping into this situation.

Thank you all.

@pradyunsg
Copy link
Member

Now the only way to rescue my python env is to download "pip-20.1.1.tar.gz " from the following page: pypi.org/project/pip/#files and extract it and re-install pip with python setup.py install.

A much better approach would be to download and run https://bootstrap.pypa.io/get-pip.py.

@Awful-Leo As @pfmoore noted, on Windows, you should do py -m pip install pip -U and not pip install pip -U. As I noted above, the conditional that is supposed to prevent users from doing pip install pip -U is incorrect and needs to be fixed.

@deveshks
Copy link
Contributor

As I noted above, the conditional that is supposed to prevent users from doing pip install pip -U is incorrect and needs to be fixed.

So is the conditional flipped and should be modifying_pip = pip_req.satisfied_by is not None , since if we are modifying pip, and pip is already installed, pip_req.satisfied_by will not be None?

@JulienPalard
Copy link

I don't know if it's directly related, but a student of mine encontered something like this, but in a venv:

D:\sup internet\python\oeis>python -m venv .venv && ".venv\Scripts\activate.bat"

(.venv) D:\sup internet\python\oeis>pip --version
pip 19.2.3 from d:\sup internet\python\oeis\.venv\lib\site-packages\pip (python 3.8)

(.venv) D:\sup internet\python\oeis>pip install --upgrade pip
Collecting pip
  Using cached https://files.pythonhosted.org/packages/cb/28/91f26bd088ce8e22169032100d4260614fc3da435025ff389ef1d396a433/pip-20.2.4-py2.py3-none-any.whl
Installing collected packages: pip
  Found existing installation: pip 19.2.3
    Uninstalling pip-19.2.3:
ERROR: Could not install packages due to an EnvironmentError: [WinError 5] Accès refusé: 'd:\\sup internet\\python\\oeis\\.venv\\scripts\\pip.exe'
Consider using the `--user` option or check the permissions.

"Accès refusé" mean "Access denied", but as it's in a freshly created venv, the pip.exe was freshly created with the same user and same session, and also freshly deleted by the uninstall, so it's probably not exactly an acess right error.

@pfmoore
Copy link
Member

pfmoore commented Nov 20, 2020

@JulienPalard On Windows, the OS locks a running executable so it can't be modified or deleted. So pip install --upgrade pip can't upgrade the pip.exe file, and you get "Access denied". The message is correct - access is denied because you're trying to modify a running exe.

The fix is, as per the recommendation in the documentation, to use the longer form py -m pip install --upgrade pip, which runs Python directly, not via the wrapper. So the wrapper isn't locked and the upgrade can replace it.

The discussion here is mostly about whether we can improve the error to explain what's happened better. It's annoyingly difficult to do so 🙁

@JulienPalard
Copy link

Thanks @pfmoore for this clear clarification!

IIRC, after trying the pip install --ugprade pip, the student did no longer have pip at all in its venv, which is a bit strange, I understand pip was unable to update it in place, but why was it able to remove it?

The student had hard time figuring this out, as "the pip command" would continue working after the deletion of .venv/Scripts/pip.exe: Windows was finding the "global pip", next on the PATH, itself trying to install things outside of the venv.

Enhancing the message would be a good start, but if it still delete pip.exe it may be a bit hard for a newcomer to recover?

@pradyunsg
Copy link
Member

The pip.exe shouldn't be getting deleted, unless Windows has started doing something new.

@uranusjr
Copy link
Member

Not entirely on topic, but I would suggest recommending newcomers to avoid pip.exe (and pip on POSIX) entirely in favour of python -m pip (or even better on Windows, py -m pip). The python executable is not likely to be removed (and the environment would be in big trouble if it is), and would work in all cases that the pip executable works. It also avoids pip install -U pip from breaking the workflow.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
C: uninstall The logic for uninstallation of packages type: bug A confirmed bug or unintended behavior
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants