Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calling from within Python #81

Open
fladd opened this issue Nov 6, 2020 · 9 comments
Open

Calling from within Python #81

fladd opened this issue Nov 6, 2020 · 9 comments

Comments

@fladd
Copy link

fladd commented Nov 6, 2020

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

@raimon49
Copy link
Owner

raimon49 commented Nov 7, 2020

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
Copy link
Author

fladd commented Nov 9, 2020

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
Copy link
Owner

raimon49 commented Nov 9, 2020

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
Copy link

sweigert commented Jan 4, 2021

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
Copy link

diazona commented Jan 17, 2021

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
Copy link

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
Copy link

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
Copy link

pwuertz commented Mar 5, 2024

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants