Home »
Python
Python | How to upgrade all Python packages with pip?
By IncludeHelp Last updated : December 07, 2024
While using Python as a programming language, it's a very common scenario to use a virtual environment and PIP, a package manager for python.
Things to do before upgrading all Python packages
It's a common practice to use a text file, named as "requirement.txt", which would be populated with the list of libraries used in the given application.
Generally, the developers maintain the version of the libraries in the "requirement.txt", as shown in the below example,
(venv) XXX:src XXX$ more requirements.txt
numpy==1.17.2
requirements.txt (END)
Upgrading all Python packages with pip
Upgrading every library is a monotonous task, and hence the following commands can be used to upgrade all the packages in the venv (virtual environment) using PIP. We could either follow the below as a two-step process or also combine it to be a single line command.
Approach 1: Upgrade all Python packages with pip
-
Freeze all the libraries to a file called 'requirements.txt' (file name could be anything)
pip freeze > installed_library_list.txt
-
Update all the libraries available in the file
pip install -r installed_library_list.txt –upgrade
Approach 2: Upgrade all Python packages with pip
pip freeze --local | grep -v '^\-e' | cut -d = -f 1 | xargs -n1 pip install -U
The grep is to skip editable ("-e") package definitions, and the -n1 flag for xargs prevents stopping everything if updating one package fails.