24 lines
923 B
Python
24 lines
923 B
Python
![]() |
import importlib
|
|||
|
import pkg_resources
|
|||
|
|
|||
|
def check_versions(requirements_file):
|
|||
|
with open(requirements_file, 'r', encoding='utf-8') as f:
|
|||
|
packages = [line.strip() for line in f if line.strip() and not line.startswith('#') and not line.startswith('//')]
|
|||
|
|
|||
|
print(f"{'Package':<20} {'Installed Version':<20}")
|
|||
|
print('-' * 40)
|
|||
|
for pkg in packages:
|
|||
|
try:
|
|||
|
# 有些包名和import名不同,优先用pkg_resources
|
|||
|
version = pkg_resources.get_distribution(pkg).version
|
|||
|
print(f"{pkg:<20} {version:<20}")
|
|||
|
except Exception:
|
|||
|
try:
|
|||
|
mod = importlib.import_module(pkg)
|
|||
|
version = getattr(mod, '__version__', 'Unknown')
|
|||
|
print(f"{pkg:<20} {version:<20}")
|
|||
|
except Exception:
|
|||
|
print(f"{pkg:<20} {'Not Installed':<20}")
|
|||
|
|
|||
|
if __name__ == "__main__":
|
|||
|
check_versions("requirements.txt")
|