Last active
August 26, 2024 16:33
-
-
Save firstDismay/a63102c48bfaeab5a23c9939dfed57e3 to your computer and use it in GitHub Desktop.
pip update python enviroment all not required packages
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from importlib.metadata import distributions | |
| from subprocess import call, check_output, PIPE, run | |
| import json | |
| def get_top_level_packages(): | |
| pip_output = check_output(["pip", "list", "--format=json", "--not-required"]).decode() | |
| pip_packages = json.loads(pip_output) | |
| return {pkg['name'].lower() for pkg in pip_packages} | |
| def update_top_level_packages(): | |
| print(f"Запущена операция обновления корневых пакетов окружения") | |
| top_level_packages = get_top_level_packages() | |
| call("pip list --outdated --format=json > outdated.txt", shell=True) | |
| with open("outdated.txt", 'r') as packages_file: | |
| outdated_packages = json.load(packages_file) | |
| for package in outdated_packages: | |
| package_name = package['name'] | |
| if package_name.lower() in top_level_packages: | |
| print(f"Обновляется пакет --> {package_name}") | |
| call(f"pip install --upgrade {package_name}", shell=True) | |
| else: | |
| print(f"Пропускается зависимость --> {package_name}") | |
| # Выполнение pip check и вывод результата | |
| print("\nПроверка зависимостей:") | |
| result = run(["pip", "check"], stdout=PIPE, stderr=PIPE, text=True) | |
| if result.returncode == 0: | |
| print("Все зависимости удовлетворены.") | |
| else: | |
| print("Обнаружены проблемы с зависимостями:") | |
| print(result.stdout) | |
| print(result.stderr) | |
| if __name__ == "__main__": | |
| update_top_level_packages() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment