How pip install Works: Wheels vs sdists
pip install requests looks like one step, but pip does a small pipeline of work behind it: find candidate files on an index, pick the best one for this interpreter, resolve dependencies, possibly build a wheel from source, then unpack. Knowing the steps tells you why some installs take a second and others take ten minutes and need a C compiler.
The pipeline
- Ask the index. pip requests
<index>/simple/<name>/, an HTML (or JSON) page listing every file of every version (the Simple Repository API, PEP 503/691). - Filter. It drops files that don't match the requirement (
requests>=2.30), don't support this Python (Requires-Python), or are wheels for another platform (their tags, next lesson). - Prefer wheels. Among the newest matching version, a compatible wheel wins over an sdist.
- Resolve. pip reads each candidate's metadata (
Requires-Dist) and repeats steps 1–3 for its dependencies, backtracking if two requirements clash. - Build any sdist into a wheel (in an isolated environment with the project's build backend).
- Install every wheel: unzip into
site-packages, write the.dist-infofolder, create console-script launchers.
Two kinds of distribution
Wheel (.whl) |
sdist (.tar.gz) |
|
|---|---|---|
| Contents | files laid out exactly as they go into site-packages, plus .dist-info metadata |
the project's source tree plus PKG-INFO and pyproject.toml |
| Installing it | unzip and copy: no code from the package runs | must be built first: pip runs the project's build backend |
| Compiled extensions | already compiled, one wheel per platform | compiled on your machine (needs a compiler and headers) |
| Filename | name-version-pythontag-abitag-platformtag.whl |
name-version.tar.gz |
An sdist is the "source of truth" a package publishes; wheels are the convenient pre-built form. Most popular packages publish both, and many pure-Python packages publish a single py3-none-any wheel that works everywhere.
A wheel install
The index in this course's container has a wheel for attrs. pip finds it, downloads it and unpacks it; no build step appears:
python3 -m venv .venv && . .venv/bin/activate
pip install attrs==26.1.0
ls .venv/lib/python3.13/site-packages | grep -i attrLooking in indexes: http://127.0.0.1:8080/simple/
Collecting attrs==26.1.0
Downloading attrs-26.1.0-py3-none-any.whl (67 kB)
Installing collected packages: attrs
Successfully installed attrs-26.1.0
attr
attrs
attrs-26.1.0.dist-info
attrs and attr are the import packages; attrs-26.1.0.dist-info is the install record.
An sdist install
six is in the index only as a source distribution, so pip has to build it. Watch the extra steps: pip creates an isolated build environment, installs the build backend into it (setuptools, from the same index), asks the backend for metadata, then builds a wheel and caches it:
python3 -m venv .venv && . .venv/bin/activate
pip install six
echo "--- a second environment reuses the cached wheel"
python3 -m venv .venv2 && .venv2/bin/pip install six | grep -E 'six|Successfully'Looking in indexes: http://127.0.0.1:8080/simple/
Collecting six
Downloading six-1.17.0.tar.gz (34 kB)
Installing build dependencies: started
Installing build dependencies: finished with status 'done'
Getting requirements to build wheel: started
Getting requirements to build wheel: finished with status 'done'
Preparing metadata (pyproject.toml): started
Preparing metadata (pyproject.toml): finished with status 'done'
Building wheels for collected packages: six
Building wheel for six (pyproject.toml): started
Building wheel for six (pyproject.toml): finished with status 'done'
Created wheel for six: filename=six-1.17.0-py2.py3-none-any.whl size=11132 sha256=1d820afaed4960412591d973483057a0e368de1e4c535963b1641e8b6299397b
Stored in directory: /home/learner/.cache/pip/wheels/c9/58/09/cf828223c366a243494803196d111d450d5d4b723d45e3b665
Successfully built six
Installing collected packages: six
Successfully installed six-1.17.0
--- a second environment reuses the cached wheel
Collecting six
Using cached six-1.17.0-py2.py3-none-any.whl
Installing collected packages: six
Successfully installed six-1.17.0
The second install says Using cached six-…whl: pip keeps wheels it built in ~/.cache/pip/wheels, so the build happens once per machine. That cache is also why CI jobs that build sdists benefit from caching ~/.cache/pip.
Note: Building runs the package's own build code. Installing an sdist from an untrusted source is running untrusted code.
pip install --only-binary=:all:refuses sdists entirely, and--no-binary=:all:does the opposite (forces source builds).
Forcing the choice
python3 -m venv .venv && . .venv/bin/activate
pip install --only-binary=:all: six 2>&1 | tail -2
pip download --no-deps -d dl requests==2.31.0 six 2>&1 | grep Saved
ls dlERROR: Could not find a version that satisfies the requirement six (from versions: none)
ERROR: No matching distribution found for six
Saved ./dl/requests-2.31.0-py3-none-any.whl
Saved ./dl/six-1.17.0.tar.gz
requests-2.31.0-py3-none-any.whl
six-1.17.0.tar.gz
With sdists forbidden, six has no installable file at all (from versions: none): the index only has its source. pip download runs the same find-and-choose logic without installing, which is handy for seeing what pip would pick, or for filling an offline wheelhouse.
A runnable model: choosing a file
This model reproduces steps 2 and 3 for one package: keep files whose version matches, prefer the newest version, and within it prefer a compatible wheel over the sdist:
files = [
"six-1.16.0-py2.py3-none-any.whl",
"six-1.17.0.tar.gz",
"fastmath-2.0.tar.gz",
"fastmath-2.0-cp313-cp313-manylinux_2_28_x86_64.whl",
"fastmath-2.0-cp313-cp313-win_amd64.whl",
"fastmath-1.9-cp313-cp313-manylinux_2_28_x86_64.whl",
]
supported = {"cp313-cp313-manylinux_2_28_x86_64", "py3-none-any", "py2.py3-none-any"}
def parse(f):
if f.endswith(".tar.gz"):
name, version = f[:-7].rsplit("-", 1)
return name, version, "sdist"
name, version, py, abi, plat = f[:-4].split("-")
return name, version, f"{py}-{abi}-{plat}"
def choose(project, only_binary=False):
cands = []
for f in files:
name, version, tag = parse(f)
if name != project:
continue
if tag == "sdist":
if not only_binary:
cands.append((tuple(map(int, version.split("."))), 0, f))
elif tag in supported:
cands.append((tuple(map(int, version.split("."))), 1, f))
return max(cands)[2] if cands else "no matching distribution"
print(choose("fastmath"))
print(choose("six"))
print(choose("six", only_binary=True))fastmath-2.0-cp313-cp313-manylinux_2_28_x86_64.whl
six-1.17.0.tar.gz
six-1.16.0-py2.py3-none-any.whl
Note the last two lines: the newest version wins before the wheel preference, so six 1.17.0 comes as an sdist even though an older wheel exists, unless you forbid sdists. (pip's real rule is the same: version first, then file type.)
Test yourself
What does pip have to do with an sdist that it doesn't do with a wheel?
- Verify its hash
- Build it into a wheel by running the project's build backend
- Download its dependencies
- Nothing, they're equivalent
Why did the second environment install six without rebuilding it?
- six is built into Python
- pip cached the wheel it built in ~/.cache/pip/wheels
- The index stored the built wheel
- venv copies packages between environments
Which option makes pip refuse to build anything from source?
- --no-deps
- --only-binary=:all:
- --no-build-isolation
- --prefer-binary