Python packaging and dependencies
A Python project describes itself in pyproject.toml, runs in its own virtual environment, and ships as a wheel. A library says which versions it can work with (ranges). An application locks the exact versions it was tested with. Mixing these two jobs is the cause of most "it works on my machine" and "I cannot install it" problems. A specialist sets this up so every laptop, CI run and server installs the same thing.
Author: bezzad
Contents
The problem: a Friday deploy that broke by itself
Our shop has three Python services: orders, payments and the price import. They share an internal library, shop-common, with money and tax code.
On Friday, nobody changed the orders service, but the new Docker image failed at start. A dependency had a new major version, and pip installed it because the service had only a requirements.txt with names and no versions.
On the same day, the payments team could not install shop-common at all. pip said: “Cannot install shop-common and httpx because these package versions have conflicting dependencies”. shop-common asked for one exact version of httpx, and payments needed another.
Two opposite mistakes: one project had no versions at all, the other had versions that were too strict. Both come from not knowing the difference between a library and an application.
The words you need
- Package (import package): a folder with Python modules that you import, like shop_common.
- Distribution: the thing you install with pip, like shop-common. One distribution can contain several import packages.
- sdist (source distribution): a .tar.gz with the source. The installer must build it first.
- Wheel: a .whl file that is already built. Installing it is just unzipping into the right place. It is fast and needs no compiler.
- Index: a server with distributions, like PyPI or your company’s private index.
- Installer: pip or uv. It picks versions (the resolver) and installs them.
- Build backend: the tool that makes the sdist and the wheel, like hatchling or setuptools.
One file: pyproject.toml
Today, every project should describe itself in pyproject.toml. setup.py with code inside is the old way.
[project]
name = "shop-common"
version = "2.4.0"
requires-python = ">=3.12"
dependencies = [
"httpx>=0.27,<1",
"pydantic>=2.6,<3",
]
[project.optional-dependencies]
redis = ["redis>=5"]
[project.scripts]
shop-check = "shop_common.cli:main"
[dependency-groups]
dev = ["pytest>=8", "ruff"]
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
- [project] is the standard metadata: name, version, the Python versions it supports, and what it needs to run.
- optional-dependencies are extras. A user can install shop-common[redis] to also get Redis support.
- project.scripts creates a command. After install, shop-check runs the main function.
- dependency-groups are for the developers of this project only, like test tools. They are not installed for users of the library.
- [build-system] says which tool builds the package.
Use the src layout
shop-common/
pyproject.toml
src/
shop_common/
__init__.py
money.py
tests/
test_money.py
With the code inside src, your tests cannot import the folder by accident. They import the installed package, the same as your users. So a missing file in the wheel is found in CI, not in production.
One virtual environment per project
A virtual environment is a folder with its own Python and its own packages. Never install project packages into the system Python.
python -m venv .venv
source .venv/bin/activate
python -m pip install -e .
- python -m pip makes sure you use the pip of this Python, not another one on the PATH.
- -e (editable install) points to your source folder, so changes are seen without installing again.
- uv does the same work much faster: uv venv, uv pip install, or uv sync for the whole project.
Library or application: ranges or a lock
This is the most important idea in this lesson.
- A library (shop-common) is installed next to other code that it does not control. It must say what versions it can work with: wide ranges, like
httpx>=0.27,<1. If every library pins one exact version, two libraries soon want different versions and nothing can be installed. - An application (the orders service) is the final product. It must say exactly what it was tested with: every package, also the indirect ones, with an exact version and a hash. This is a lock file.
Common lock tools:
uv lock # writes uv.lock from pyproject.toml
uv sync --frozen # installs exactly what uv.lock says
pip-compile --generate-hashes requirements.in # pip-tools
pip install --require-hashes -r requirements.txt
- The lock file goes into git. A change to it is a reviewed change, like code.
- Hashes protect you: if someone replaces a file on the index, the install fails instead of running other code.
- A bot (like Dependabot or Renovate) can update the lock every week, so CI tests new versions before they reach production.
- pylock.toml (PEP 751) is a new standard lock format that several tools are starting to support.
Version numbers
Most Python projects use a form of semantic versioning: MAJOR.MINOR.PATCH.
- MAJOR may break your code. MINOR adds features. PATCH fixes bugs.
- A range like
>=2.6,<3means “any 2.x from 2.6”. This is a good default for a library. ~=2.6is short for>=2.6,<3.0. Be careful:~=2.6.1means>=2.6.1,<2.7.- Do not set upper limits that are too tight in a library (like
<2.7). It blocks your users from bug fixes. Many maintainers only set an upper limit on the next major version, or none at all. - Not every project follows semantic versioning. Read the changelog before a big update.
Wheels and native code
A pure Python package has one wheel for all systems (its name ends in py3-none-any.whl). A package with C or Rust code needs a wheel for each Python version and each system, for example cp314-manylinux_2_28_x86_64.
- If no wheel fits, pip downloads the sdist and tries to build it. On a slim Docker image without a compiler, this fails or takes many minutes.
- Check that your dependencies have wheels for your Python version before you upgrade Python. A brand-new Python version often waits a few weeks for wheels.
- The free-threaded build of Python needs its own wheels (with a t in the tag, like cp314t).
- Alpine Linux uses musl, not glibc, so it needs musllinux wheels. Many packages build slower or not at all there. A slim Debian image is often easier.
Build and publish
uv build # or: python -m build
# dist/shop_common-2.4.0.tar.gz
# dist/shop_common-2.4.0-py3-none-any.whl
- Build in CI from a git tag, not on a laptop. Then each version on the index matches one commit.
- Test the built wheel, not only the source folder.
- Upload with Trusted Publishing on PyPI: CI proves who it is with a short-lived token (OIDC), so there is no long-lived password to leak.
- Internal libraries go to a private index (for example a package registry from your Git host or cloud). Never upload company code to the public PyPI by mistake.
- A version on the index never changes. If 2.4.0 is broken, release 2.4.1. You can “yank” 2.4.0, so new installs skip it.
In Docker
FROM python:3.14-slim
COPY --from=ghcr.io/astral-sh/uv:0.9 /uv /bin/uv
WORKDIR /app
COPY pyproject.toml uv.lock ./
RUN uv sync --frozen --no-dev --no-install-project
COPY src ./src
RUN uv sync --frozen --no-dev
CMD ["/app/.venv/bin/python", "-m", "orders"]
- Copy the lock file first and install, then copy the code. Docker keeps the install layer in cache until the lock changes.
- –frozen fails if the lock does not match pyproject.toml, instead of quietly picking new versions.
- –no-dev leaves out the test tools.
Common mistakes
| Mistake | Result | Better way |
|---|---|---|
| An application with no versions | A new release breaks a deploy that changed nothing. | Lock every package with exact versions and hashes. |
| A library with exact pins | Users cannot install it next to other libraries. | Use ranges in a library. Lock only in the application. |
| Installing into the system Python | Projects break each other and the OS tools. | One virtual environment per project. |
| Tests import the source folder | A file missing from the wheel is found in production. | Use the src layout and test the installed package. |
| Building on a laptop and uploading | Nobody knows which code is in the package. | Build and publish from a tag in CI. |
| Changing a published version | Two machines get different files for the same version. | Release a new version and yank the broken one. |
Summary in 7 lines
- Describe every project in pyproject.toml: metadata, dependencies, extras, dev groups and the build backend.
- One virtual environment per project. Use python -m pip, or uv.
- A library declares wide ranges. An application locks exact versions with hashes.
- Put the lock file in git and update it with a bot, so CI tests new versions first.
- Use the src layout and test the built wheel.
- Native packages need a wheel per Python version and system. Check before you upgrade Python.
- Build and publish from a git tag in CI with Trusted Publishing. Never change a published version.