UAV/check_version.py
2025-06-11 10:40:18 +08:00

24 lines
923 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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")