1 min read

Python Ensurepip Module

The ensurepip module provides support for bootstrapping the pip installer into an existing Python installation or virtual environment. This bootstrapping approach reflects the fact that pip is an independent project with its own release cycle, and the latest available stable version is bundled with maintenance and regular releases of the CPython interpreter.

Importing the Module

import ensurepip

Command Line Usage

The primary way to use ensurepip is from the command line.

Basic Usage

To install pip into the current environment:

python -m ensurepip

Upgrading pip

To ensure that the installed version of pip is at least as recent as the one bundled with ensurepip, pass the --upgrade or -U option:

python -m ensurepip --upgrade

User Installation

To install pip into the user site-packages directory (useful if you don't have write permissions to the system Python installation):

python -m ensurepip --user

Programmatic Usage

The ensurepip module also exposes a function to invoke it programmatically.

ensurepip.bootstrap()

Bootstraps pip into the current or designated environment.

import ensurepip

# Install pip if not present
ensurepip.bootstrap()

ensurepip.version()

Returns a string specifying the bundled version of pip that will be installed.

import ensurepip

print(ensurepip.version())

programming/python/python