- How to Upgrade Python Packages with Pip
- How to use pip to upgrade Python packages
- 1. List outdated packages
- 2. Upgrade a specific package
- 3. Upgrade package to specific version
- 4. Upgrade every package using Pip
- Wrapping Up
- Upgrade all packages in Python using pip
- The Pip Python Package Manager
- Update all packages using pip on Windows
- Update all packages using pip on Linux
- Command for either Windows or Linux for updating packages
- Updating all packages in a Virtual Environment
- For pip < 10.0.1
- For pip >= 10.0.1
- Updating all Local Packages using pip-review
- Conclusion
- How to Update All Python Packages
- Python Package Upgrade Checklist
- Update all Python Packages on Windows
- Update all Python Packages on Linux
- Updating Python Packages on Windows or Linux
- Updating all Packages in a Virtual Environment
- Updating all Packages in a Pipenv Environment
- Modern way to manage Python packages – ActiveState Platform
How to Upgrade Python Packages with Pip
When was the last that you updated Python packages installed via Pip? Most of the users tend to forget that those packages also need to be updated, as just updating the system repository is not going to work here.
So let’s take a moment and see how to update old Python packages with Pip.
How to use pip to upgrade Python packages
Pip (Pip Installs Packages) is a command line utility to manage python packages. You can think of this as how we use apt to manage packages in Ubuntu and Debian.
So let’s dive deep into how you can use this fab utility to manage everything related to Python packages.
1. List outdated packages
Listing the outdated packages is the best idea to plan how you want to update packages as not many want to update their entire library of packages at once and wants to be selective.
To list outdated packages of Python, you just have to pair pip command with list option and —outdated flag as shown:
2. Upgrade a specific package
Once you get the list of the packages that need to be updated, you can be selective as I mentioned earlier, and to update a specific package, you’ll need to follow the given command syntax:
pip install package_name -U
For example, I want to upgrade the package named anime-api to the most recent version, so I’ll be using the given command:
3. Upgrade package to specific version
It is not necessary to use only the most recent version of the software (cough Debian cough) and if you are in need of using packages to a specific version that may or may not be the most recent software, can be done using the given command syntax:
So I want to update the package named xdg to version 5.1 which is one point release behind the most recent build so my command would be:
pip install --upgrade xdg==5.1
4. Upgrade every package using Pip
NOTE: I do not recommend upgrading every package at once as most of the time, the dependencies are too complex to be handled.
To upgrade every python package, you’d need to follow the given command:
pip3 list --outdated --format=freeze | grep -v '^\-e' | cut -d = -f 1 | xargs -n1 pip3 install -U
The above command utilizes xargs. First, it will grab the packages that are needed to be updated and then perform pip3 install -U command over each package.
And I used pip3 here instead of pip. In Ubuntu 22.04 and later, both pip and pip3 commands are available.
Wrapping Up
Upgrading everything at once has never been a good idea in the case of pip. And I found myself in a state of broken dependencies so make sure you know what you will have.
And if you have any queries, feel free to ask in the comments.
Upgrade all packages in Python using pip
In this article, we will learn to upgrade all Python packages using pip manager. We will use some built-in functions, pip Python manager available in Python to upgrade all packages available in Python. Let’s first have a quick look over what is a pip in Python.
The Pip Python Package Manager
Programmers generally use virtual environments and pip package while working with the Python programming language. When working with projects in Python, users have packages versions being used are defined, which starts growing with time and some packages start to be outdated. pip Python manager is designed to upgrade the python packages system-wide. Let us look at different ways to use pip to upgrade packages from older versions to newer or latest versions.
Update all packages using pip on Windows
This is the easier way to upgrade packages by using pip in conjunction with Windows PowerShell. Open your command shell and enter the below command. This will upgrade all packages system-wide to the latest or newer version available in the Python Package Index (PyPI) .
Update all packages using pip on Linux
Linux provides a number of ways to use pip in order to upgrade python packages. This includes two ways using grep and awk.
- Use grep to upgrade packages — The grep is to skip editable («-e») package definitions, and the -n1 flag for xargs that prevents stopping everything, if updating one package fails.
pip3 list --outdated --format=freeze | grep -v '^\-e' | cut -d = -f 1 | xargs -n1 pip3 install -U
- Use awk to upgrade packages — The below command first lists all outdated packages, then fetches the first column and converts the multiline result from cut into a single-line, and forms a space-separated list. It then skips header lines, fetches the first column and takes 1 argument from the pipe left of it, and at last passes it to the command to upgrade the list of packages.
pip3 list -o | cut -f1 -d' ' | tr " " "\n" | awk '=3)print)' | cut -d' ' -f1 | xargs -n1 pip3 install -U
Command for either Windows or Linux for updating packages
pip freeze first outputs a list of installed packages into a requirements file (requirements.txt). Then the user needs to edit requirements.txt, and replace all ‘ == ’ with ‘ >= ’. Use the ‘Replace All’ command in the editor. It then upgrades all outdated packages.
#outputs the list of installed packages pip freeze > requirements.txt #updates all packages pip install -r requirements.txt --upgrade
Updating all packages in a Virtual Environment
The easiest way to update unpinned packages (i.e., packages that do not require a specific version) in a virtual environment is to run the following Python script that uses pip. Unlike pip freeze , this command will not print warnings and FIXME errors.
For pip < 10.0.1
import pkg_resources from subprocess import call for dist in pkg_resources.working_set: call("python -m pip install --upgrade " + dist., shell=True)
For pip >= 10.0.1
import pkg_resources from subprocess import call packages = [dist.project_name for dist in pkg_resources.working_set] call("pip install --upgrade " + ' '.join(packages), shell=True)
Updating all Local Packages using pip-review
This command updates only local Python packages. However, this command may not be feasible because it might sometimes generate errors and pip.review may or may not support Python 3 version. pip-review is a fork of pip-tools . pip-review package works but pip-tools package no longer works in the latest versions of Python.
$ pip install pip-review $ pip-review --local --interactive
Conclusion
In this article, we learned different commands to upgrade or update all Python packages using pip manager in Python. We saw two main methods such as pip freeze and pip review to update packages.
How to Update All Python Packages
With Python, the best practice of pinning all the packages in an environment at a specific version ensures that the environment can be reproduced months or even years later.
- 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.
The pip package manager can be used to update one or more packages system-wide. However, if your deployment is located in a virtual environment, you should use the Pipenv package manager to update all Python packages.
NOTE: be aware that upgrading packages can break your environment by installing incompatible dependencies. This is because pip and pipenv do not resolve dependencies, unlike the ActiveState Platform. To ensure your environment doesn’t break on upgrade, you can sign up for a free ActiveState Platform account and import your current requirements.txt, ready to be upgraded.
Python Package Upgrade Checklist
In general, you can use the following steps to perform a package upgrade:
1. Check that Python is installed
Before packages can be updated, ensure that a Python installation containing the necessary files needed for updating packages is in place by following the steps outlined in < Installation Requirements >
2. Get a list of all the outdated packages
To generate a list of all outdated packages:
3. Upgrade outdated packages
Depending on your operating system or virtual environment, refer to the following sections.
Update all Python Packages on Windows
The easiest way to update all packages in a Windows environment is to use pip in conjunction with Windows PowerShell:
- Open a command shell by typing ‘powershell’ in the Search Box of the Task bar
- Enter:
This will upgrade all packages system-wide to the latest version available in the Python Package Index (PyPI).
Update all Python Packages on Linux
Linux provides a number of ways to use pip in order to upgrade Python packages, including grep and awk.
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 '=3)print>' | cut -d' ' -f1 | xargs -n1 pip3 install -U
Updating Python Packages on Windows or Linux
Pip can be used to upgrade all packages on either Windows or Linux:
pip freeze > requirements.txt
- Edit requirements.txt, and replace all ‘==’ with ‘>=’. Use the ‘Replace All’ command in the editor.
- Upgrade all outdated packages:
pip install -r requirements.txt --upgrade
Updating all Packages in a Virtual Environment
The easiest way to update unpinned packages (i.e., packages that do not require a specific version) in a virtual environment is to run the following Python script that makes use of pip:
import pkg_resources from subprocess import call for dist in pkg_resources.working_set: call("python -m pip install --upgrade " + dist. , shell=True)
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:
Modern way to manage Python packages – ActiveState Platform
The ActiveState Platform is a cloud-based build automation and dependency management tool for Python. It provides dependency resolution for:
- Python language cores, including Python 2.7 and Python 3.5+
- Python packages and their dependencies, including:
- Transitive dependencies (ie., dependencies of dependencies)
- Linked C and Fortran libraries, so you can build data science packages
- Operating system-level dependencies for Windows, Linux, and macOS
- Shared dependencies (ie., OpenSSL)
The ActiveState Platform is the only Python package management solution that not only resolves dependencies but also provides workarounds for dependency conflicts.
Simply following the instruction prompts will resolve the conflict, eliminating dependency hell.
You can try the ActiveState Platform for free by creating an account using your email or your GitHub credentials. Start by creating a new Python project, pick the latest version that applies to your project, your OS and start to add packages. Or start by simply importing your requirements.txt file and creating a Python version with all the packages you need. The Platform will automatically pick the right package versions for your environment to ensure security and reproducibility.
Watch this tutorial to learn how to use the ActiveState Platform to create a Python 3.9 environment, and then use the Platform’s Command-Line Interface (State Tool) to install and manage it.
Ready to see for yourself? You can try the ActiveState Platform by signing up for a free account using your email or GitHub credentials.
Just run the following command to install Python 3.9 and our package manager, the State Tool:
powershell -Command "& $([scriptblock]::Create((New-Object Net.WebClient).DownloadString('https://platform.activestate.com/dl/cli/install.ps1'))) -activate-default ActiveState-Labs/Python-3.9Beta"
Now you can run state install . Learn more about how to use the State Tool to manage your Python environment. Or sign up for a free demo and let us show you how it can help improve your dev team’s workflow by compiling Python packages and resolve dependencies in minutes.