pythonpip

Python: programmatically running "pip list"


I'm writing a bit of code that will report and reconcile differences between two pip-managed python installations.

How can I programmatically get the information provided by pip list without making a subprogram invocation of pip?


Solution

  • It's possible to get a list of packages programmatically:

    Options:

    A. _internal.main

    from pip import _internal
    _internal.main(['list'])
    

    This will print out three columns with Package. Version, and Location

    Note that usage of pip's internal api is not recommended.

    B. pkg_resources

    import pkg_resources
    print([p.project_name for p in pkg_resources.working_set])
    # note that this is same as calling pip._vendor.pkg_resources.working_set
    

    C. iter_modules

    Takes a long time to execute (~300ms on computer w/ I5 CPU, SSD, & 8 gigs ram). The benefit is that it will have a far more extensive list of modules and it will output importable names.

    Ex: python-dateutil is imported as dateutil, but iter_modules will give you the importable name: dateutil

    from pkgutil import iter_modules
    print([p.name for p in iter_modules()])
    

    D. Call pip in command line via subprocess

    The solution to this is trivial and I'll leave this as an exercise to the reader

    aka I'm too lazy to do this, good luck! :D