Thursday, November 12, 2026

requirements.txt vs pyproject.toml: What Each File Does and When to Use Both

Say a data team has a reporting script that ran fine for a year. A new analyst clones the repo, runs pip install -r requirements.txt, and gets a different version of pandas than everyone else, because the file says pandas>=2.2 and a new major version came out last month. The report breaks on her laptop and nowhere else.

That is the real question behind “requirements.txt vs pyproject.toml”. The short answer: pyproject.toml is the standard file for describing a Python project, including its name, Python version and the dependencies it needs. requirements.txt is a pip-specific list of packages to install, and today it works best as an exact, pinned snapshot of an environment. Modern tools such as uv, Poetry and Hatch read pyproject.toml and write a separate lock file for the exact versions. You do not have to choose one file forever, but you should know which job each one does. This guide shows real versions of each file, generated with uv 0.11 and pip 26.2.

Key Takeaways

  • pyproject.toml declares what your project needs, usually as version ranges such as pandas>=2.2. It is a Python packaging standard, read by pip, uv, Poetry, Hatch, PDM and others.
  • requirements.txt is a list of things for pip to install. It has no standard place for project metadata and is most useful as an exact, pinned list.
  • A lock file records the exact version and file hash of every package, including indirect dependencies. uv writes uv.lock, and pylock.toml is the new standard lock format from PEP 751.
  • For a new project, put dependencies in pyproject.toml and commit the lock file. Generate a requirements.txt from the lock only if a tool or platform needs one.
  • Recent pip (25.1 and later) can install dependency groups from pyproject.toml with --group, so dev tools no longer need a separate requirements file.

What Is requirements.txt?

A requirements.txt file is a plain text list of packages for pip to install, one per line. The format is defined by pip itself, not by a Python standard. The pip documentation notes that even the file name is only a convention, and that the full syntax is meant for pip, not for other tools.

Most requirements files are made one of two ways. Some are written by hand with loose ranges, like the analyst’s pandas>=2.2. Others are produced by pip freeze, which lists every package installed in the current environment. Here is the real pip freeze --exclude-editable output from an environment where we installed our example project plus its test tools:

certifi==2026.10.23
charset-normalizer==3.5.2
idna==3.20
iniconfig==2.3.1
numpy==2.5.4
packaging==26.3
pandas==3.1.0
pluggy==1.7.0
Pygments==2.21.0
pytest==9.1.1
python-dateutil==3.0.0
requests==2.34.2
six==1.17.0
tzdata==2026.5
urllib3==2.8.0

That list shows both strengths and limits of the format. Every version is pinned, which is good. But pytest, Pygments, pluggy and iniconfig are test tools, mixed in with what the report actually needs, and nothing says which packages you asked for and which came along as dependencies. There are no file hashes either, so nothing checks that the file you download is the one you tested.

What Is pyproject.toml?

pyproject.toml is the standard configuration file for a Python project, defined by Python packaging specifications (PEP 518 for the build system and PEP 621 for the [project] table). The Python Packaging User Guide has a step-by-step guide to writing your pyproject.toml. Here is the file for our example project:

[project]
name = "sales-report"
version = "0.1.0"
description = "Builds the weekly sales report"
requires-python = ">=3.12"
dependencies = [
    "pandas>=2.2",
    "requests>=2.32",
]

[project.optional-dependencies]
excel = ["openpyxl>=3.1"]

[dependency-groups]
dev = ["pytest>=8"]

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

Each table has a clear job:

  • [project] holds metadata and the runtime dependencies, as ranges. These are what the code needs, not a snapshot of one machine.
  • [project.optional-dependencies] defines extras. pip install ".[excel]" adds openpyxl only for people who export to Excel.
  • [dependency-groups] (PEP 735) holds tools for developers, such as pytest. They are not installed for users of the package.
  • [build-system] tells pip and other tools how to build the project. Many tools also read their own settings from [tool.*] tables in the same file, such as Ruff, pytest and mypy.

Notice what is missing: exact versions. pyproject.toml says “pandas 2.2 or newer”, which is right for describing the project and wrong for making two laptops identical. That second job belongs to a lock file.

requirements.txt vs pyproject.toml: Side by Side

requirements.txt pyproject.toml
Defined by pip’s own format Python packaging standards
Main job List packages to install Describe the project and what it needs
Project metadata (name, version, Python version) No Yes
Typical versions Exact pins (from pip freeze) or loose ranges Ranges
Separates runtime, optional and dev dependencies Only with several files Yes: dependencies, extras, dependency groups
Tool settings No Yes, in [tool.*] tables
Needed to build and publish a package No Yes

Where Lock Files Fit: uv.lock and pylock.toml

A lock file records the exact resolved version of every package, direct and indirect, along with where each file comes from and its hash. It is generated by a tool, committed to version control, and never edited by hand.

uv and uv.lock

uv is a Python package and project manager from Astral, written in Rust. Running uv lock on the project above resolved 19 packages and wrote uv.lock next to pyproject.toml. Here is the real entry for pandas:

name = "pandas"
version = "3.1.0"
source = { registry = "https://pypi.org/simple" }
dependencies = [
    { name = "numpy" },
    { name = "python-dateutil" },
    { name = "tzdata", marker = "sys_platform == 'emscripten' or sys_platform == 'win32'" },
]

Below that entry the file lists every pandas wheel with its URL, size and SHA-256 hash. The marker line shows something a pip freeze list cannot express: pandas pulls in tzdata only on Windows and in the browser (Emscripten). The uv documentation on locking and syncing describes uv.lock as a universal lock file: one file covers every operating system and supported Python version.

The daily commands are short:

uv add pandas            # add to [project] dependencies and update uv.lock
uv add --dev pytest      # add to the dev dependency group
uv sync                  # make .venv match uv.lock exactly
uv run python report.py  # run inside the synced environment
uv lock --upgrade        # move to newer versions on purpose

Newer releases of pandas do not change anything until you run uv lock --upgrade. That is exactly the protection the analyst in the opening was missing.

pylock.toml: The Standard Lock Format

Every tool used to have its own lock format. PEP 751, accepted in 2025, defines a shared one called pylock.toml, which records hashes for every package. pip 25.1 added an experimental pip lock command that writes it. Running pip lock -e . -o pylock.toml on our project produced a file that starts like this:

lock-version = "1.0"
created-by = "pip"

[[packages]]
name = "certifi"
version = "2026.10.23"

[[packages.wheels]]
name = "certifi-2026.10.23-py3-none-any.whl"
url = "https://files.pythonhosted.org/packages/32/d1/a3652fe2c19134d0d15922177821d863cbd7e842d53a21bbe7e8e3a1b5ac/certifi-2026.10.23-py3-none-any.whl"

[packages.wheels.hashes]
sha256 = "e9b54de76e530bb6cf0ccd036787103acb58fd42ba930ea8bf948ddbaa2f61d1"

[[packages]]

pip’s file listed 11 packages: the project and its 10 runtime dependencies. pip’s help text says the lock is only guaranteed to be valid for the Python version and platform it was created on, while uv.lock covers all platforms. uv can also write the standard format with uv export --format pylock.toml, and both pip and uv can install from a pylock.toml (pip marks that support as experimental).

When requirements.txt Still Makes Sense

A requirements.txt is still useful in a few places:

  • Platforms that expect it. Some hosting services, notebook environments and older CI setups look for a requirements.txt by name.
  • Scripts and notebooks that are not packages. A folder of analysis notebooks may not need a full project definition.
  • A pinned export for deployment. Generate it from the lock file instead of writing it by hand.

For the third case, uv exports the lock as a requirements file. With uv export --format requirements.txt --no-dev --no-hashes, our project produced:

# This file was autogenerated by uv via the following command:
#    uv export --format requirements.txt --no-dev --no-hashes
-e .
certifi==2026.10.23
    # via requests
charset-normalizer==3.5.2
    # via requests
idna==3.20
    # via requests
numpy==2.5.4
    # via pandas
pandas==3.1.0
    # via sales-report
python-dateutil==3.0.0
    # via pandas
requests==2.34.2
    # via sales-report
six==1.17.0
    # via python-dateutil
tzdata==2026.5
    # via
    #   pandas
    #   python-dateutil
urllib3==2.8.0
    # via requests

Compare it with the pip freeze output earlier: the test tools are gone, and each line says why the package is there. Drop --no-hashes for deployments, and pip will refuse any file whose hash does not match. If you deploy with uv itself, you do not need the export: uv sync --locked installs straight from uv.lock and fails if the lock is out of date.

How to Move From requirements.txt to pyproject.toml

  1. Write the [project] table. List only the packages your code imports directly, with lower bounds (for example pandas>=2.2). Leave out indirect dependencies such as numpy unless you import them yourself.
  2. Move dev tools into [dependency-groups]. pytest, linters and type checkers go in a dev group.
  3. Create a lock file. Run uv lock (or pip lock for a single platform) and commit the result.
  4. Install from the new files. With pip: pip install -e . and pip install --group dev. With uv: uv sync.
  5. Keep an exported requirements.txt only if something needs it, and regenerate it from the lock in CI so the two never drift.

Pinned, reproducible environments matter most in scheduled jobs, where an unplanned upgrade can fail overnight with nobody watching. Our guide to logging Databricks jobs to the Elastic Stack covers how to see those failures when they happen. For editor setup across a team, see our list of VS Code extensions for data science teams.

Back to the Broken Report

The analyst from the opening got a new pandas because the only file in the repo said “2.2 or newer”. With a pyproject.toml that states the range and a committed uv.lock that pins the exact versions, she runs uv sync and gets the same environment as everyone else, down to the file hashes. The team upgrades pandas when they choose to, with one command, and the report stops breaking on one laptop at a time.

Frequently Asked Questions

Is requirements.txt Deprecated?

No. pip still supports it fully, and many platforms rely on it. It is simply not the place for project metadata, and newer standards (pyproject.toml for the project, pylock.toml for locks) cover jobs it was often stretched to do.

Can I Use Both requirements.txt and pyproject.toml?

Yes, as long as one is generated from the other. Declare dependencies in pyproject.toml, lock them, and export a requirements.txt if a platform needs one. Editing both by hand leads to drift, and the uv documentation recommends against keeping a hand-maintained requirements.txt alongside uv.lock.

Does pip Install From pyproject.toml?

Yes. pip install . (or pip install -e . for development) installs the project and the dependencies in [project]. Since pip 25.1, pip install --group dev installs a dependency group. pip does not use a lock file unless you give it one, such as an exported requirements file or a pylock.toml.

Should I Use uv or Poetry?

Both read pyproject.toml and write a lock file, so either fixes the problems in this article. uv is newer and focuses on speed and on replacing several tools (pip, virtualenv, pip-tools) with one. If your team already uses Poetry and it works, there is no urgent reason to switch.

Disclaimer: The content on this website reflects the views of contributing authors and not necessarily those of Generative AI Lab. This site may contain sponsored content, affiliate links, and material created with generative AI. Thank you for your support.

Generative AI Lab mascot
Generative AI Lab Editorial Team
Articles researched, written and edited by the Generative AI Lab team. About Generative AI Lab

Must read

- Advertisement -spot_img

More articles

- Advertisement -spot_img

Latest articles