How to List All Available Versions of a Python Package
Knowing the available versions of a Python package is essential for managing dependencies, troubleshooting compatibility issues, and choosing the right version for your project.
This guide explains how to list all available versions of a Python package using pip
, querying PyPI directly, and using a programmatic approach with the requests
library.
Using pip index versions
(Recommended - pip 21.1+)
Starting with pip
version 21.1, the pip index versions
command provides a direct and reliable way to list available package versions without attempting to install anything. This is now the recommended method.
pip index versions <package_name>
# Example:
pip index versions requests
- Replace
<package_name>
with the actual name of the package (e.g.,requests
,numpy
,django
). - The output lists available versions from newest to oldest, along with the package name and the supported Python versions.
- This method is fast, efficient, and doesn't involve any "tricks" or workarounds.
If the command above doesn't work for you, check your pip version:
pip --version
If it's below 21.1 update it using:
pip install --upgrade pip
#or
python -m pip install --upgrade pip
#or
python3 -m pip install --upgrade pip
#or
py -m pip install --upgrade pip # Windows
Using pip install ==
(Works with older pip versions)
Before pip index versions
was available, a common technique was to use pip install
with an invalid version specifier (e.g., ==
). This causes pip to list available versions in the error message.
pip install <package_name>==
# Example:
pip install requests==
This method still works with current versions of pip
, but the output format has changed over time, and can be less clear.
Example Output (older pip versions):
ERROR: Could not find a version that satisfies the requirement requests== (from versions: 0.2.0, 0.2.1, 0.2.2, ..., 2.28.1, 2.29.0, 2.31.0)
ERROR: No matching distribution found for requests==
Example Output (newer pip versions):
ERROR: Could not find a version that satisfies the requirement requests== (from versions: none)
ERROR: No matching distribution found for requests==
- It is recommended to use
pip index versions
if your version of pip is newer than 21.1, as the result is more accurate, and the command is more straightforward. - If your version is
pip install --use-deprecated=legacy-resolver requests==
can be used, but is not recommended.
Querying the PyPI JSON API
For programmatic access to package versions (e.g., within a Python script), you can query the PyPI JSON API:
import requests
def list_versions(package_name):
url = f"https://pypi.org/pypi/{package_name}/json"
try:
response = requests.get(url, timeout=10)
response.raise_for_status() # Raise HTTPError for bad responses (4xx or 5xx)
data = response.json()
versions = sorted(data['releases'].keys(), reverse=True) # Sort from newest to oldest
return versions
except requests.exceptions.RequestException as e:
print(f"Error fetching data from PyPI: {e}")
return [] # Return empty list
versions = list_versions('requests')
if versions:
print(versions) # For example: ['2.31.0', '2.30.0', '2.29.0', '2.28.2', '2.28.1', '2.28.0', ..., '0.2.0']
else:
print("No versions found or error occurred.")
requests.get(url, timeout=10)
: Fetches the JSON data from PyPI. Thetimeout
is essential to prevent your script from hanging indefinitely if PyPI is slow or unavailable.response.raise_for_status()
: Raises an exception if the HTTP request returns an error status code (4xx or 5xx). This is crucial for robust error handling.data['releases'].keys()
: The JSON data has areleases
key, which is a dictionary where the keys are the version strings.sorted(..., reverse=True)
: Sorts the versions in descending order (newest first). This is generally the most useful order. We use the default string sorting, which works correctly for most well-behaved version strings.- Error Handling: If the request to the URL fails, it is handled in the
except
block and returns an empty list.