package_name:str, # The name of a python package
depth_limit:int=1, # How deep to follow nested dependencies
) -> dict: # A dictionary of {package:version}
"Recursively grabs dependencies of python package"
pkgs = pipdeptree.get_installed_distributions(local_only=False, user_only=False)
tree = pipdeptree.PackageDAG.from_pkgs(pkgs)
tree = tree.filter([package_name], None)
curr_depth=0
def _get_deps(j, dep_dict={}, curr_depth=0):
if curr_depth > depth_limit: return dep_dict
if isinstance(j, list):
for a in j:
_get_deps(a, dep_dict, curr_depth)
elif isinstance(j, dict):
if 'package_name' in j.keys():
if j['package_name'] not in dep_dict.keys() and j['package_name'] != package_name:
dep_dict[j['package_name']] = j['installed_version']
if 'dependencies' in j.keys():
curr_depth += 1
return _get_deps(j['dependencies'], dep_dict, curr_depth)
return dep_dict
return _get_deps(ast.literal_eval(pipdeptree.render_json_tree(tree, 4)), {})
Here is a new option to use that is better: