Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 38 ยท Getting Started

Installing Packages with pip

Learn the everyday pip commands, version specifiers, extras and requirements files, plus how installers resolve dependencies, with live micropip examples.

pip is the installer that ships with Python and the tool most people meet first. Its basic job is simple: fetch a distribution from an index, work out what else it needs, and place everything into the current environment. A handful of commands cover almost every day.

In your browser there is no pip binary, so the commands below are read-only. The live examples use micropip, the installer for Pyodide, which does the same core work (resolve requirements, download wheels, unpack them) for pure-Python packages. That lets you watch real installs happen and inspect the results.

The everyday commands

Terminal
python -m pip install requests              # latest version
python -m pip install "requests==2.32.3"    # exact version
python -m pip install "requests>=2.30,<3"  # a range
python -m pip install -U requests           # upgrade
python -m pip uninstall requests            # remove
python -m pip list                          # what is installed
python -m pip show requests                 # details + who requires it
python -m pip freeze > requirements.txt     # pin the current state
python -m pip install -r requirements.txt   # install a list
python -m pip check                         # find broken dependencies

Always write python -m pip rather than bare pip. It guarantees that you use the pip belonging to the interpreter you mean, which prevents the classic mistake of installing into one Python and running another. Quote specifiers that contain <, > or *, because the shell treats them as redirections.

Installing from other places

Terminal
python -m pip install ./myproject                 # a local directory
python -m pip install -e ./myproject              # editable: changes apply at once
python -m pip install dist/mylib-1.0-py3-none-any.whl   # a built wheel
python -m pip install "git+https://github.com/org/[email protected]"  # a VCS URL
python -m pip install --index-url https://test.pypi.org/simple/ mylib

An editable install (-e) does not copy your code. It registers your source directory, so edits take effect without reinstalling. It is the standard way to develop a project, and the topic of src Layout and Package Data.

Requirement strings

Every argument above is a requirement specifier: a name, optional extras, version constraints and optionally a marker. The grammar is standardized (PEP 508) and the packaging library parses it. That is the same parser pip uses, so you can experiment live.

Python Parsing requirement strings like pip does

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The pieces:

  • Extras (uvicorn[standard]) opt in to optional dependency groups that the library defines.
  • Specifiers limit versions. ~=1.4 means "compatible release" (>=1.4, ==1.*).
  • Markers (after ;) say when a requirement applies: platform, Python version, and so on.

What an installer does

flowchart TD
    A["Requested: app"] --> B["Ask index for versions of app"]
    B --> C["Read metadata:<br/>Requires-Dist"]
    C --> D["Resolve: pick versions<br/>satisfying ALL constraints"]
    D --> E["Download wheels<br/>(build sdists if needed)"]
    E --> F["Unpack into site-packages<br/>write .dist-info"]

Resolution is the hard step: every package's requirements must be satisfied at the same time by one chosen version of each.

Modern pip has a backtracking resolver. If package A needs x<2 and package B needs x>=2, it reports ResolutionImpossible instead of quietly installing a broken set. Read that error from the bottom up: it shows the chain of requirements that collided.

A live install

Python Installing a package with micropip and inspecting the result

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Dependencies you pin and dependencies you declare

Two lists are easy to confuse:

  • Abstract (declared) dependencies are what your project needs, written as loose ranges in pyproject.toml: requests>=2.30.
  • Concrete (pinned) dependencies are the exact versions that were installed, written by pip freeze or a lock file: requests==2.32.3.

Libraries should declare ranges, so users can combine them with other packages. Applications should additionally pin, so every deployment is identical. Dependency Management covers the pinning workflows.

Python Turning a freeze list into structured data

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Useful options

Option Use
--upgrade / -U Install newer versions of what is requested
--no-deps Skip dependencies (rarely right; breaks pip check)
--require-virtualenv Refuse to install outside a virtual environment
--user Install into your user site (avoid; prefer environments)
--only-binary :all: Never build from source; fail instead
--no-cache-dir Ignore pip's download cache
--dry-run Show what would be installed
-v / --verbose See why it chose a file

Do not run sudo pip install into the system Python. Operating systems use their own Python for system tools and mark it "externally managed" (PEP 668), so recent pip refuses. Use a virtual environment instead.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why is python -m pip install ... recommended over pip install ...?

    Bare pip may belong to a different Python. python -m pip uses the pip of that exact interpreter.
  2. 2. What does the requirement 'colorama; sys_platform == "win32"' mean?

    Text after the semicolon is a marker. The requirement applies only when the marker is true.
  3. 3. What is an editable install (pip install -e .)?

    Editable installs link to your working tree instead of copying files.
  4. 4. Which belongs in a library's declared dependencies?

    Libraries should allow ranges so that users can combine them with other packages. Applications pin exact versions in a lock file.

Summary

  • Use python -m pip install and quote specifiers containing <, >.
  • A requirement string = name + extras + version specifier + optional marker (PEP 508).
  • Installers resolve all constraints together, download wheels, unpack them and write .dist-info.
  • -e installs link to your source for development.
  • Libraries declare ranges; applications also pin exact versions.
  • Do not install into the system Python; use environments.

Next: Wheels, Sdists and the Package Index