python -m pip: Invoke pip Safely Command Reference
The reliable way to invoke pip for a specific Python.
python -m pip runs pip as a module of the interpreter you name, guaranteeing the install lands in that interpreter environment -- not whichever pip happens to be first on PATH.
Common flags / usage
- python -m pip install --upgrade pip -- self-upgrade pip
- python3.12 -m pip install -r requirements.txt -- target a specific Python
- python -m pip install -e . -- editable install with the right interpreter
- python -m pip --version -- show pip and the bound interpreter
Example
shell
- uses: actions/setup-python@v5
with:
python-version: '3.12'
- run: python -m pip install --upgrade pip
- run: python -m pip install -r requirements.txtIn CI
With several Pythons on a runner, a bare "pip" may belong to a different interpreter than "python", installing packages where your code cannot import them. Always use "python -m pip" with the same interpreter you run tests with.
Key takeaways
- python -m pip targets the pip belonging to a chosen interpreter.
- It eliminates the wrong-pip-on-PATH class of CI failures.
- Use it to upgrade pip before installing dependencies.
Related guides
pip install: Command Reference for CIReference for pip install in CI pipelines: installing packages, pinning versions, common flags, a GitHub Acti…
python -m venv: Virtual Environment Command ReferenceReference for python -m venv in CI: creating an isolated environment, options, and a workflow example showing…
pytest: Run Tests Command Reference for CIReference for pytest in CI: running the suite, useful flags, why python -m pytest fixes import errors, and a…