Search code examples
pythonpython-2.7pipsetuptoolspkg-resources

find pip packages: pkg_resources specify custom target directory


Is there any way to specify a custom target directory for pkg_resources to list pip packages? I want to be able to find packages which have been installed in a custom target directory using --target through something like pkg_resources.require() that already fed with a custom target directory.

What I don't want is to use:

  • setuptools.find_packages as it's only using sys.path
  • setuptools.PEP420PackageFinder.find
  • Finding packages through traversing the directory names and files was not helping as well, as I need a more generic way of finding package namespaces.

I appreciate any help on this.


Solution

  • Update

    Python 3.8 introduced importlib.metadata, a module for querying the installed packages that supersedes pkg_resources. Example usage:

    In [1]: from importlib import metadata
    In [2]: dists = metadata.distributions(path=['my_target_dir'])
    In [3]: list(f"{d.metadata['Name']}=={d.metadata['Version']}" for d in dists)
    Out[22]: 
    ['pip==20.0.2',
     'ipython==7.13.0',
     ...
    ]
    

    For Python 2.7 and Python >=3.5, there's a backport called importlib-metadata:

    $ pip install importlib-metadata
    

    Original answer

    The pkg_resources.find_distributions function (documented under Getting or Creating Distributions) accepts a target dir to search for packages in. Example:

    $ ls -l my_target_dir/
    total 36
    drwxr-xr-x 2 hoefling hoefling 4096 May 17 13:29 __pycache__
    -rw-r--r-- 1 hoefling hoefling  126 May 17 13:29 easy_install.py
    drwxr-xr-x 5 hoefling hoefling 4096 May 17 13:29 pip
    drwxr-xr-x 2 hoefling hoefling 4096 May 17 13:29 pip-10.0.1.dist-info
    drwxr-xr-x 5 hoefling hoefling 4096 May 17 13:29 pkg_resources
    drwxr-xr-x 6 hoefling hoefling 4096 May 17 13:29 setuptools
    drwxr-xr-x 2 hoefling hoefling 4096 May 17 13:29 setuptools-39.1.0.dist-info
    drwxr-xr-x 5 hoefling hoefling 4096 May 17 13:29 wheel
    drwxr-xr-x 2 hoefling hoefling 4096 May 17 13:29 wheel-0.31.1.dist-info
    

    Scanning my_target_dir with pkg_resources.find_distributions yields:

    In [2]: list(pkg_resources.find_distributions('my_target_dir'))
    Out[2]:
    [wheel 0.31.1 (/data/gentoo64/tmp/so-50380624/my_target_dir),
     setuptools 39.1.0 (/data/gentoo64/tmp/so-50380624/my_target_dir),
     pip 10.0.1 (/data/gentoo64/tmp/so-50380624/my_target_dir)]