Python course · Module 6: Modules and environments · Lesson 31 of 63
Quick answer: pyproject.toml is the standard settings file of a Python project. Its [project] table holds the name, version, requires-python and dependencies, and [build-system] says which tool builds the project. pip, uv and Poetry all read it: pip comes with Python but leaves the file and the virtual environment to you, while uv and Poetry write the file, make the venv and save exact versions in a lock file. uv vs pip: learn pip first, then use uv for new projects, and keep Poetry where a team already uses it.
In this lesson you will learn:
- what
pyproject.tomlis and what[project],dependencies,requires-pythonand[build-system]mean - how to create, run and change a real project with uv (
uv init,uv add,uv run,uv.lock,uv sync) - how to install the same project with pip and venv
- how Poetry does the same job, in brief
- how uv, pip and Poetry compare, and which one to pick
Before you start: finish Lesson 30, Python Virtual Environments with venv. You also need pip from Lesson 29. We ran everything on Ubuntu 24.04 (Linux) with Python 3.14.6, uv 0.13.7 and Poetry 2.5.1, the newest uv and Poetry on PyPI on our test day. The newest Python on python.org is 3.15.2.
Table of Contents
What pyproject.toml is
The Python Packaging User Guide describes pyproject.toml as “a configuration file used by packaging tools”. It lives in the top folder of your project. One file describes the project, and many tools understand it.
The file is written in TOML, a simple settings format:
[project]starts a table, a group of settings. A table is a bit like a dictionary.name = "shop-report"is a key with a text value.dependencies = ["rich>=15.0.0"]is a list.
The guide lists four tables: [build-system] (how to build the project), [project] (the project’s details), [dependency-groups] (development tools, such as test tools) and [tool] (settings for one tool, like [tool.poetry]). Let’s see them in a real project.
Create a project with uv init
uv calls itself “an extremely fast Python package and project manager, written in Rust”. It is a separate program; its installation page shows how to install it on Windows, macOS and Linux. uv init creates a new project folder:
$ uv --version
uv 0.13.7 (x86_64-unknown-linux-gnu)
$ uv init shop-report
Initialized project `shop-report` at `/home/learner/python-course/shop-report`
$ cd shop-report
$ ls -A
.git .gitignore .python-version README.md pyproject.toml src
$ cat pyproject.toml
[project]
name = "shop-report"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
authors = [
{ name = "Learner", email = "learner@example.com" }
]
requires-python = ">=3.14"
dependencies = []
[project.scripts]
shop-report = "shop_report:main"
[build-system]
requires = ["uv_build>=0.13.7,<0.14.0"]
build-backend = "uv_build"
uv also started a Git repository (.git, .gitignore) and took the author from the Git settings of our test user, “Learner”.
The pyproject.toml file, line by line
nameandversionare required. The guide saysname“is the only field that cannot be marked as dynamic”, which means filled in later by a tool.description,readmeandauthorsare information for people. uv put a placeholder description; change it.requires-python = ">=3.14"means “this project needs Python 3.14 or newer”. The guide says it lets you “declare the minimum version of Python that you support”.dependencies = []is the list of packages your project needs. It is empty for now.[project.scripts]makes a command calledshop-reportthat runs themain()function in theshop_reportpackage.[build-system]names the tool that turns your code into an installable package, hereuv_build. The guide says this table “should always be present, regardless of which build backend you use”. The uv docs note that before uv 0.12,uv initdid not add a build system for applications, so older tutorials show amain.pyfile and no[build-system].
The code lives in src/shop_report/__init__.py. A folder with an __init__.py file is a package, as in Lesson 28.
Run it and add a dependency with uv
$ cat .python-version
3.14
$ cat src/shop_report/__init__.py
def main() -> None:
print("Hello from shop-report!")
$ uv run shop-report
Using CPython 3.14.6
Creating virtual environment at: .venv
Building shop-report @ file:///home/learner/python-course/shop-report
Built shop-report @ file:///home/learner/python-course/shop-report
Installed 1 package in 1ms
Hello from shop-report!
.python-versiontells uv which Python to use for the venv.uv runcreated the.venvvirtual environment and auv.lockfile, installed our project into the venv and ran theshop-reportcommand. There was nothing to activate.
Now add a package. rich prints nice tables in the terminal:
$ uv add rich
Resolved 5 packages in 197ms
Building shop-report @ file:///home/learner/python-course/shop-report
Built shop-report @ file:///home/learner/python-course/shop-report
Downloading pygments (1.2MiB)
Downloaded pygments
Prepared 5 packages in 133ms
Uninstalled 1 package in 0.67ms
Installed 5 packages in 9ms
+ markdown-it-py==4.2.0
+ mdurl==0.1.2
+ pygments==2.21.0
+ rich==15.0.0
~ shop-report==0.1.0 (from file:///home/learner/python-course/shop-report)
$ grep -A 3 "^dependencies" pyproject.toml
dependencies = [
"rich>=15.0.0",
]
$ ls -A
.git .gitignore .python-version .venv README.md pyproject.toml src uv.lock
One command wrote "rich>=15.0.0" into dependencies, found the three packages rich needs, installed them into .venv and updated uv.lock. + marks new packages and ~ our reinstalled project. Your times will differ.
uv run and the uv.lock file
Replace the code in src/shop_report/__init__.py with this program:
from rich.console import Console
from rich.table import Table
def main() -> None:
table = Table(title="Shop report")
table.add_column("Item")
table.add_column("Price", justify="right")
for item, price in [("Notebook", 450), ("Pen", 60), ("Backpack", 3200)]:
table.add_row(item, str(price))
Console().print(table)
$ uv run shop-report
Shop report
┏━━━━━━━━━━┳━━━━━━━┓
┃ Item ┃ Price ┃
┡━━━━━━━━━━╇━━━━━━━┩
│ Notebook │ 450 │
│ Pen │ 60 │
│ Backpack │ 3200 │
└──────────┴───────┘
$ grep -c "^\[\[package\]\]" uv.lock
5
$ grep -A 2 '^name = "rich"' uv.lock
name = "rich"
version = "15.0.0"
source = { registry = "https://pypi.org/simple" }
pyproject.toml says what you want (“rich 15.0.0 or newer”). uv.lock records what you got: the exact version of all 5 packages (our project and four from PyPI), so every computer gets the same ones. The uv project guide says uv.lock “should be checked into version control” and “should not be edited manually”.

The venv itself can always be rebuilt from the lock file. Delete it and run uv sync:
$ rm -rf .venv
$ uv sync
Using CPython 3.14.6
Creating virtual environment at: .venv
Resolved 5 packages in 0.88ms
Installed 5 packages in 7ms
+ markdown-it-py==4.2.0
+ mdurl==0.1.2
+ pygments==2.21.0
+ rich==15.0.0
+ shop-report==0.1.0 (from file:///home/learner/python-course/shop-report)
$ uv run shop-report
Shop report
┏━━━━━━━━━━┳━━━━━━━┓
┃ Item ┃ Price ┃
┡━━━━━━━━━━╇━━━━━━━┩
│ Notebook │ 450 │
│ Pen │ 60 │
│ Backpack │ 3200 │
└──────────┴───────┘
This is what a teammate does after cloning your project: uv sync, then uv run. Nothing was downloaded this time, because uv reused the packages in its cache.
The same project with pip and venv
pip reads pyproject.toml too. We copy the code and pyproject.toml (not .venv or uv.lock) into a new folder, make a venv by hand (Lesson 30) and install the project with pip install -e .. The dot means “the project in this folder”, and -e (editable) means changes to your code work without reinstalling:
$ mkdir shop-report-pip
$ cd shop-report-pip
$ cp -r ../shop-report/src ../shop-report/pyproject.toml ../shop-report/README.md .
$ python3 -m venv .venv
$ source .venv/bin/activate
(.venv) $ python -m pip install -e .
Obtaining file:///home/learner/python-course/shop-report-pip
Installing build dependencies ... done
Checking if build backend supports build_editable ... done
Getting requirements to build editable ... done
Preparing editable metadata (pyproject.toml) ... done
Collecting rich>=15.0.0 (from shop-report==0.1.0)
Downloading rich-15.0.0-py3-none-any.whl.metadata (18 kB)
Collecting markdown-it-py>=2.2.0 (from rich>=15.0.0->shop-report==0.1.0)
Downloading markdown_it_py-4.2.0-py3-none-any.whl.metadata (7.4 kB)
Collecting pygments<3.0.0,>=2.13.0 (from rich>=15.0.0->shop-report==0.1.0)
Downloading pygments-2.21.0-py3-none-any.whl.metadata (2.5 kB)
Collecting mdurl~=0.1 (from markdown-it-py>=2.2.0->rich>=15.0.0->shop-report==0.1.0)
Downloading mdurl-0.1.2-py3-none-any.whl.metadata (1.6 kB)
Downloading rich-15.0.0-py3-none-any.whl (310 kB)
Downloading pygments-2.21.0-py3-none-any.whl (1.3 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.3/1.3 MB 3.9 MB/s 0:00:00
Downloading markdown_it_py-4.2.0-py3-none-any.whl (91 kB)
Downloading mdurl-0.1.2-py3-none-any.whl (10.0 kB)
Building wheels for collected packages: shop-report
Building editable for shop-report (pyproject.toml) ... done
Created wheel for shop-report: filename=shop_report-0.1.0-py3-none-any.whl size=1664 sha256=143f69eb69fd9bd3a46593bd770839502987daac9528cb3db07299af33564daa
Stored in directory: /tmp/pip-ephem-wheel-cache-mzqf_9p2/wheels/a5/77/2c/873158bbaf35579184cd8e5edba53e1416fe50319526ab91f3
Successfully built shop-report
Installing collected packages: pygments, mdurl, markdown-it-py, rich, shop-report
Successfully installed markdown-it-py-4.2.0 mdurl-0.1.2 pygments-2.21.0 rich-15.0.0 shop-report-0.1.0
(.venv) $ shop-report
Shop report
┏━━━━━━━━━━┳━━━━━━━┓
┃ Item ┃ Price ┃
┡━━━━━━━━━━╇━━━━━━━┩
│ Notebook │ 450 │
│ Pen │ 60 │
│ Backpack │ 3200 │
└──────────┴───────┘
(.venv) $ python -m pip freeze
markdown-it-py==4.2.0
mdurl==0.1.2
Pygments==2.21.0
rich==15.0.0
# Editable install with no version control (shop-report==0.1.0)
-e /home/learner/python-course/shop-report-pip
The same pyproject.toml worked. (The speed, hash and temporary folder name vary on every run.) pip read [build-system], installed the uv_build tool to build our project, then installed rich and its helpers from dependencies.
The difference is the extra work. With pip you create and activate the venv yourself, you type new packages into dependencies by hand and run pip install -e . again, and there is no lock file. Most pip users save exact versions with pip freeze > requirements.txt. pip also has a pip lock command that writes a pylock.toml file, but the pip docs still mark it “EXPERIMENTAL”.
Poetry in brief
Poetry is another all-in-one tool that many teams use. Its docs say to install it in its own environment, for example with pipx install poetry, and never “in the environment of the project”. Poetry needs Python 3.10 or newer. Here is a new project with Poetry 2.5.1:
$ poetry --version
Poetry (version 2.5.1)
$ poetry new shop-report-poetry
Created package shop_report_poetry in shop-report-poetry
$ cd shop-report-poetry
$ cat pyproject.toml
[project]
name = "shop-report-poetry"
version = "0.1.0"
description = ""
authors = [
{name = "Learner",email = "learner@example.com"}
]
readme = "README.md"
requires-python = ">=3.14"
dependencies = [
]
[tool.poetry]
packages = [{include = "shop_report_poetry", from = "src"}]
[build-system]
requires = ["poetry-core>=2.0.0,<3.0.0"]
build-backend = "poetry.core.masonry.api"
$ poetry add rich
Creating virtualenv shop-report-poetry-VI2X_M0w-py3.14 in /root/.cache/pypoetry/virtualenvs
Using version ^15.0.0 for rich
Updating dependencies
Resolving dependencies...
Package operations: 4 installs, 0 updates, 0 removals
- Installing mdurl (0.1.2)
- Installing markdown-it-py (4.2.0)
- Installing pygments (2.21.0)
- Installing rich (15.0.0)
Writing lock file
$ grep -A 3 "^dependencies" pyproject.toml
dependencies = [
"rich (>=15.0.0,<16.0.0)"
]
$ poetry run python -c "import rich, sys; print(sys.prefix)"
/root/.cache/pypoetry/virtualenvs/shop-report-poetry-VI2X_M0w-py3.14
- Poetry 2 uses the same standard
[project]table. Its pyproject docs say “With Poetry 2.0, you should consider using theproject.dependenciessection”. Older tutorials show the Poetry 1 style,[tool.poetry.dependencies]. - Poetry-only settings go in
[tool.poetry], and the build tool ispoetry-core. poetry addwroterich (>=15.0.0,<16.0.0): by default Poetry also blocks the next major version, while uv wrote only>=15.0.0.- It wrote
poetry.lock, the same idea asuv.lock. The basic usage page says application developers should commit it. - The venv is not in the project folder. By default Poetry keeps it in its cache folder; here that is
/root/.cache/pypoetry/virtualenvs, because our test machine runs as therootuser, and theVI2X_M0wpart differs on each computer.poetry runruns a command inside it.
uv vs pip vs Poetry: comparison
| Question | pip + venv | uv | Poetry |
|---|---|---|---|
| Comes with Python? | Yes | No, install it once | No, install it once (pipx) |
| Creates the venv | You run python -m venv .venv | Automatic, .venv in the project | Automatic, in Poetry’s cache folder by default |
| Adds a package to pyproject.toml | No, edit it by hand | uv add rich | poetry add rich |
| Lock file | None by default (pip freeze, or the experimental pip lock) | uv.lock | poetry.lock |
| Run your program | Activate, then run it | uv run | poetry run |
| Installs Python itself | No | Yes, uv python install | Experimental, poetry python install |
| Build tool in our project | Any (it used uv_build here) | uv_build | poetry-core |
The uv docs claim uv is “10-100x faster than pip”; we did not measure that, but in our runs uv’s install steps took milliseconds. And poetry python install is marked “experimental” in the Poetry command docs.
Which one should you pick?
- You are learning Python: use pip and venv first. They come with Python and show you what the other tools do for you.
- You start a new project: uv is a good default. One tool makes the venv, edits
pyproject.toml, writes the lock file and can even install Python (Lesson 2 showeduv python install). - Your team already uses Poetry: keep Poetry. Since version 2 it uses the standard
[project]table.
All three read the same pyproject.toml, so you are not locked in: we installed the uv project with pip without changing a line.
Try it yourself
Exercise 1: Read pyproject.toml with Python
Python 3.11 and newer include tomllib, which reads TOML files. Write read_pyproject.py in the shop-report folder that prints the project name, requires-python, the dependencies and the build backend. The tomllib docs say the file must be opened in binary mode, "rb" (Lesson 32 explains with open()).
Exercise 2: Add a second package with uv
Add humanize to shop-report with uv, check the dependencies list, and print humanize.intcomma(3710) with uv run.
Exercise 3: Remove it again
Remove humanize with uv and check that dependencies contains only rich.
Solutions
Solution 1
import tomllib
with open("pyproject.toml", "rb") as f:
data = tomllib.load(f)
project = data["project"]
print("Name:", project["name"])
print("Python:", project["requires-python"])
print("Dependencies:", project["dependencies"])
print("Build backend:", data["build-system"]["build-backend"])
$ python3 read_pyproject.py
Name: shop-report
Python: >=3.14
Dependencies: ['rich>=15.0.0']
Build backend: uv_build
tomllib.load() turns the file into normal dictionaries and lists, so data["project"]["name"] works like any dictionary lookup.
Solution 2
$ uv add humanize
Resolved 6 packages in 78ms
Building shop-report @ file:///home/learner/python-course/shop-report
Built shop-report @ file:///home/learner/python-course/shop-report
Prepared 2 packages in 86ms
Uninstalled 1 package in 4ms
Installed 2 packages in 2ms
+ humanize==4.16.0
~ shop-report==0.1.0 (from file:///home/learner/python-course/shop-report)
$ grep -A 4 "^dependencies" pyproject.toml
dependencies = [
"humanize>=4.16.0",
"rich>=15.0.0",
]
$ uv run python -c "import humanize; print(humanize.intcomma(3710))"
3,710
uv keeps the list in alphabetical order, and uv run python runs Python inside the project’s venv.
Solution 3
$ uv remove humanize
Resolved 5 packages in 14ms
Building shop-report @ file:///home/learner/python-course/shop-report
Built shop-report @ file:///home/learner/python-course/shop-report
Prepared 1 package in 5ms
Uninstalled 2 packages in 6ms
Installed 1 package in 1ms
- humanize==4.16.0
~ shop-report==0.1.0 (from file:///home/learner/python-course/shop-report)
$ grep -A 3 "^dependencies" pyproject.toml
dependencies = [
"rich>=15.0.0",
]
uv remove updates pyproject.toml, uv.lock and .venv together; the - line shows the removed package.
Common mistakes
- Running the project with plain
python3. The packages are in.venv, sopython3 -c "import rich"fails withModuleNotFoundError: No module named 'rich'. Useuv run,poetry run, or activate the venv. - Using pip inside a uv project. uv’s venv has no pip:
uv run python -m pip --versionprinted/home/learner/python-course/shop-report/.venv/bin/python: No module named pip. Useuv add, sopyproject.tomlanduv.lockstay correct. - Editing
uv.lockorpoetry.lockby hand. Changepyproject.toml(or useaddandremove) and let the tool rewrite the lock file. - Committing
.venvbut not the lock file. It should be the other way round: commitpyproject.tomland the lock file, and let everyone rebuild the venv. - Installing Poetry into the project’s venv. Poetry’s docs say it should run in its own environment, so install it with pipx or its installer.
Frequently asked questions
Is uv better than pip?
For managing a whole project, uv does more: it creates the venv, edits pyproject.toml and writes a lock file, which pip does not do by default. pip is still the standard installer that comes with Python.
Do I still need requirements.txt if I have pyproject.toml?
Not with uv or Poetry: dependencies lists what you need and the lock file pins exact versions. With plain pip, many projects still use pip freeze > requirements.txt to pin exact versions.
Can I use pip with a project made by uv or Poetry?
Yes. Both write a standard [project] table and a [build-system] table, so pip install -e . works, as we showed. You lose the lock file’s exact versions, because pip does not read uv.lock or poetry.lock.
Summary
pyproject.tomldescribes a project:[project](name, version, requires-python, dependencies) and[build-system](the build tool).- uv:
uv initcreates the project,uv addadds packages,uv runruns it,uv.lockpins exact versions anduv syncrebuilds.venv. - pip + venv reads the same file with
pip install -e ., but you edit dependencies by hand and have no lock file by default. - Poetry 2 uses the same
[project]table,poetry add,poetry runandpoetry.lock, with the venv in its cache folder. - Learn with pip, start new projects with uv, and keep Poetry where a team already uses it.
Next lesson
Next: Lesson 32, Python Read and Write Files: open(), with and encoding.
Sources
- Writing your pyproject.toml (Python Packaging User Guide)
- uv documentation: overview
- Working on projects (uv docs)
- Creating projects with uv init (uv docs)
- Installing uv (uv docs)
- pip lock (pip docs)
- Poetry introduction and installation
- The pyproject.toml file (Poetry docs)
- Basic usage (Poetry docs)
- Commands (Poetry docs)
- tomllib (Python docs)
- Python downloads page (python.org)





