Skip to content

Instantly share code, notes, and snippets.

@pszemraj
Created March 23, 2024 02:26
Show Gist options
  • Save pszemraj/d420060360253406b747ffc6eefadf61 to your computer and use it in GitHub Desktop.
Save pszemraj/d420060360253406b747ffc6eefadf61 to your computer and use it in GitHub Desktop.
find local package meta dependencies
import pkg_resources
def list_dependencies(package_name, level=0, explored=set()):
# Define indent outside of try-except to ensure it's always assigned
indent = " " * level
if package_name in explored:
return
explored.add(package_name)
try:
# Get the package's distribution and its direct dependencies
distribution = pkg_resources.get_distribution(package_name)
dependencies = distribution.requires()
print(f"{indent}- {package_name}")
# Recursively list the dependencies of each dependency
for dep in dependencies:
dep_key = dep.key
list_dependencies(dep_key, level + 1, explored)
except pkg_resources.DistributionNotFound:
# Correctly handle and display packages that are not found
print(f"{indent}- {package_name} (not found)")
# List of your primary dependencies
primary_dependencies = [
"importlib_metadata",
"lxml",
"numpy",
"scikit_learn",
"setuptools",
"tensorflow",
"torch",
"tqdm",
"transformers",
]
for package in primary_dependencies:
list_dependencies(package, explored=set())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment