TExcelTAD
Home › Python Packaging: pip, venv, Poetry & uv › poetry.lock & poetry install

poetry.lock & poetry install

pyproject.toml says what the project accepts (requests>=2.31,<3). poetry.lock says what it got: one exact version of every package in the graph, with the hashes of every file. Commit both. Then every machine that runs poetry install gets the same environment, whatever was released on PyPI in the meantime.

What's in the lockfile

bash · try it yourself
poetry new -q greet && cd greet
poetry source add -q --priority=primary local http://127.0.0.1:8080/simple/
poetry add -q "requests>=2.31,<3"
grep -c '^\[\[package\]\]' poetry.lock
sed -n '/^name = "requests"/,/^reference/p' poetry.lock
echo "..."
sed -n '/^\[metadata\]/,$p' poetry.lock
5
name = "requests"
version = "2.34.2"
description = "Python HTTP for Humans."
optional = false
python-versions = ">=3.10"
groups = ["main"]
files = [
    {file = "requests-2.34.2-py3-none-any.whl", hash = "sha256:2a0d60c172f83ac6ab31e4554906c0f3b3588d37b5cb939b1c061f4907e278e0"},
]

[package.dependencies]
certifi = ">=2023.5.7"
charset_normalizer = ">=2,<4"
idna = ">=2.5,<4"
urllib3 = ">=1.26,<3"

[package.extras]
socks = ["PySocks (>=1.5.6,!=1.5.7)"]
use-chardet-on-py3 = ["chardet (>=3.0.2,<8)"]

[package.source]
type = "legacy"
url = "http://127.0.0.1:8080/simple"
reference = "local"
...
[metadata]
lock-version = "2.1"
python-versions = ">=3.13"
content-hash = "d1a3dc295a6d2161cb02f743b48508601015dd03c2b072953cebdafd80b7d2a1"

For each package: the exact version, which groups need it (groups = ["main"]), the Python versions it supports, every file with its SHA-256, its own dependencies (with markers), and the source it came from. [metadata] holds the lock format version and a content-hash, a hash of the relevant parts of pyproject.toml. That's how Poetry notices the two files have drifted apart.

Note that Poetry resolved requests to 2.34.2, the newest in range, and recorded it. The lock is universal: it's resolved for every platform and Python version that requires-python allows, which is why Windows-only packages (like colorama for pytest) show up in locks made on Linux.

Reproducing an environment

A teammate (or CI) needs only the two files:

bash · try it yourself
poetry new -q greet && cd greet
poetry source add -q --priority=primary local http://127.0.0.1:8080/simple/
poetry add -q requests==2.31.0
sed -i 's/"requests (==2.31.0)"/"requests (>=2.31,<3)"/' pyproject.toml
poetry lock -q
grep -A1 '^name = "requests"' poetry.lock
echo "=== a fresh checkout elsewhere"
mkdir -p ../ci/src/greet && cp pyproject.toml poetry.lock ../ci/ && cp src/greet/__init__.py ../ci/src/greet/ && touch ../ci/README.md
cd ../ci
poetry install --no-root
name = "requests"
version = "2.31.0"
=== a fresh checkout elsewhere
Creating virtualenv greet in /home/learner/ci/.venv
Installing dependencies from lock file

Package operations: 5 installs, 0 updates, 0 removals

  - Installing certifi (2026.10.23)
  - Installing charset-normalizer (3.5.2)
  - Installing idna (3.20)
  - Installing requests (2.31.0)
  - Installing urllib3 (2.8.0)

The range allows 2.34.2, but poetry lock kept 2.31.0: poetry lock only resolves what changed (Poetry 2 behaviour; poetry lock --regenerate throws the old resolution away). The fresh checkout got exactly the locked versions, in an order that respects dependencies.

Updating on purpose

Command What changes
poetry lock re-lock after editing pyproject.toml, keeping existing versions where possible
poetry update re-resolve everything to the newest versions allowed, update lock and environment
poetry update requests only requests (and what it needs)
poetry lock --regenerate resolve from scratch
poetry show --outdated preview
bash · try it yourself
poetry new -q greet && cd greet
poetry source add -q --priority=primary local http://127.0.0.1:8080/simple/
poetry add -q "requests>=2.31,<3" "rich>=13"
poetry add -q requests==2.31.0 rich==13.9.4
sed -i 's/"requests (==2.31.0)"/"requests (>=2.31,<3)"/; s/"rich (==13.9.4)"/"rich (>=13)"/' pyproject.toml
poetry lock -q
poetry show --outdated
poetry update requests
poetry show --outdated
requests 2.31.0 2.34.2 Python HTTP for Humans.
rich     13.9.4 15.0.0 Render rich text, tables, progress bars, syntax highl...
Updating dependencies
Resolving dependencies...

Package operations: 0 installs, 1 update, 0 removals

  - Updating requests (2.31.0 -> 2.34.2)

Writing lock file
rich 13.9.4 15.0.0 Render rich text, tables, progress bars, syntax highlight...

Catching drift in CI

If someone edits pyproject.toml and forgets to re-lock, poetry check --lock fails, and so does poetry install:

bash · try it yourself
poetry new -q greet && cd greet
poetry source add -q --priority=primary local http://127.0.0.1:8080/simple/
poetry add -q requests
poetry check --lock
echo "--- someone adds a dependency by hand"
sed -i 's/^dependencies = \[/dependencies = [\n    "attrs (>=24)",/' pyproject.toml
poetry check --lock; echo "exit code: $?"
poetry install 2>&1 | tail -1
All set!
--- someone adds a dependency by hand
Error: pyproject.toml changed significantly since poetry.lock was last generated. Run `poetry lock` to fix the lock file.
exit code: 1
pyproject.toml changed significantly since poetry.lock was last generated. Run `poetry lock` to fix the lock file.

Put poetry check --lock (or simply poetry install, which refuses a stale lock) at the start of the CI pipeline. The error is cheap; a silently different resolution in production isn't.

Note: Should a library commit poetry.lock? Yes, for its own CI and contributors, but remember the lock is never published: users of your library resolve against your [project] dependencies ranges, not your lock. Test your ranges too (for example a CI job with --resolution lowest-direct in uv, or periodic poetry update).

Test yourself

What does content-hash in poetry.lock protect against?

  1. Tampered wheels
  2. pyproject.toml being edited without re-locking
  3. Network errors
  4. Wrong Python version

In Poetry 2, what does poetry lock do with packages that already have a locked version?

  1. Always upgrades them
  2. Keeps their locked versions where still allowed; use poetry update or --regenerate to move them
  3. Deletes them
  4. Downgrades them

Why can a poetry.lock created on Linux contain Windows-only packages such as colorama?

  1. It's a bug
  2. Poetry locks universally: for every platform and Python version allowed by requires-python
  3. colorama is needed on Linux
  4. Because of the local index