Generative AIby Dhruv Narang

What Is a Virtual Environment?

Definition

A virtual environment is a private folder of Python packages for one project. When it is active, Python uses only the packages inside it, and everything you install goes inside it too.

Without
One shared set of packages
Every project uses the same packages. Two projects that need different versions of one package clash.
With a virtual environment
One set for each project
Each project has its own packages. Installing in one project never affects another.

What Is Inside the Folder?

The folder is named .venv and it sits inside your project.

your-project/
├── .venv/
│   ├── Scripts/            python and the activate script
│   ├── Lib/site-packages/  the packages you install
│   └── pyvenv.cfg
├── requirements.txt
└── your code files
Installed packages live in the folder. pip install and uv pip install put them in Lib/site-packages inside .venv, not on your computer.
Activate switches the project on. It tells the terminal to use the Python and packages of this folder. (.venv) appears in the terminal line.
It can be thrown away. Delete the folder and create it again from requirements.txt. Nothing is lost.
On macOS and Linux, Scripts is named bin and Lib is named lib.

Package Managers: pip and uv

A package manager downloads and installs Python packages for you. There are two to know.

ToolComes with Python?Install a package
pipYes, already installedpip install package
uvNo, a one-time downloaduv pip install package
uv does the same job as pip, and faster. Every uv command in these notes has a pip twin, so pip works too.
We use uv. The commands that follow are uv commands.

Install uv

uv is a one-time download. You do it once per computer.

# Windows (PowerShell)
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"

# macOS and Linux
curl -LsSf https://astral.sh/uv/install.sh | sh

# Or, with pip (any system)
pip install uv
Close the terminal and open a new one after installing, so that the uv command is found.
Check it worked: uv --version prints the version number.
Windows has WinGet? This also works: winget install --id=astral-sh.uv -e

Create and Activate

Every project gets its own environment. Create it, then switch it on.

uv venv
.venv\Scripts\activate
uv venv creates the .venv folder inside the project.
activate switches it on. (.venv) appears at the start of the terminal line.
On macOS and Linux, activate with source .venv/bin/activate.
When you finish, type deactivate.
Not active? Packages go to the computer's Python, and the project cannot find them. Always check for (.venv) before you install or run.
Windows says scripts are disabled? Run this once, then activate again: Set-ExecutionPolicy -Scope CurrentUser RemoteSigned

What Is requirements.txt?

A plain text file that lists the packages a project needs, one on each line.

mistralai
google-genai
python-dotenv
groq
requirements.txt is a list of package names, one on each line. This one belongs to a project that calls LLMs.
To install everything in the list, run this:
uv pip install -r requirements.txt
With pip: pip install -r requirements.txt
Share a project with this file, and anyone can build the same environment.

Setup in Three Commands

This is the whole setup for any project. Run it in the project folder.

uv venv
.venv\Scripts\activate
uv pip install -r requirements.txt
Taskuvpip
Create the environmentuv venvpython -m venv .venv
Install one packageuv pip install packagepip install package
Install from the fileuv pip install -r requirements.txtpip install -r requirements.txt
See what is installeduv pip listpip list
Save the list to the fileuv pip freeze > requirements.txtpip freeze > requirements.txt

Files to Keep Out of Git

Two things should stay out of Git.

.env
.venv/
.env holds your API keys. Never share it.
.venv/ is large, and it can be rebuilt from requirements.txt. Share the file, not the folder.
Put these two lines in a file named .gitignore.

Documentation

Where to read more.