Pip list all available versions of a Python package

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
3 min

banner

# Table of Contents

  1. Pip list all available versions of a Python package
  2. Pip list all available versions of a Python package using pypi
  3. Pip list all available versions of a Python package inside a script

# Pip list all available versions of a Python package

Use the pip install package== command to list all of the available versions of a Python package using pip, e.g. pip install requests==.

The output contains a tuple of all of the versions of the package from the oldest to the most recent version.

shell
# ๐Ÿ‘‡๏ธ For pip >=21.1 pip install requests== # ๐Ÿ‘‡๏ธ For pip >=20.3 pip install --use-deprecated=legacy-resolver requests== # ๐Ÿ‘‡๏ธ For pip >=21.2 (experimental - might change) pip index versions requests

pip list all available versions of package

The versions of the package will be printed without having to download the actual package.

The examples above use the requests package. Make sure to replace requests with your specific package.

You can check your version of pip with the pip --version command.

shell
pip --version pip3 --version

check pip version

If you have an older version of pip, you can upgrade it with the following command.

shell
# ๐Ÿ‘‡๏ธ If you have pip already installed pip install --upgrade pip # ๐Ÿ‘‡๏ธ If your pip is aliased as pip3 (Python 3) pip3 install --upgrade pip # ๐Ÿ‘‡๏ธ If you don't have pip in your PATH environment variable python -m pip install --upgrade pip # ๐Ÿ‘‡๏ธ If you don't have pip in your PATH environment variable python3 -m pip install --upgrade pip

If you pip version is greater than or equal to 21.1, run the pip install example== command with two equal signs after the package name.

shell
# ๐Ÿ‘‡๏ธ For pip >=21.1 pip install requests==

pip list all available versions of package

You can install a specific version of the package by specifying the version after the two equal signs.

shell
# ๐Ÿ‘‡๏ธ In a virtual environment or using Python 2 pip install requests==2.28.1 # ๐Ÿ‘‡๏ธ For Python 3 pip3 install requests==2.28.1 # ๐Ÿ‘‡๏ธ If you don't have pip in your PATH environment variable python3 -m pip install requests==2.28.1

The example commands install version 2.28.1 of the requests package.

If you already have the package installed, it's best to uninstall it and rerun the command scoped to the specific version.

shell
# ๐Ÿ‘‡๏ธ Uninstall package pip uninstall requests pip3 uninstall requests python3 -m pip uninstall requests # ๐Ÿ‘‡๏ธ Install a specific version of the package pip install requests==2.28.1

If you need to find which version of a specific package is installed, use the pip show <package-name> command.

shell
pip show requests

If you need to check the versions of all of the installed, packages, use the pip freeze command.

shell
pip freeze
If you use a pip version that is in the range 20.3-21.1, you might have to set the --use-deprecated option when checking the versions of the package.
shell
# ๐Ÿ‘‡๏ธ For pip >=20.3 <=21.1 pip install --use-deprecated=legacy-resolver requests==

If you use a pip version greater than 21.2, you can also try using the pip index versions <package-name> command.

shell
# ๐Ÿ‘‡๏ธ For pip >=21.2 (experimental - might change) pip index versions requests

pip list all versions of package using index

The command returns all of the available versions of the package from the most recent to the oldest.

However, note that the command is experimental and might change or be removed in the future.

Alternatively, you can view all versions of a package on its pypi page.

# Pip list all available versions of a Python package using pypi

This is a three-step process:

  1. Google "package-name pypi".
  2. Clicking on the pypi page of the package.
  3. Click on "Release history".

get versions of package

Here is the pypi page for requests.

# Pip list all available versions of a Python package inside a script

To pip list all available versions of a package from inside a Python script:

  1. Make an HTTP request to pypi to get the JSON data for the package.
  2. Parse the JSON response and access the releases key.
main.py
import requests from pkg_resources import parse_version def list_versions(package_name): url = f"https://pypi.org/pypi/{package_name}/json" res = requests.get(url, timeout=5) data = res.json() versions = data['releases'] return sorted(versions, key=parse_version, reverse=True) # ['2.28.1', '2.28.0', '2.27.1', '2.27.0', '2.26.0', '2.25.1', '2.25.0', '2.24.0', ....] print(list_versions('requests'))

pip list all versions of package inside script

The code sample uses the requests package to make an HTTP request, so make sure to install it if you haven't.

shell
pip install requests pip3 install requests

The list_versions function takes a package name as a parameter and makes an HTTP request to the pypi JSON API.

We parse the data, access the releases key and sort the versions from most recent to oldest.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev