Skip to content

Instantly share code, notes, and snippets.

@agostof
Last active April 12, 2023 14:35
Show Gist options
  • Save agostof/8a76bad124bc18e8c53155d80f152d08 to your computer and use it in GitHub Desktop.
Save agostof/8a76bad124bc18e8c53155d80f152d08 to your computer and use it in GitHub Desktop.
List dependencies for a specific pip package without downloading it.
import requests
import sys
script_name = sys.argv[0]
USAGE = f'''Lists package dependencies for a pip package without downloading it.
Usage:
{script_name} package_name (or package_name==version)
e.g.
To print all versions and depenencies of a given package.
{script_name} pandas
To check dendencies of specific version.
{script_name} pandas==2.0.0
'''
# I created this simple script because:
# 1. Only wanted dependencies and versions for a SINGLE package.
# 2. Did not want to install additional packages.
# 3. Wanted something self-contained that did not requiere any downloads.
# Note: Ideas from the following SO quesiton were used:
# https://stackoverflow.com/questions/11147667/is-there-a-way-to-list-pip-dependencies-requirements
# If you need something more complex you can look into: pipdeptree, johnnydep, and other options listed above.
if len(sys.argv) < 2:
print(f"{USAGE}")
sys.exit(0)
package_input = sys.argv[1]
if len(sys.argv)==3:
last_count = int(sys.argv[2])
else:
last_count = 20
# splits package name and version, lazy way I know! :)
package_name, *package_version = package_input.split("==")
print(f"Input: {package_name} {package_version}")
def get_package_url(package, version=None):
if version:
if type(version)==list:
version = version[0]
#print("VERSIONED")
pack_url = f'https://pypi.org/pypi/{package}/{version}/json'
else:
#print("UNVERSIONED")
pack_url = f'https://pypi.org/pypi/{package}/json'
return pack_url
url = get_package_url(package_name, package_version)
pack_data = requests.get(url).json()
if 'message' in pack_data:
print(pack_data['message'])
print("Check package name.")
sys.exit(0)
# pack_data['urls']
dependencies = pack_data['info']['requires_dist']
python_version = pack_data['info']['requires_python']
from packaging.version import parse as parseVersion
if 'releases' in pack_data:
rel_count = len(pack_data['releases'].keys())
past_releases = list(pack_data['releases'].keys())
past_releases.sort(key=parseVersion)
last_n_releases = past_releases[-1:-1-last_count:-1]
last_n_releases_out = "\n".join(last_n_releases)
print(f"Releases count: {rel_count}")
print(f"Last {last_count} releases (reversed)\n{last_n_releases_out}")
# use the lastest package release version
if not package_version:
package_version = last_n_releases[0]
else:
package_version = package_version[0]
if dependencies:
out_deps = "\n".join(dependencies)
print(f'Dependencies for {package_name} {package_version}:\n{out_deps}')
else:
print(f"Can't find dependency details for: {package_name} {package_version}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment