Skip to content
lorebow

Dependencies

Declare every package the app needs in a file, and install them where nothing else can leak in.

KFounder and editor 201 words 1 min read Find it on the map
On this page

Explicitly declare and isolate dependencies. A twelve-factor app never relies on a package just because it happens to be installed on the machine. Two things make that true:

  • Declaration: a file in the repo lists every package and its version. For Python that's requirements.txt; for Node it's package.json.
  • Isolation: the packages are installed somewhere only this app sees, like a virtual environment or a container, so nothing from the system leaks in.

#Declare them

requirements.txttext
fastapi==0.115.0
uvicorn==0.30.6
sqlalchemy==2.0.35

Pin exact versions. "Whatever was newest when I installed it" is how two machines end up running different code.

#Isolate them

A new developer should be able to clone the repo and get a working app with the same few commands every time.

From clone to running
$ git clone https://github.com/acme/shop && cd shop
$ python -m venv .venv
$ source .venv/bin/activate
$ pip install -r requirements.txt
Collecting fastapi==0.115.0
Collecting uvicorn==0.30.6
---> 100%
Successfully installed fastapi-0.115.0 uvicorn-0.30.6 sqlalchemy-2.0.35
$ uvicorn main:app
INFO:     Uvicorn running on http://127.0.0.1:8000

The .venv folder belongs to this app alone. Delete it and run the same commands, and you get the same packages back.

#System tools count too

If the app shells out to curl or ffmpeg, that's a dependency as well, and the machine may not have it. Declare it the same way, in a Dockerfile, so every image has it:

Dockerfiledockerfile
FROM python:3.12-slim
RUN apt-get update && apt-get install -y --no-install-recommends curl \
    && rm -rf /var/lib/apt/lists/*
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["uvicorn", "main:app", "--host", "0.0.0.0"]
Your app works on your laptop but fails on the server with 'No module named requests'. What was missing?

Referenced by