Skip to content

Instantly share code, notes, and snippets.

@dl6nm
Last active March 13, 2024 11:09
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/

@stefanos82
Copy link

For Linux it's a lot easier and simpler to use

pip install --upgrade $(pip freeze | sed 's/==.*//g')

To explain what it does, we use a subshell command via $() to generate a freeze format output, such as

sphinxcontrib-qthelp==1.0.7
sphinxcontrib-serializinghtml==1.1.10
stack-data==0.6.3
terminado==0.18.1
tinycss2==1.2.1
tomli==2.0.1
tornado==6.4
traitlets==5.14.2
types-python-dateutil==2.8.19.20240311
typing_extensions==4.10.0
uri-template==1.3.0
urllib3==2.2.1
wcwidth==0.2.13
webcolors==1.13
webencodings==0.5.1

and pipe it with sed 's/==.*//g' command to remove the double equals numeric version

sphinxcontrib-qthelp
sphinxcontrib-serializinghtml
stack-data
terminado
tinycss2
tomli
tornado
traitlets
types-python-dateutil
typing_extensions
uri-template
urllib3
wcwidth
webcolors
webencodings

and go through all packages in sequential order to check for possible upgrade.

If there is an update, it will proceed to upgrade it, else it will report back a message like

Requirement already satisfied: json5 in /home/stefanos/tmp/repos/cy3env/lib/python3.11/site-packages (0.9.14)

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