UAV/check_version.py

24 lines
923 B
Python
Raw Normal View History

2025-06-11 10:40:18 +08:00
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")