raimon49 / pip-licenses

Dump the license list of packages installed with pip.
MIT License
307 stars 43 forks source link

Retrieve licenses from other environment #149

Closed ClementPinard closed 1 year ago

ClementPinard commented 1 year ago

Somehow related to #107

Currently, we can only get packages from the environments we are running pip-licenses on. This is a problem for two reasons

I think there is a simple fix to this.

Indeed, thepckages are discovered via importlib distributions() function. As it can be seen here : https://github.com/raimon49/pip-licenses/blob/master/piplicenses.py#L193

By looking at the documentation and code of importlib, it seems to me that distributions should accept a path argument with a list of folders to search into. See https://importlib-metadata.readthedocs.io/en/latest/api.html#importlib_metadata.distributions

and https://github.com/python/importlib_metadata/blob/700f2c7d74543e3695163d5487155b92e6f04d65/importlib_metadata/__init__.py#L809

distributions(**kwargs) calls Distribution.discover(**kwargs) which expects either a Context object or its args for creating one (see https://github.com/python/importlib_metadata/blob/700f2c7d74543e3695163d5487155b92e6f04d65/importlib_metadata/__init__.py#L387 )

All in all, calling distributions([folder_path]) will retrieve all packages metadata in the given folder_path which could be a CLI parameter.

What do you think ?