CS 212, AI Programming 1
[TOC]
Python’s real power comes from its huge collection of third-party packages, such as NumPy, TensorFlow, and Z3. Packages are installed with a package manager. Python’s official package manager is pip, which downloads packages from the Python Package Index (PyPI).
If you installed every package you used into your one system-wide copy of Python, you would eventually run into problems:
pip modify these system installations, and display an externally-managed-environment error.The solution is to give each project its own isolated environment with its own packages (and possibly its own version of Python). This is called a virtual environment.
venv and pipPython includes the venv module for creating virtual environments, and pip for installing packages into them. These are the official, built-in tools, and they work the same way on every computer that has Python.
Note that
venvis a module (a file of Python code that provides ready-made functions and tools, which Python can import or run), not a standalone program. The-moption means “run this module as a script,” so the command always starts withpython -m(orpy -m).
A virtual environment is just a folder that contains:
site-packages folder where packages installed into the environment are stored.pyvenv.cfg, that records which Python the environment was built from.By convention the folder is named .venv and is created inside the project folder. (The leading dot makes it a “hidden” folder on macOS and Linux, and many tools, including VS Code look for this name automatically.)
A typical project looks like this:
my_project/
├── .venv/ <- the virtual environment (don't edit, don't commit)
│ ├── pyvenv.cfg
│ ├── Scripts/ <- Windows: python.exe, pip.exe, activate scripts
│ ├── bin/ <- MacOS: python, pip, activate scripts
│ └── Lib/ or lib/ <- installed packages live here
├── main.py
└── requirements.txt
When the environment has been activated (see below), typing python or pip runs or uses what is inside .venv, so anything you install goes will go into that folder and only affect that project. Deleting the .venv folder removes the environment completely, and you can always make a new one.
Open a terminal, use cd to move to your project folder, and create the environment.
py -m venv .venv
(If py isn’t available, use python -m venv .venv.)
python3 -m venv .venv
The -m venv option tells Python to run its venv module, and .venv is the name of the folder to create. The environment uses the same version of Python that ran the command.
Creating the environment doesn’t use it. You must activate it in each new terminal window.
In PowerShell:
.venv\Scripts\Activate.ps1
In Command Prompt (CMD):
.venv\Scripts\activate.bat
If PowerShell says that running scripts is disabled on this system, allow locally created scripts for your user account (you only need to do this once):
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned
source .venv/bin/activate
When activation succeeds, the name of the environment appears at the start of your prompt:
(.venv) PS C:\PythonProjects\my_project>
You can confirm that you are using the environment’s Python by asking where it is:
# Windows PowerShell
Get-Command python
# MacOS
which python
The path should point inside your project’s .venv folder. Inside an activated environment, python works on macOS too—you don’t need to type python3.
With the environment active, use pip to install packages. Since pip is itself a Python module, the most reliable way to call it is through Python:
python -m pip install z3-solver
Some other useful pip commands:
python -m pip install numpy pandas # install several packages
python -m pip install numpy==2.1.0 # install a specific version
python -m pip install --upgrade numpy # upgrade a package
python -m pip uninstall numpy # remove a package
python -m pip list # show what's installed
python -m pip show numpy # details about one package
Packages installed this way are stored in .venv and are visible only when it’s active.
requirements.txtTo record exactly which packages (and versions) your project uses, save them to a text file:
python -m pip freeze > requirements.txt
Anyone else (or you, on another computer) can then recreate the same environment:
python -m venv .venv
# activate it, then:
python -m pip install -r requirements.txt
This is how most course projects and many open-source projects share their dependencies.
When you’re finished working, return to your normal shell:
deactivate
VS Code needs to know which Python to use to run and debug your code.
Ctrl+Shift+P (Cmd+Shift+P on macOS) and run Python: Select Interpreter..venv.VS Code often detects a .venv folder on its own and offers to use it. Once selected, new integrated terminals are activated automatically, and the green “Run” button and the debugger (F5) use the environment’s Python.
Never commit the .venv folder to Git. It is large, it contains files specific to your operating system and your computer, and it can be re-created from requirements.txt at any time. Add this line to your project’s .gitignore file:
.venv/
Also, don’t copy or move a .venv folder to another location or computer. It contains absolute paths and won’t work. Create a new one instead.
| Problem | Likely cause and fix |
|---|---|
ModuleNotFoundError: No module named 'z3' |
The package isn’t installed in the Python you’re running. Activate .venv (or select it in VS Code) and run python -m pip install z3-solver. |
externally-managed-environment error from pip |
You are using the system Python, not a virtual environment. Create and activate a .venv first. |
Activate.ps1 cannot be loaded because running scripts is disabled |
Run the Set-ExecutionPolicy command shown above, once. |
| Nothing happens, or the wrong Python version is used | Check the path with Get-Command python or which python. Delete .venv and re-create it with the right version of Python. |
| Packages vanished after moving the project | .venv can’t be moved. Delete it and re-create it, then run pip install -r requirements.txt. |
uvuv?uv is a modern, very fast Python package and project manager from a company called Astral. It’s written in Rust, and it’s designed to replace a whole collection of separate tools with a single command:
| Task | Traditional tool(s) | uv equivalent |
|---|---|---|
| Install and switch Python versions | pyenv and installers from python.org |
uv python install |
| Create virtual environments | python -m venv |
uv venv (or automatic) |
| Install packages | pip |
uv add, uv pip install |
| Record exact dependency versions | pip freeze, pip-tools |
uv.lock (automatic) |
| Run tools and scripts | pipx, manual activation |
uvx, uv run |
| Project metadata and dependencies | Poetry, setuptools, PDM | pyproject.toml managed by uv |
In short, uv does everything in the previous section—and more—but you rarely have to think about activating environments or running pip. It still creates a .venv folder so everything you learned above still applies.
Advantages
pip, thanks to Rust and aggressive caching.uv.lock file records the exact version of every package (including packages that your packages depend on) so everyone on a team gets the same environment.uv run automatically uses the project’s environment, creating or updating it if necessary.requirements.txt files, and uv pip install works like pip install.Disadvantages
venv and pip, uv doesn’t come with Python.uv.lock is a uv-specific file that other tools don’t understand. Some tutorials, CI systems, and hosting services still assume pip and requirements.txt.uv add (project-based) and uv pip install (pip-style) behave differently, which can be confusing to beginners. Mixing them in one project is discouraged.pip and venv which are part of the Python project itself.Run this in PowerShell:
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"
Or, if you prefer using WinGet:
winget install --id=astral-sh.uv -e
Using Homebrew (which you installed in the previous lecture notes):
brew install uv
Or use the standalone installer:
curl -LsSf https://astral.sh/uv/install.sh | sh
Verify the Installation
Close and re-open your terminal, then run:
uv --version
To update uv later, run uv self update (if you installed it with the standalone installer), or use your package manager (brew upgrade uv or winget upgrade astral-sh.uv).
Create a new project in a new folder:
uv init --no-package my_project
cd my_project
Or, to turn an existing, empty folder into a project, cd into it and run:
uv init
uv creates these files:
my_project/
├── .gitignore <- already lists .venv/
├── .python-version <- the Python version this project uses
├── main.py <- a starter "Hello World" program
├── pyproject.toml <- project settings and dependencies
└── README.md
(uv init also sets up a Git repository, unless you’re already in one.) The .venv folder and uv.lock file are created the first time you add a package or run your code.
Instead of pip install, use uv add:
uv add z3-solver
uv add numpy pandas
uv add "numpy>=2.0"
Each command does three things: it installs the package into .venv (creating it if needed), records it in the dependencies list in pyproject.toml, and updates uv.lock with the exact versions that were installed.
To remove a package:
uv remove pandas
Some packages are only needed while developing (such as testing or formatting tools). Add them to a separate dependency group:
uv add --dev pytest
To see everything that is installed:
uv tree
Use uv run instead of activating the environment and running python:
uv run main.py
Before running, uv makes sure that the environment exists and that it matches pyproject.toml and uv.lock, fixing it if it doesn’t. You can run any command inside the environment the same way:
uv run python
uv run pytest
If you do want to activate .venv the traditional way (for example, to use a tool that expects it), you still can, using the same commands described earlier (.venv\Scripts\Activate.ps1 on Windows or source .venv/bin/activate on macOS).
uv can find or download different versions of Python:
uv python list # show available and installed versions
uv python install 3.12 # download and install Python 3.12
uv python pin 3.12 # use 3.12 for this project (writes .python-version)
uv init --python 3.12 my_proj # start a new project with a specific version
These Python installations are managed by uv and are separate from any Python you installed from python.org or with Homebrew.
When you download or clone a project that already has a pyproject.toml and uv.lock (for example, from GitHub), set it up with a single command:
uv sync
uv creates .venv, installs the right version of Python if necessary, and installs the exact versions of all the packages that are recorded in uv.lock. You can then use uv run as usual.
If a project only has a requirements.txt file, you can still use uv in a pip-compatible way:
uv venv
uv pip install -r requirements.txt
| Command | What it does |
|---|---|
uv init [name] |
Create a new project |
uv add <package> |
Install a package and add it to pyproject.toml |
uv remove <package> |
Uninstall a package and remove it from pyproject.toml |
uv sync |
Make .venv match pyproject.toml and uv.lock |
uv lock |
Update uv.lock without installing anything |
uv run <command> |
Run a command or script in the project’s environment |
uv tree |
Show the dependency tree |
uv python install <ver> |
Download a version of Python |
uv python pin <ver> |
Set the Python version for the project |
uvx <tool> |
Run a command-line tool in a temporary environment (without installing it permanently) |
uv pip install <package> |
pip-compatible interface (for projects that don’t use pyproject.toml) |
TOML (Tom’s Obvious, Minimal Language) is a plain-text format for configuration files. It was designed to be easy for people to read and write. If you’ve seen JSON or INI files, TOML will look familiar. Files in this format end with the .toml extension. Python uses it for pyproject.toml, and many other tools do as well (for example, Rust’s Cargo.toml).
The basic building blocks are:
# This is a comment
# Key = value pairs
name = "my-project" # a string (always in quotes)
version = "0.1.0"
max_retries = 3 # an integer
pi = 3.14 # a float
verbose = true # a boolean (lowercase)
# Arrays (lists) use square brackets
authors = ["Ada", "Grace"]
# A table is a named group of key/value pairs, introduced by a [header]
[database]
host = "localhost"
port = 5432
# Tables can be nested using dots in the header
[database.credentials]
user = "student"
Compared with JSON, TOML allows comments, doesn’t require quotes around keys, and doesn’t need commas and braces everywhere. The table above corresponds to this JSON:
{
"name": "my-project",
"version": "0.1.0",
"max_retries": 3,
"pi": 3.14,
"verbose": true,
"authors": ["Ada", "Grace"],
"database": {
"host": "localhost",
"port": 5432,
"credentials": { "user": "student" }
}
}
Python 3.11 and later can read TOML files with the built-in tomllib module.
pyproject.toml is the standard file for describing a Python project (defined by PEP 621). After running uv init and uv add z3-solver and uv add --dev pytest, it will look something like this:
[project]
name = "my-project"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.12"
dependencies = [
"z3-solver>=4.13.0",
]
[dependency-groups]
dev = [
"pytest>=8.3.0",
]
The sections are:
[project]—Basic information about your project.
name, version, description—Descriptive metadata.requires-python—Which versions of Python the project works with.dependencies—The packages your project needs, each with an optional version specifier: "numpy" (any version), "numpy>=2.0" (2.0 or newer), "numpy==2.1.0" (exactly that version), or "numpy>=2.0,<3" (a range).[dependency-groups]—Extra sets of dependencies, such as dev for development-only tools.[tool.*]—Settings for individual tools. Each tool uses its own table name, for example [tool.pytest.ini_options] or [tool.ruff]. You can add these sections yourself.[build-system]—Describes how to package the project. It’s only needed if you plan to build and publish a package, and uv init doesn’t add it for a simple application.You can edit this file by hand. After doing so, run uv sync (or uv run) to update the environment to match.
pyproject.toml lists the packages you asked for, usually with flexible version ranges. uv.lock records the exact version of every package that was installed, including the packages that your packages depend on, along with checksums for each. It is also a TOML file, but it’s generated by uv, so don’t edit it by hand.
| File | You edit it? | Commit to Git? | Purpose |
|---|---|---|---|
pyproject.toml |
Yes (or let uv add do it) |
Yes | What the project needs |
uv.lock |
No | Yes | Exactly what was installed, so others get the same thing |
.python-version |
Rarely | Yes | Which Python version to use |
.venv/ |
No | No | The installed environment (can be re-created) |
uv creates a .venv folder in your project, so VS Code finds it the same way as in the previous section: open the project folder, run Python: Select Interpreter, and choose the .venv interpreter. After that, the debugger and F5 work normally.
Note: Parts of this document were drafted with assistance from Claude Sonnet 5.5 and reviesed by Brian Bird 10/17/2026
AI Programming Course Materials by Brian Bird, written in are licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.