Advertisement
Python253

update_all_packages

Mar 16th, 2024
711
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.45 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Filename: update_all_packages.py
  4. # Version: 1.00
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. A Python script to update all installed packages to their latest versions,
  9. excluding those installed in editable mode.
  10.  
  11. This script utilizes the 'pip' package manager to update all packages installed in the Python environment
  12. to their latest available versions. It filters out packages installed in editable mode ('-e' flag),
  13. typically used for development purposes, as these should not be updated automatically.
  14.  
  15. Requirements:
  16.    - Python (2.7 or later) must be installed on your system.
  17.    - 'pip' package manager must be available in your PATH.
  18.  
  19. Usage:
  20.    1. Ensure Python and 'pip' are installed and accessible from your command line.
  21.    2. Save this script as 'update_packages.py' in a directory of your choice.
  22.    3. Open a command prompt or terminal window.
  23.    4. Navigate to the directory where 'update_packages.py' is located.
  24.    5. Run the script by executing the following command:
  25.      
  26.       $ python update_packages.py
  27.  
  28.    6. The script will list all installed packages and their versions,
  29.       excluding those installed in editable mode. It will then proceed to update
  30.       each package to its latest available version, displaying the updated packages
  31.       as it progresses.
  32.  
  33. Note: It's recommended to review the changes made by the script to ensure
  34.      compatibility with your projects, as updating packages may introduce
  35.      breaking changes or unexpected behavior.
  36. """
  37.  
  38. import subprocess
  39. import re
  40.  
  41. def update_packages():
  42.     # List installed packages
  43.     try:
  44.         output = subprocess.check_output(['pip', 'freeze', '--local'], universal_newlines=True)
  45.         installed_packages = output.split('\n')
  46.     except subprocess.CalledProcessError as e:
  47.         print("Error:", e)
  48.         return
  49.  
  50.     # Filter out editable packages
  51.     filtered_packages = [pkg for pkg in installed_packages if not pkg.startswith('-e')]
  52.  
  53.     # Extract package names
  54.     package_names = [re.split(r'==|\s+', pkg)[0] for pkg in filtered_packages if pkg]
  55.  
  56.     # Update packages
  57.     for package in package_names:
  58.         try:
  59.             subprocess.check_call(['pip', 'install', '-U', package])
  60.             print(f"Successfully updated {package}")
  61.         except subprocess.CalledProcessError as e:
  62.             print(f"Error updating {package}: {e}")
  63.  
  64. if __name__ == "__main__":
  65.     update_packages()
  66.  
  67.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement