-
Notifications
You must be signed in to change notification settings - Fork 603
Add pip auto-updater script with support for outdated package detection and upgrade. #1023
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ayush-a-srivastava
wants to merge
1
commit into
python-geeks:main
from
ayush-a-srivastava:install_outdated_packages
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
pip_auto_updater/README.md
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| # 🛠️ Pip Auto-Updater | ||
|
|
||
| This script automates the process of updating all outdated Python packages installed via `pip`. It’s designed for developers and QA engineers who want to maintain clean, up-to-date environments with minimal manual effort. | ||
|
|
||
| ## Features | ||
|
|
||
| - Identifies all outdated packages using `pip list --outdated` excluding built-ins. | ||
| - Iteratively upgrades each package via `pip install --upgrade`, ensuring minimal disruption. | ||
| --- | ||
|
|
||
| ## Usage | ||
|
|
||
| ### Run the script directly: | ||
| ```bash | ||
| python pip_auto_updater/update_packages.py | ||
| ``` | ||
|
|
||
| ## Compatibility | ||
| - Python 3.8+ | ||
| - Pip 21+ | ||
|
|
||
| ## Notes | ||
| - Some packages may fail to update due to system-level constraints or dependency conflicts. | ||
| - It’s recommended to run this inside a virtual environment to avoid breaking global installs. | ||
|
|
||
| ## Author | ||
| Ayush Srivastava |
41 changes: 41 additions & 0 deletions
pip_auto_updater/update_packages.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import subprocess | ||
| import sys | ||
|
|
||
| def update_outdated_packages(): | ||
| """ | ||
| Updates all outdated third-party Python packages using pip. | ||
| Built-in and system packages are excluded. | ||
| """ | ||
| try: | ||
| # Get list of outdated packages | ||
| result = subprocess.run( | ||
| [sys.executable, "-m", "pip", "list", "--outdated", "--format=json"], | ||
| capture_output=True, text=True, check=True | ||
| ) | ||
| outdated = eval(result.stdout) # JSON parsing without import for simplicity | ||
|
|
||
| if not outdated: | ||
| print("All packages are up to date.") | ||
| return | ||
|
|
||
| print(f"Found {len(outdated)} outdated packages. Updating...\n") | ||
|
|
||
| for pkg in outdated: | ||
| name = pkg['name'] | ||
| current_version = pkg['version'] | ||
| latest_version = pkg['latest_version'] | ||
| print(f"Updating {name} ({current_version} → {latest_version})...") | ||
|
|
||
| try: | ||
| subprocess.run( | ||
| [sys.executable, "-m", "pip", "install", "--upgrade", name], | ||
| check=True | ||
| ) | ||
| print(f"✅ {name} updated successfully.\n") | ||
| except subprocess.CalledProcessError: | ||
| print(f"Failed to update {name}. Skipping...\n") | ||
|
|
||
| except Exception as e: | ||
| print(f"Error occurred: {e}") | ||
|
|
||
| update_outdated_packages() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.