matt wilkie
matt wilkie

Reputation: 18084

Where is Pip's list of files to remove for a package

Where does pip keep a record of what files to remove when uninstall a package?

I have an application package which is installed from pip, often in editable mode. For convenience after install there are user scripts to add desktop and menu shortcuts. How do I tell pip and other package managers about these extra files so they can be automatically removed when uninstalled?

Typical install scenario:

git clone {application}
pip install --editable path\to\myapp-code
python user-scripts\make-menu-shortcuts.py

cross posted to https://discuss.python.org/t/how-to-add-to-list-of-files-to-uninstall .

Upvotes: 0

Views: 982

Answers (1)

matt wilkie
matt wilkie

Reputation: 18084

draft answer in progress. If you have something better than this, jump in!

Wheel installs

Look for PYTHONHOME/Lib/site-packages/{package}{version}.dist-info. In there is a file called RECORD.The Wheel specification has some details on the RECORD format: https://www.python.org/dev/peps/pep-0427/#the-dist-info-directory

This directory isn't created for packages installed in editable mode (pip install --editable path/to/code).

# extract from *.dist-info/RECORD:
../../Scripts/myapp.exe,sha256=tQaANRLxdJ3Su3vLNakbzlNhRtnU-HBhdwHGTpJHTxc,103271
myapp-0.1.20.dist-info/INSTALLER,sha256=zuuue4knoyJ-UwPPXg8fezS7VCrXJQrAP7zeNuwvFQg,4
...
myapp/__pycache__/__init__.cpython-36.pyc,,
myapp/__pycache__/_version.cpython-36.pyc,,

Pip uninstall will remove any files at the paths we add to this file. The hash isn't necessary but take care to append trailing commas if not used.

Windows: it's okay to use native format (C:\users\...\myapp.lnk) but files recorded must exist on same drive as dist-info (ref).

# myapp.dist-info/RECORD:
myapp-0.1.20.dist-info/INSTALLER,sha256=zuuue4knoyJ-UwPPXg8fezS7VCrXJQrAP7zeNuwvFQg,4
...
myapp/__pycache__/__init__.cpython-36.pyc,,
myapp/__pycache__/_version.cpython-36.pyc,,
C:\Users\me\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\myapp.lnk,,
> pip uninstall myapp
Uninstalling myapp-0.1.20:
  Would remove:
    c:\tools\miniconda3\envs\test\lib\site-packages\myapp-0.1.20.dist-info\*
    c:\tools\miniconda3\envs\test\lib\site-packages\myapp\*
    c:\tools\miniconda3\envs\test\scripts\myapp.exe
    c:\users\me\appdata\roaming\microsoft\windows\start menu\programs\myapp.lnk
Proceed (y/n)? y

Editable installs

Look in PYTHONHOME/lib/site-packages for myapp.egg-link. That file contains a path to the code location:

D:\code-external\app-code
.

In that folder look for myapp.egg-info. It has a similar structure to dist-info but is not the same.

Running pip show --files myapp yields an error:

Name: myapp
...
Files:
Cannot locate installed-files.txt

Creat .egg-info/installed-files.txt and put the extra filename in it. Now pip show works, but unfortunately pip uninstall still misses it.

> pip show -f myapp
Name: myapp
...
Files:
..\..\test-link.lnk

> pip uninstall myapp
Uninstalling myapp-0.1.20:
  Would remove:
    c:\tools\miniconda3\envs\test\lib\site-packages\myapp.egg-link
    c:\tools\miniconda3\envs\test\scripts\myapp-script.py
    c:\tools\miniconda3\envs\test\scripts\myapp.exe
Proceed (y/n)? n

Notes

Figured dist-info/RECORD part out by creating a bare virtual conda environment, noting install time, installing a single small package with pip, and then used Windows Advanced Query Syntax to search files modified after timestamp locate what was changed within the envs folder.

installed-files.txt: On Windows the paths must be on same drive as the egg-info folder. Pip show always resolves the path as relative, but that's impossible across drives in Windows. You've hit this bug if get the error ValueError: path is on mount 'C:', start on mount 'D:'

Upvotes: 1

Related Questions