brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: python -m ensurepip installs pip when your Python has none

Python
~/tips/tip-python-m-ensurepip-bootstrap-pip

python -m ensurepip --upgrade

Installs the pip version bundled with Python, offline, if pip is missing from the interpreter.

Sometimes python -m pip answers that there is no module named pip. Before reaching for a download script, check whether Python can bring its own.

python -m ensurepip --upgrade

The ensurepip module bootstraps the pip installer into the current Python installation or virtual environment. Everything it needs is bundled with Python itself, so it does not touch the internet. Without --upgrade it installs pip if it is absent and otherwise does nothing. With --upgrade it makes sure the installed pip is at least as recent as the bundled copy.

You can ask which version it carries before installing anything:

python -m ensurepip --version

There is also --user, which installs into the user site-packages instead of the system-wide location. The documentation says that option is not allowed inside an active virtual environment.

The caveat is that you get the pip that shipped with that Python release, which can be a good bit older than the newest one. After bootstrapping, update it the normal way with python -m pip install --upgrade pip. Also note that some Linux distributions split the module off into a separate package, so on those systems the command may need an extra install before it works. Check your distribution’s package name rather than assuming.

Further reading: Python docs: ensurepip

More Python tips import asyncio from enum import StrEnum, auto except* ValueError as eg: import pytest pytest --maxfail=2 All Python tips (107) All topics

Link