Single logo
  • Courses
  • Blog
  • About
Join on SkoolJoin on Skool

In this topic

Python
  • Python for AI: The Five Tools You Actually Need
  • Thinking Like a Python Architect in an AI-First World
  • Your First Python Program: print, Comments, sep and end
  • Set Up PyCharm for Python: Install, First Project and First Run
  • Run a Python File and Read the Error: Tracebacks in Three Steps
  • Python Data Types, Variables and Strings: Indexing, Slicing and f-strings
  • Python Operators: Arithmetic, Comparison, Logical, Membership, Identity and Precedence
  • Python Conditionals and Loops: if/elif/else, for, while, break and continue
  • Looping Over Real Data in Python: Dicts, Lists of Dicts, enumerate and zip
  • Checking AI-Written Python Code: A Six-Step Habit That Finds Three Hidden Faults
  • Python Basics Practice: Report Card, Shopping Cart and Password Checker from a Prompt
  • Python Functions: Parameters, Return, Scope (LEGB), *args and **kwargs
  • Type Hints on Python Functions: Parameters, Returns, None and Collections
  • Python lambda, map, filter, Modules and Packages (with pip and venv)
  • Python Functions Practice: Bonus Calculator, Product Pipeline and a Math Package
  • Python Lists, Tuples, Sets and Dicts, Plus Type Hints on Variables
  • Python Comprehensions: List, Dict, Set and Generator Expressions, Read as Loops
  • Python Class Methods, Static Methods and Properties: What Each One Receives
  • Python Classes and Objects: __init__, self and PEP 8 Names
  • Python Inheritance and Polymorphism: super(), Overriding and Duck Typing
  • Python Multiple Inheritance, the MRO and Mixins
  • Python Encapsulation and Special Methods: Underscores, __str__, __add__ and __len__
  • Python Composition, Abstract Classes and Dataclasses
  • Python OOP Practice: Design a Bank, an Employee System and a Library
  • Set Up a Python AI Project with uv, VS Code and an API Key in .env
  • uv and pyproject.toml: Start, Add, Run and Rebuild a Python Project
  • Python try and except Basics: Catch an Error and Keep Going
  • Read and Write a File in Python: open, with, Modes and Encoding
  • Python Custom Exceptions and Advanced Error Handling: raise, raise from and the Call Stack
  • Python Logging and Debugging: Levels, Handlers, logger.exception and breakpoint()
  • Python File Modes, pathlib and Context Managers: x, r+, rb, Path and __enter__/__exit__
  • Python CSV, JSON and Large Files: csv, json, yield Pipelines, tomllib and pickle
  • Python Practice: Build a Log File Analyzer and a File Organizer
  • Python Project Structure for AI-Assisted Development: Packages, config.py and AGENTS.md
  • Regular Expressions in Python: search, findall, Groups and Greedy vs Lazy
  • Testing Python Code with pytest: assert, parametrize, fixtures and pytest.raises
  • Async Python: async def, await, asyncio.gather and the Blocking Trap
  • Django, FastAPI or Flask? The Same Blog Feature in Three Shapes, and How to Choose
  • One Blog, Three Frameworks: Routing, Models, Templates and Layout in Django, FastAPI and Flask
  • Getting Started with Flask: Routes, Jinja2 Templates and WSGI in One Small App
  • Your First Flask App with uv: From an Empty Folder to a Page in Debug Mode
  • Flask Routes, Templates and url_for: Converters, Forms, Redirects and the Signed Session
  • Flask-SQLAlchemy Models and Flask-Migrate: From a Python Class to a Real Table
  • Building a Blog with an AI Coding Agent: Plan Mode, a Master Prompt and the First Review
  • Handing a Figma Design to an AI Agent: Compare the Files, Then Review the Change
  • Inside a Flask Blog: The App Factory, Blueprints, the Instance Folder and CORS Done Right
  • How a Request and a Login Flow Through a Flask Blog: Flask-Login, CSRF and the Signed Session
  • Testing a Flask Blog with pytest: A Test App from the Factory, Fixtures and the Test Client
  1. Blog
  2. /
  3. Python
  4. /
  5. Modules, Files and Errors

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.

Chaand Sheikh

·27 November 2026·Updated 28 November 2026Modules, Files and Errors

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 is the shopping list with requests at least 2.34.2; uv.lock is the receipt with six exact versions; uv sync turns the two files into the .venv folder pyproject.toml the shopping list requests>=2.34.2 uv.lock the receipt certifi 2026.10.23 charset-normalizer 3.5.2 chatbot 0.1.0 idna 3.20 requests 2.34.2 urllib3 2.8.0 uv sync .venv disposable
pyproject says what you want; the lock says what you got. Commit both, rebuild .venv anywhere.

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 wayuv
python -m venv .venv + activatehandled by uv run / uv sync
pip install requestsuv add requests (also writes pyproject.toml)
pip freeze > requirements.txtuv.lock, kept up to date by uv
pip install -r requirements.txtuv 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 install inside a uv project. The package installs but never reaches pyproject.toml.
  • Editing uv.lock by hand. Change pyproject.toml or use uv add, and let uv rewrite the lock.
  • Committing .venv. It's big, machine-specific and rebuildable.
  • Forgetting --no-package for a plain script project and wondering where the src folder came from.

Quick recap

Command or fileJob
uv init --no-packagestart a simple project
uv add <pkg>install and write it down
uv run file.pyrun inside the project's .venv, no activation
pyproject.tomlwhat you want
uv.lockexactly what you got
uv syncrebuild .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.

Tried the exercise? Share your answer or ask a question.

The StudyEasy AI Community is free to join, with the lesson files, notebooks and code.

Join free on Skool

Keep going in Python · 26 of 48

Previous
Set Up a Python AI Project with uv, VS Code and an API Key in .env
Next post
Python try and except Basics: Catch an Error and Keep Going

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.

On this page

  • Start the project
  • Reading pyproject.toml
  • uv run
  • A package that's missing
  • uv add
  • A shopping list and a receipt
  • Still a normal virtual environment
  • Delete it, rebuild it
  • venv and pip, side by side
  • What goes in Git
  • Common mistakes
  • Quick recap
  • Try it yourself
Community · Free to join
Single logo

StudyEasy AI Community

Hosted onSkool

Learn AI engineering, share what you build, and connect with other learners.

  • Free resources: lesson files, notebooks and code

  • Ask questions and join engineering discussions

  • Meet real people learning and building with AI

Join free on Skool

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)

Single logo

Empowering Education, One Course at a Time

About
About usContact us
Legal
Terms of UsePrivacy Policy

© 2026 StudyEasy Organization. All rights reserved.