Skip to content
AgenticAIWiki
  1. Learn
  2. Part 0
  3. 1. Engineering prerequisites for AI apps
  4. 1.3

Lesson 1.3 · 3 of 7 · 6 min read · Reviewed 24 Nov 2026

Environments and packages: venv, uv and poetry

By the end, you can

  • Explain why every Python project needs its own environment.
  • Choose between venv with pip, uv and Poetry, and pin dependencies with a lock file.

AI projects pull in a lot of packages: a model SDK, an HTTP client, Pydantic, an eval library, maybe a vector databaseVector database: A database built to store embeddings and find the nearest ones to a query quickly. client. Install them all into the system Python and two projects soon need different versions of the same thing. One upgrade breaks the other.

The idea: one environment per project

A virtual environment is a folder (by convention .venv) with its own Python interpreter link and its own site-packages. When it is active, python and pip use that folder only.

python3 -m venv .venv
source .venv/bin/activate        # Windows: .venv\Scripts\activate
pip install anthropic httpx pydantic

Delete the folder and the environment is gone. Nothing else on the machine changed.

Declaring and locking dependencies

Two files do different jobs:

File Holds Written by
pyproject.toml What you depend on, with allowed ranges (httpx>=0.27) You
Lock file (uv.lock, poetry.lock) The exact version of every package, including dependencies of dependencies The tool

The ranges say what should work. The lock file says what did work. Install from the lock file in CIContinuous integration (CI): Automatically building and testing code on every change, usually in a hosted pipeline. and in production so every machine runs the same code. A plain requirements.txt with == pins can do the same job with pip, but you must keep it up to date yourself.

Three common tools

Tool What it adds Typical commands
venv + pip Built into Python. No lock file of its own python -m venv .venv, pip install -r requirements.txt
uv Very fast installs, creates the environment for you, writes uv.lock, can install Python versions uv init, uv add httpx, uv sync, uv run main.py
Poetry Dependency resolution, poetry.lock, packaging and publishing poetry init, poetry add httpx, poetry install

All three use the same underlying idea. Pick one per project and use it everywhere: mixing pip install into a uv or Poetry project gives you packages the lock file does not know about.

A worked example with uv

uv init support-agent && cd support-agent
uv add anthropic pydantic
uv add --dev pytest
uv run python main.py      # runs inside the project environment

uv add updates pyproject.toml and uv.lock together. A teammate clones the repo and runs uv sync to get the identical set.

Keys stay out of the project files

Your environment holds packages, not secrets. Put API keys in environment variables (lesson 1.5). If you use a .env file locally, add it to .gitignore next to .venv/.

Common mistakes

  • Running pip install without activating the environment, so packages land in the system Python.
  • Committing .venv/. It is large, machine-specific and rebuilt in seconds.
  • Upgrading everything at once before a release. Update the lock file deliberately and run your evalsEval: A repeatable test that measures how well an AI system performs on a set of cases with defined success criteria. after.
  • Pinning nothing. A new SDK release can change behaviour overnight.

Key points

  • A virtual environment is a folder with its own Python packages, so one project's upgrade cannot break another.
  • pyproject.toml lists what you depend on. A lock file records the exact versions that were installed, so every machine gets the same set.
  • venv plus pip is built in. uv and Poetry add dependency resolution, lock files and faster installs on top of the same idea.
  • Never commit the environment folder or a .env file. Commit pyproject.toml and the lock file.
Hands-on labOn your own machine

Set up a locked project environment

Create an isolated environment for an AI project and pin its dependencies with a lock file.

You will have: A project folder with pyproject.toml, a committed lock file, a .gitignore that excludes .venv and .env, and a script that runs inside the environment.

Steps

  1. Install uv from its official docs, or use python3 -m venv .venv with pip if you prefer the built-in tool.
  2. Run uv init lab-env, then uv add httpx pydantic to record two dependencies.
  3. Open uv.lock and find the exact versions it pinned.
  4. Create main.py that prints pydantic.VERSION and run it with uv run python main.py.
  5. Add .venv/ and .env to .gitignore, delete .venv, run uv sync and confirm main.py still runs.

Done when

  • pyproject.toml lists httpx and pydantic, and uv.lock pins exact versions.
  • Deleting and rebuilding .venv with uv sync gives the same versions.
  • git status shows no .venv or .env files to commit.

Docs: uv documentation · venv (Python docs) · Poetry documentation

Quick checkYour agent works on your laptop but fails in CI with an error from inside an SDK. Both installed from the same pyproject.toml with loose version ranges. What is the most likely fix?
How sure are you?
Pick an answer and how sure you are