Python PIP

Tutorial 30 of 65 · pythondeck.com Python course

pip is Python's package installer. It pulls wheels and source distributions from PyPI. Combine with virtual environments to keep dependencies isolated and reproducible. Pin versions in requirements.txt or use modern tools like uv, poetry or pip-tools.

pip installs third-party packages from PyPI (and other indexes) into your environment. Isolating projects with virtual environments prevents version conflicts between applications. Reproducible installs matter for CI, deployment, and debugging "works on my machine" issues.

Modern workflows often combine pip with venv, pip-tools, poetry, or uv. The underlying idea stays the same: declare dependencies, pin versions, install from a lockfile or requirements file, and never install blindly into the system Python on production servers.

python -m pip install package and version specifiers (==, >=, ~=).

Virtual environments: python -m venv .venv and activation per OS.

requirements.txt from pip freeze vs curated dependency lists.

pip install -e . editable installs for local package development.

Inspecting installs: pip show, pip list, pip check for conflicts.

Upgrading safely: pip install -U and reading changelogs before major bumps.

Pin direct dependencies you control; let resolvers pick transitive versions or use a lockfile tool. Commit lockfiles or hashed requirements for CI so every build uses identical wheels.

System Python on Linux/macOS may be managed by the OS—install into a venv instead. On Windows, the Python installer usually provides a clean base interpreter suitable for venvs.

Private indexes and air-gapped installs use pip install --index-url or --find-links to local wheel directories. Audit packages with pip-audit or similar security scanners.

Installing into global Python and breaking OS tools or other projects.

Unpinned requirements.txt that changes behaviour on every fresh install.

Running pip without python -m pip and hitting the wrong interpreter.

Committing virtualenv directories to git instead of dependency manifests.

Ignoring incompatible platform markers and binary wheel requirements.

Create a venv per project; document activation in README.

Pin versions for applications; use ranges only when you actively test upgrades.

Run python -m pip install -r requirements.txt in CI identical to production.

Periodically run pip-audit and update vulnerable dependencies.

Re-read the examples below with these ideas in mind; change variable names and inputs to match your own project.

The program below demonstrates common commands. Read the comments on each line, run the code, then change names or values to see how the output shifts.

# Example: Common commands
# Run in the REPL or save as a .py file and execute with python.
pip install requests
pip install "flask>=3,<4"
pip uninstall requests
pip list
pip show requests

This sample walks through freeze / install in a small, runnable script. Paste it into the REPL or save it as a .py file before you continue to the next block.

# Example: Freeze / install
# Run in the REPL or save as a .py file and execute with python.
pip freeze > requirements.txt
pip install -r requirements.txt

Here is a hands-on illustration of upgrade. Follow the inline comments first; only then execute the snippet and compare the result with what you expected.

# Example: Upgrade
# Run in the REPL or save as a .py file and execute with python.
python -m pip install --upgrade pip
pip install --upgrade requests

The program below demonstrates install package. Read the comments on each line, run the code, then change names or values to see how the output shifts.

# Always tie pip to the interpreter: python -m pip
python -m pip --version  # show pip tied to this Python
python -m pip install requests  # install HTTP client library
python -m pip show requests  # metadata: version, location
python -m pip list  # all installed distributions
python -m pip freeze > requirements.txt  # pin versions to file
type requirements.txt  # Windows display (use cat on Unix)
python -m pip install -r requirements.txt  # install from pins
python -m pip uninstall -y requests  # remove package non-interactive

This sample walks through upgrade tools in a small, runnable script. Paste it into the REPL or save it as a .py file before you continue to the next block.

# Keep pip/setuptools/wheel current to avoid resolver bugs
python -m pip install --upgrade pip  # newest pip
python -m pip install --upgrade setuptools wheel  # build backends
python -m pip cache dir  # show wheel cache location
python -m pip cache purge  # free disk if installs fail oddly
python -m pip install "charset-normalizer>=3.0"  # version constraint
python -m pip check  # report broken dependencies
python -m pip index versions pip  # list available pip versions (if supported)
python -c "import pip; print('pip import ok')"  # sanity import

« Python RegEx All tutorials Python Try Except »