Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find which pip package owns a file?

Tags:

I have a file that I suspect was installed by pip. How can I find which package installed that file?

In other words, I'm looking for a command similar to pacman -Qo filename or dpkg -S filename, but for pip. Does it exist? Or should I use some combination of pip and grep? In that case, I don't know how to list all the file installed.

like image 749
DdD Avatar asked Nov 02 '15 17:11

DdD


People also ask

How do I check my pip packages?

To do so, we can use the pip list -o or pip list --outdated command, which returns a list of packages with the version currently installed and the latest available. On the other hand, to list out all the packages that are up to date, we can use the pip list -u or pip list --uptodate command.

Where does pip find packages?

The pip command looks for the package in PyPI, resolves its dependencies, and installs everything in your current Python environment to ensure that requests will work. The pip install <package> command always looks for the latest version of the package and installs it.

How do I see all pip modules?

There are three ways to get the list of all the libraries or packages or modules installed in python using pip list command, pip freeze command and help function . This will list all the modules installed in the system .


2 Answers

You could try with

pip list | tail -n +3 | cut -d" " -f1 | xargs pip show -f | grep "filename"

Then search through the results looking for that file.

like image 73
JRD Avatar answered Oct 20 '22 03:10

JRD


You can use a python script like this:

#!/usr/bin/env python

import sys
try:
    from pip.utils import get_installed_distributions
except ModuleNotFoundError:
    from pip._internal.utils.misc import get_installed_distributions

MYPATH=sys.argv[1]
for dist in get_installed_distributions():
    # RECORDs should be part of .dist-info metadatas
    if dist.has_metadata('RECORD'):
        lines = dist.get_metadata_lines('RECORD')
        paths = [l.split(',')[0] for l in lines]
    # Otherwise use pip's log for .egg-info's
    elif dist.has_metadata('installed-files.txt'):
        paths = dist.get_metadata_lines('installed-files.txt')
    else:
        paths = []

    if MYPATH in paths:
        print(dist.project_name)

Usage looks like this:

$ python lookup_file.py requests/__init__.py 
requests

I wrote a more complete version here, with absolute paths:

https://github.com/nbeaver/pip_file_lookup

like image 26
Nathaniel M. Beaver Avatar answered Oct 20 '22 01:10

Nathaniel M. Beaver