11
22import sys
3+ import os
34import subprocess
45import json
56
7+ os .environ ['PYTHONUTF8' ] = '1'
8+
69def in_venv ():
710 # Check for sys.real_prefix for older virtualenv compatibility
811 if hasattr (sys , 'real_prefix' ):
@@ -18,9 +21,9 @@ def list_packages() -> list[dict]:
1821 return json .loads (result .stdout )
1922
2023
21- def list_package_dependencies (package :str = None ) -> list [dict ]:
24+ def list_package_dependents (package :str = None ) -> list [dict ]:
2225 """
23- returns a list of higher-order packages that depend on this package.
26+ List what packages depend on the target package.
2427 """
2528 result = subprocess .run (['pip' , 'show' , package ], capture_output = True , text = True ).stdout
2629 for line in result .split ('\n ' ):
@@ -29,4 +32,19 @@ def list_package_dependencies(package:str=None) -> list[dict]:
2932 line = line .replace ('Required-by:' , '' )
3033 deps = list (filter (lambda dep : dep != '' , line .split (',' )))
3134 return deps
32- return []
35+ return []
36+
37+
38+ def list_user_installed_packages () -> list [str ]:
39+ _packages = []
40+
41+ result = subprocess .run (['pip' , 'inspect' , '--no-color' ], capture_output = True , text = True , errors = 'ignore' )
42+ pinfo = json .loads (result .stdout )
43+
44+ for package in pinfo ['installed' ]:
45+ if package ['requested' ] == True :
46+ package_name = package ['metadata' ]['name' ]
47+ package_version = package ['metadata' ]['version' ]
48+ _packages .append (f'{ package_name } =={ package_version } ' )
49+
50+ return sorted (_packages , key = str .lower )
0 commit comments