Get License Information of Python Libraries

This can be done by the super useful pip-licenses library.
Here we go,
Get License info of all python libraries in your system
import os
import subprocess
import numpy as np
import pandas as pd
_ = subprocess.Popen("pip install pip-licenses".split(" "))
_.communicate()
cmd = "pip-licenses | awk -v OFS='\t' '{ print $1,$3}'"
libs = os.popen(cmd).read()
libs = [lib.split('\t') for lib in libs.split('\n') if len(lib)>0]
header = libs[0]
name = [x[0] for x in libs[1:]]
license_type = [x[1] for x in libs[1:]]
pd.DataFrame({'Name':name, 'License type':license_type}).drop_duplicates()
You can see that a shell command is being run using os.popen to get the license information. The same command can directly be run on the terminal.
License of Specific Library
For a specific package there are two options. Lets see this with terminal option.
Use pip
pip show numpy
You get
Name: numpy
Version: 1.20.3
Summary: NumPy is the fundamental package for array computing with Python.
Home-page: https://www.numpy.org
Author: Travis E. Oliphant et al.
Author-email:
License: BSD
Location: /Users/ml/opt/anaconda3/lib/python3.9/site-packages
Requires:
Required-by: xgboost, transformers, tifffile, tables, statsmodels, seaborn, scipy, scikit-learn, scikit-image, PyWavelets, pyerfa, pyarrow, patsy, pandas, numexpr, numba, mkl-random, mkl-fft, matplotlib, imageio, imagecodecs, h5py, gensim, datasets, daal4py, Bottleneck, bokeh, bkcharts, astropy
You can see that the license is BSD
Use pip-licenses
pip-licenses --packages numpy
So…that’s it!
But when would you ever need this???? When legal and compliance teams reach out to you to ensure that all production systems are built with components with right license privileges.