uv and pyproject.toml: Start, Add, Run and Rebuild a Python Project
One tool instead of venv, activate, pip install and pip freeze. Build a small chatbot project with uv init, hit ModuleNotFoundError, fix it with uv add, read pyproject.toml and uv.lock, then delete .venv and rebuild it with uv sync.
What you will learn
- Start a project with uv init --no-package and read every line of pyproject.toml
- Run code with uv run and add a dependency with uv add
- Explain pyproject.toml (the shopping list) versus uv.lock (the receipt)
- Delete .venv and rebuild it with uv sync, and map each uv command to its venv/pip step
Before you start
- Python lambda, map, filter, Modules and Packages
- Python virtual environments with venv
The manual way to set up a project is four steps: create the toolbox with venv, activate it, install with pip, then freeze the list into requirements.txt. Each one is easy to forget, especially activate. uv folds all of those jobs into one tool: one command to start a project, one to add a package, one to run your code, and one to rebuild.
You install uv once with the one-line command from the uv docs, and uv --version tells you it's ready:
uv 0.12.9 (9f9286029 2026-09-01 x86_64-pc-windows-msvc)
If you want the quick setup tour with VS Code and API keys, there's Python project setup with uv. This article goes deeper into what uv writes and why.
Start the project
In a new empty folder called chatbot:
uv init --no-package
Initialized project `chatbot`
uv took the name from the folder. Now you have pyproject.toml (describes the project), main.py (a small hello program), .python-version (which Python the project uses), an empty README.md, and a .gitignore that already keeps .venv out of Git. Without --no-package, uv sets things up as a package you could build and publish, with a src folder. For a plain program, ask for the simple layout.
Reading pyproject.toml
[project]
name = "chatbot"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.12"
dependencies = []
It's a plain text settings file. TOML is its format: sections in square brackets, names with values. name and version identify the project; description and readme are there for when you share it; requires-python is the lowest Python version it accepts. The important one is dependencies: this is where the packages your project needs get written down, and right now there aren't any.
uv run
uv run main.py
Using CPython 3.12.14
Creating virtual environment at: .venv
Hello from chatbot!
You didn't create or activate anything. uv found a Python, made the .venv toolbox, and ran the program inside it. A file called uv.lock showed up too. uv run is the whole routine in one command: make the toolbox if it's missing, then run your file inside it.
A package that's missing
Now the chatbot's own main.py:
import requests
def reply(message):
return f"Bot heard: {message}"
print(reply("hello"))
print("requests", requests.__version__)
Traceback (most recent call last):
File "S02L02_3_UvProject\main.py", line 1, in <module>
import requests
ModuleNotFoundError: No module named 'requests'
The toolbox exists, but there's nothing in it, and the dependencies list is still empty.
uv add
uv add requests
Resolved 6 packages in 16ms
Installed 5 packages in 95ms
+ certifi==2026.10.23
+ charset-normalizer==3.5.2
+ idna==3.20
+ requests==2.34.2
+ urllib3==2.8.0
One command, two jobs. First it writes the dependency down in pyproject.toml:
dependencies = [
"requests>=2.34.2",
]
Then it installs. You asked for one package and got five, because requests needs four others to work. Run again:
Bot heard: hello
requests 2.34.2
Yours may show a newer version, and that's fine. You never install something and then have to remember to write it down.
A shopping list and a receipt
Two files now record packages, which can look odd at first. Think about going shopping. Your shopping list is short and says what you want: milk, at least one litre. The receipt is long and exact: every item, the exact size, and the extras that came along. Hand that receipt to a friend and they bring home exactly the same bag.
pyproject.toml says requests, at least 2.34.2, so anything from that version upward is okay. uv.lock records exactly what was chosen: six names, each with one exact version. That explains "resolved 6, installed 5": the six include chatbot itself. uv tree shows who needs whom:
Resolved 6 packages in 1ms
chatbot v0.1.0
└── requests v2.34.2
├── certifi v2026.10.23
├── charset-normalizer v3.5.2
├── idna v3.20
└── urllib3 v2.8.0
You don't edit uv.lock by hand; uv writes it for you.
Still a normal virtual environment
import sys
import requests
print(sys.prefix)
print(requests.__file__)
S02L02_3_UvProject\.venv
S02L02_3_UvProject\.venv\Lib\site-packages\requests\__init__.py
Nothing magic. Python runs from the project's own .venv, and requests sits in site-packages, the same place pip would put it. uv pip list shows the same five packages in the same kind of table pip list gives. uv only does the steps for you.
Delete it, rebuild it
The .venv folder is disposable. Delete it, and what's left is your code, pyproject.toml and uv.lock. That's enough:
uv sync
Using CPython 3.12.14
Creating virtual environment at: .venv
Resolved 6 packages in 1ms
Installed 5 packages in 97ms
+ certifi==2026.10.23
+ charset-normalizer==3.5.2
+ idna==3.20
+ requests==2.34.2
+ urllib3==2.8.0
Same five packages, same versions, and uv run main.py prints the same two lines as before. When a teammate gets your project, one command gives them the same toolbox.
venv and pip, side by side
| Manual way | uv |
|---|---|
python -m venv .venv + activate | handled by uv run / uv sync |
pip install requests | uv add requests (also writes pyproject.toml) |
pip freeze > requirements.txt | uv.lock, kept up to date by uv |
pip install -r requirements.txt | uv sync |
You'll meet both ways in other projects, so it's good to read both (the manual way is in lambda, map, filter and packages). Inside a uv project, use uv add rather than pip install, so the list stays correct.
What goes in Git
uv init also started a Git repository, and its .gitignore already has the .venv line. Commit your code, pyproject.toml, uv.lock and .python-version. Leave .venv out, because uv sync can rebuild it anywhere. (Git itself is covered in Git and GitHub fundamentals.)
Common mistakes
- Running
pip installinside a uv project. The package installs but never reachespyproject.toml. - Editing
uv.lockby hand. Changepyproject.tomlor useuv add, and let uv rewrite the lock. - Committing
.venv. It's big, machine-specific and rebuildable. - Forgetting
--no-packagefor a plain script project and wondering where thesrcfolder came from.
Quick recap
| Command or file | Job |
|---|---|
uv init --no-package | start a simple project |
uv add <pkg> | install and write it down |
uv run file.py | run inside the project's .venv, no activation |
pyproject.toml | what you want |
uv.lock | exactly what you got |
uv sync | rebuild .venv from the two files |
Try it yourself
Make a new project with uv and add one package. Or prompt an AI tool to do it, and then read every line it gives you.
Related course on Udemy
The Python Architect Masterclass: Full Stack to AI Engineer
Python, Django, FastAPI, React, Docker, AWS, LLMs, RAG, agents and MCP, from first steps to AI engineer.View on Udemy ↗: The Python Architect Masterclass: Full Stack to AI Engineer (paid course, opens in a new tab)Related posts
Set Up a Python AI Project with uv, VS Code and an API Key in .env
One environment for every AI project: uv brings Python 3.12 and pinned packages from uv.lock, a check script fails four times on plain python and passes after uv sync, VS Code points at .venv, the API key stays in .env, and a stand-in test model answers when there is no key.
Python try and except Basics: Catch an Error and Keep Going
An exception is the object Python creates when something goes wrong. Catch it with try and a named except, read it with as, add else and finally, retry bad input in a loop, and see why a bare except hides your bugs.
Read and Write a File in Python: open, with, Modes and Encoding
Make your data outlive the program: write notes.txt with open and with, read it back three ways, see how w wipes a file and a appends, why encoding matters, and what to do when a file isn't there.
Python Custom Exceptions and Advanced Error Handling: raise, raise from and the Call Stack
raise sends an error and except receives it. Follow an error up the call stack, use the exception family tree, re-raise and chain errors with raise from, and build InsufficientFundsError and an AppError family of your own.
