Skip to content

Instantly share code, notes, and snippets.

@dl6nm
Last active February 19, 2025 21:05
Show Gist options
  • Save dl6nm/1bd40ed663be228f54a28aec35cf616b to your computer and use it in GitHub Desktop.
Save dl6nm/1bd40ed663be228f54a28aec35cf616b to your computer and use it in GitHub Desktop.
Hot to update all Python packages with pip

How To Update All Python Packages

List Outdated Packages

pip list --outdated

Update All Python Packages

Windows

pip freeze | %{$.split('==')[0]} | %{pip install --upgrade $}

Linux

To upgrade all packages using pip with grep on Ubuntu Linux

pip3 list --outdated --format=freeze | grep -v '^\-e' | cut -d = -f 1 | xargs -n1 pip3 install -U

To upgrade all packages using pip with awk on Ubuntu Linux

pip3 list -o | cut -f1 -d' ' | tr " " "\n" | awk '{if(NR>=3)print}' | cut -d' ' -f1 | xargs -n1 pip3 install -U

Update Specific Packages On Windows Or Linux

  1. Freeze packages

    pip freeze > requirements.txt
    
  2. Edit requirements.txt (set the versions you need for your project)

  3. Upgrade to the newer/older versions you've setted

    pip install -r requirements.txt --upgrade
    

References

https://www.activestate.com/resources/quick-reads/how-to-update-all-python-packages/

@knumskull
Copy link

Format option freeze is not available on Mac in Python 3.13. The follow command is more convenient by using jq instead of regex or other fancy string manipulation.

$ pip3 list --outdated --format=json | jq -r '.[].name' | xargs -n1 pip3 install -U

Like @stefanos82 mentioned, it's also possible to ease the usage with the help of a subshell.

$ pip3 install --upgrade $(pip3 list --outdated --format=json | jq -r '.[].name')

If nothing can be upgraded, the command will fail with the following

ERROR: You must give at least one requirement to install (see "pip help install")

@stefanos82
Copy link

The jq is an added dependency on user's side; logically and probably AWK is installed on any UNIX and UNIX-like environments - one of them is Mac and I hope I'm not wrong - as there are many scripts that are being used by the system in various areas behind the scenes.

The alternative to jq command is the following (I will be using pip for both Python2 and Python3): pip install --upgrade $(pip list --outdated | awk 'NR>2 {print $1}')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment