pip icon indicating copy to clipboard operation
pip copied to clipboard

Unable to uninstall packages if site-packages is a symlink

Open EpicWink opened this issue 5 years ago • 4 comments

Environment

  • pip version: 20.1.1
  • Python version: 3.7.5
  • OS: Ubuntu 19.10
  • virtualenv: 20.0.21

Description

If the site-packages directory has been moved (in my case to a large disk drive) and then symbolically linked to, package installation and importing works fine. However, when trying to uninstall a package, pip sees the package outside of the environment, and skips its uninstallation.

Expected behavior

Successful uninstallation

How to Reproduce

  1. Create a virtual environment
  2. Move the site-packages directory to an external location, and symbolically link to it in its place
  3. Install any package in that environment
  4. Uninstall that package in that environment
VENV_DIR=/media/data/venv-data/
virtualenv -v .venv
mv -vT .venv/lib ${VENV_DIR}tmp_lib
ln -vrs ${VENV_DIR}/tmp_lib .venv/lib

source .venv/bin/activate
pip install requests
pip uninstall requests
python -c "import requests; assert not requests.get"

Output

(.venv) ~/tmp/ $ pip uninstall requests
Found existing installation: requests 2.24.0
Not uninstalling requests at /media/data/venv-data/tmp_lib/python3.7/site-packages, outside environment ~/tmp/.venv
Can't uninstall 'requests'. No files were found to uninstall.
(.venv) ~/tmp/ $ python -c "import requests; assert not requests.get"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
AssertionError

EpicWink avatar Jul 06 '20 01:07 EpicWink

Since the real location of the site-packages is outside of the virtual environment root, pip has no way to tell whether it can safely be manipulated. It chooses the safe route, ignoring the directory and leave the responsibility to the user. Maybe we can add a flag to force pip into doing the uninstallation, but this safe behaviour should still be the default.

uranusjr avatar Jul 06 '20 06:07 uranusjr

Maybe we can add a flag to force pip into doing the uninstallation, but this safe behaviour should still be the default.

I agree. Allowing a user to override safety precautions sometimes has its benefits.

Alternatively, #40 suggested to add the flag --disable-followlinks (to not resolve symbolic links), but for the use-case in the issue pip's behaviour was changed instead.

EpicWink avatar Jul 06 '20 07:07 EpicWink

Did a workaround or resolution ever get reached on this?

snova-connorm avatar Apr 25 '24 21:04 snova-connorm

No, but a PR implementing a possible solution like https://github.com/pypa/pip/issues/8549#issuecomment-654044382 would be welcomed!

The current workaround is that you need to delete the package's files yourself. The error Not uninstalling <pkg> at <pkg-location>, outside environment <environment-location> lets you know what directory to locate the package's files in (make sure to delete the package's directory and its accompanying .dist-info as well).

ichard26 avatar Apr 25 '24 21:04 ichard26