raimon49 / pip-licenses

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

Calling from within Python #81

Open fladd opened 3 years ago

fladd commented 3 years ago

The documentation currently does not explain how to call pip-licenses from within Python, only how to call it from a command line.

raimon49 commented 3 years ago

Yes, pip-licenses is developed as a CLI tool.

Does your opinion mean that you want to import pip-licenses as a module and use some of the features in your Python code?

fladd commented 3 years ago

Yes, that would be great. Currently I get around it by calling it with subprocess and then parsing the json output, but this seems a bit unnecessary, given that pip-licenses is Python code itself.

raimon49 commented 3 years ago

I didn't imagine anyone would want to use the scan results in their Python code.

I would consider APIs and documentation that users can handle with structured objects.

Thank you for your valuable opinion, @fladd .

sweigert commented 3 years ago

Yes, that would be great. Currently I get around it by calling it with subprocess and then parsing the json output, but this seems a bit unnecessary, given that pip-licenses is Python code itself.

This is what I am doing to achieve this:

import piplicenses
parser = piplicenses.create_parser()
args = parser.parse_args()
packages = piplicenses.get_packages(args)
for p in packages:
   print(p)

If you need options, you could provide them in the call to parse_args.

diazona commented 3 years ago

This would also be useful to work around the lack of "proper" support for multiply-licensed packages - that is, the fact that multiple licenses are shown in a single string separated by delimiters, rather than as some structured representation (like a JSON list, for the JSON output format). If there were a Python API, perhaps it could expose multiple licenses as a Python list.

I don't know how practical this actually is, but it would make a significant difference to my use case.

bartfeenstra commented 3 years ago

I'd like to express my support for this feature request. I'm currently using pip-licenses in a unit test to ensure all dependencies' licenses are compatible. A pip-licenses Python API would allow this code to be rewritten a lot shorter and cleaner.

stefan6419846 commented 1 year ago

I stumbled upon this recently as well when trying to re-use some functionality which is only available as a nested method like get_pkg_included_file, for now indicating that CLI usage is indeed the usual one as mentioned inside this issue.

pwuertz commented 6 months ago

A pip-licenses Python API would be a great fit for applications bundled with e.g. PyInstaller.

Using piplicenses within a PyInstaller-specfile, you could fully integrate the licence collection process right into your application bundling process.

stefan6419846 commented 6 months ago

I previously used packages = list(piplicenses.get_packages(from_source=piplicenses.FromArg.MIXED)) to get the packages from within Python without relying on a subprocess.

Due to additional requirements from my side (matching all license files, only querying metadata without files etc.) and the upstream repository receiving corresponding PRs which would never end up in a release, I decided to create my own fork of this tool which has a strict focus on library usage: https://github.com/stefan6419846/pip-licenses-lib

Lewik commented 2 months ago

Yes, that would be great. Currently I get around it by calling it with subprocess and then parsing the json output, but this seems a bit unnecessary, given that pip-licenses is Python code itself.

This is what I am doing to achieve this:

import piplicenses
parser = piplicenses.create_parser()
args = parser.parse_args()
packages = piplicenses.get_packages(args)
for p in packages:
   print(p)

If you need options, you could provide them in the call to parse_args.

parser.parse_args() without arguments will use sys.argv pass []