Skip to content

Python Script to Update all Packages with Pip (Windows, Linux, Mac, ChromeOS, etc)

RileyXX edited this page Jun 6, 2023 · 3 revisions
  1. Create a new .txt file and rename it to AutoUpdatePythonPackages.py
  2. Open AutoUpdatePythonPackages.py file in a text editor, copy the following code and save it to the .py file.
import subprocess, json

# Add or remove which python packages/projects you would like to ignore from auto-updates
# e.g. ['Project 1', 'Project 2', 'Project 3'] or no packages with []
ignored_packages = ['chromedriver-py']

# Get a list of outdated packages
outdated_packages_json = subprocess.check_output(['python', '-m', 'pip', 'list', '--outdated', '--format=json']).decode()
outdated_packages_data = json.loads(outdated_packages_json)

# Update each outdated package (excluding chromedriver-py)
updated_packages = 0
for package in outdated_packages_data:
    package_name = package['name']
    if package_name not in ignored_packages:
        subprocess.call(['python', '-m', 'pip', 'install', '-U', package_name])
        updated_packages += 1

if updated_packages > 0:
    print(f"Updated {updated_packages} package(s).")
else:
    print("All packages are up to date.")
  1. Run the file.
  2. (Optional) Edit the ignored_packages value to exclude packages and projects from auto-updating.