Skip to content

Instantly share code, notes, and snippets.

@hritik5102
Last active April 17, 2024 13:49
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hritik5102/35b2886771d60fc6d7a95b6fd55c62ba to your computer and use it in GitHub Desktop.
Save hritik5102/35b2886771d60fc6d7a95b6fd55c62ba to your computer and use it in GitHub Desktop.
How To Update All Python Packages

Python Package Upgrade Checklist

Get a list of all the outdated packages

$ pip list --outdated

Open a command shell by typing ‘powershell’ in the Search Box of the Task bar
Update All Python Packages On Windows

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

Update All Python Packages On Linux

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

Pip can be used to upgrade all packages on either Windows or Linux:


  1. Output a list of installed packages into a requirements file (requirements.txt):
$ pip freeze > requirements.txt
  1. Edit requirements.txt, and replace all ‘==’ with ‘>=’. Use the ‘Replace All’ command in the editor.

  2. Upgrade all outdated packages:

$ pip install -r requirements.txt --upgrade

Updating All Packages In A Pipenv Environment

The simplest way to update all the unpinned packages in a specific virtual environment created with pipenv is to do the following steps:

Activate the Pipenv shell that contains the packages to be upgraded

$ pipenv shell

Upgrade all packages

$ pipenv update

If a requirements.txt file is not available, you can use the pip show command to output all the requirements of a specified package:

$ pip show <packagename>

Automatically create requirements.txt

You can use the following code to generate a requirements.txt file:

$ pip install pipreqs
$ pipreqs /path/to/project

More info related to pipreqs can be found here. Sometimes you come across pip freeze, but this saves all packages in the environment including those that you don't use in your current project.

@hritik5102
Copy link
Author

NOTE

  • Pinned packages in a requirements.txt file are denoted by ==. For example, requests==2.21.0. Pinned packages should never be updated except for a very good reason, such as to fix a critical bug or vulnerability.
  • Conversely, unpinned packages are typically denoted by >=, which indicates that the package can be replaced by a later version. Unpinned packages are more common in development environments, where the latest version can offer bug fixes, security patches and even new functionality.

As packages age, many of them are likely to have vulnerabilities and bugs logged against them. In order to maintain the security and performance of your application, you’ll need to update these packages to a newer version that fixes the issue.

  1. The pip package manager can be used to update one or more packages system-wide.

  2. However, if your deployment is located in a virtual environment, you should use the Pipenv package manager to update all Python packages.

@sbugalski
Copy link

sbugalski commented Apr 17, 2024

Since pip freeze | %{$_.split('==')[0]} | %{pip install --upgrade $_} is not working properly (it includes package paths, etc) it might be helpful:
pip list --outdated | %{$_.split(' ')[0]} | % { pip install --upgrade $_}

However it may fail on upgrading pip, so it's best to run:
pip list --outdated | %{$_.split(' ')[0]} | % { python.exe -m pip install --upgrade $_}

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