pypa / pip

The Python package installer
https://pip.pypa.io/
MIT License
9.51k stars 3.02k forks source link

pip uninstall failing on Windows 10 #8274

Closed Awful-Leo closed 2 years ago

Awful-Leo commented 4 years ago

Environment

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.

McSinyx commented 4 years ago

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 commented 4 years ago

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. :)

pfmoore commented 4 years ago

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.

Awful-Leo commented 4 years ago

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.

pradyunsg commented 4 years ago

@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 commented 4 years ago

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 commented 4 years ago

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

https://github.com/pypa/pip/blob/cbdf5b1b6026a871d4b1afa564fbb15375785fb9/src/pip/_internal/commands/install.py#L340

The conditional here is wrong. :)

Awful-Leo commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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 commented 3 years ago

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 commented 3 years ago

@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 commented 3 years ago

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 commented 3 years ago

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

uranusjr commented 3 years ago

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.