Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- # Filename: update_all_packages.py
- # Version: 1.00
- # Author: Jeoi Reqi
- """
- A Python script to update all installed packages to their latest versions,
- excluding those installed in editable mode.
- This script utilizes the 'pip' package manager to update all packages installed in the Python environment
- to their latest available versions. It filters out packages installed in editable mode ('-e' flag),
- typically used for development purposes, as these should not be updated automatically.
- Requirements:
- - Python (2.7 or later) must be installed on your system.
- - 'pip' package manager must be available in your PATH.
- Usage:
- 1. Ensure Python and 'pip' are installed and accessible from your command line.
- 2. Save this script as 'update_packages.py' in a directory of your choice.
- 3. Open a command prompt or terminal window.
- 4. Navigate to the directory where 'update_packages.py' is located.
- 5. Run the script by executing the following command:
- $ python update_packages.py
- 6. The script will list all installed packages and their versions,
- excluding those installed in editable mode. It will then proceed to update
- each package to its latest available version, displaying the updated packages
- as it progresses.
- Note: It's recommended to review the changes made by the script to ensure
- compatibility with your projects, as updating packages may introduce
- breaking changes or unexpected behavior.
- """
- import subprocess
- import re
- def update_packages():
- # List installed packages
- try:
- output = subprocess.check_output(['pip', 'freeze', '--local'], universal_newlines=True)
- installed_packages = output.split('\n')
- except subprocess.CalledProcessError as e:
- print("Error:", e)
- return
- # Filter out editable packages
- filtered_packages = [pkg for pkg in installed_packages if not pkg.startswith('-e')]
- # Extract package names
- package_names = [re.split(r'==|\s+', pkg)[0] for pkg in filtered_packages if pkg]
- # Update packages
- for package in package_names:
- try:
- subprocess.check_call(['pip', 'install', '-U', package])
- print(f"Successfully updated {package}")
- except subprocess.CalledProcessError as e:
- print(f"Error updating {package}: {e}")
- if __name__ == "__main__":
- update_packages()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement