Skip to content
Search

Python:ensurepip

lastmod 2025-02-14created 2022-03-115 edits

Bootstrapping the pip installer

The ensurepip package 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 feature releases of the CPython reference interpreter.

In most cases, end users of Python shouldn't need to invoke this module directly (as pip should be bootstrapped by default). However, it may be needed if installing pip was skipped when installing Python (or when creating a virtual environment), or after explicitly uninstalling pip.

INFORMATION

Note: This module does not access the internet. All of the components needed to bootstrap pip are included as internal parts of the package.

Troubleshooting

ModuleNotFoundError in pydevd

Error while finding module specification for '/home/your/Applications/pycharm/plugins/python/helpers/pydev/pydevd.py' (ModuleNotFoundError: No module named '/home/your/Applications/pycharm/plugins/python/helpers/pydev/pydevd')

Running ensure_pip in a subprocess in debug mode in PyCharm causes the problem above...

Use the following get_isolate_ensure_pip_flag() to check whether pydevd is in use,

import os
from functools import lru_cache


@lru_cache
def detect_pydevd() -> bool:
    return "PYDEVD_LOAD_VALUES_ASYNC" in os.environ


@lru_cache
def get_isolate_ensure_pip_flag() -> bool:
    """
    .. warning::
        If pydevd is connected,
        the ``python -Im ensure_pip`` command does not work properly.
    """
    return not detect_pydevd()

and remove -I from the -Im used before the ensure_pip command.

However, running outside isolated mode can cause problems, so use this only for testing.

See also

author Zer0 sitemap