Index Ai Academy
Phase 0 · Tools · Developer tools

Packages and environments

Install libraries with pip and keep projects isolated with venv.

About 25 minutes · Free lesson

Start this lesson

Why it matters

Nearly all AI work in Python runs on packages other people wrote: anthropic and openai for model APIs, numpy and pandas for data, torch and transformers for training models. The first lines of almost every AI project's setup install them. Once you know how packages and environments work, "it doesn't work on my machine" stops being a mystery. It becomes a two-minute fix.

What you'll be able to do

  • Install a package from PyPI with pip and import it
  • Create, activate and leave a virtual environment
  • Read and use a requirements.txt file
  • Diagnose a ModuleNotFoundError

What it covers

  1. The standard library and everything else
  2. python3 and pip on a Mac
  3. One environment per project
  4. requirements.txt
  5. Worked example: set up a project you just downloaded
  6. Two surprises when installing
  7. Keep .venv out of git

The standard library and everything else

In the modules lesson you imported modules such as math. Modules like that ship with Python, as part of its standard library, so they're always there. Everything else is a package: code someone published for others to install.

Think of an app store for code. Python's public package index is PyPI (the Python Package Index, at pypi.org). It holds hundreds of thousands of packages. The tool that downloads and installs them is pip:

pip install requests

After that, import requests works in your code. Import something that isn't installed and Python stops with a ModuleNotFoundError:

import gradient_magic_toolkit

That error almost always means one of two things. Either the package isn't installed, or it's installed for a different Python than the one running your code. The second one is what the rest of this lesson is about.

Note: The name you install isn't always the name you import. pip install scikit-learn gives you import sklearn, and pip install python-dotenv gives you import dotenv. The package's page on PyPI shows both.

The academy's browser Python already includes the packages lessons need, such as numpy and a version of requests. So you won't install anything here, except in the simulator below.

Quick check
You run pip install scikit-learn, and then import scikit-learn fails. What should the line in your code say?
  1. import scikit_learn
  2. You have to reinstall it under a shorter name
  3. import sklearn
  4. pip import scikit-learn
Show the answer

import sklearn

The name you install isn't always the name you import. The package's page on PyPI shows both.

python3 and pip on a Mac

macOS comes with python3, or offers to install it the first time you type it. You may also have installed newer versions, with the installer from python.org or with Homebrew. Each Python has its own set of installed packages, so it matters which one runs.

python3 --version
which python3

which shows the path of the Python a command will run. Say you install a package, and Python still can't find it. The usual cause is that pip installed it for a different Python than the one running your script. Virtual environments end that confusion.

One environment per project

Imagine two projects. An older reporting script needs numpy version 1.26. A new AI project needs numpy 2.x. If every package is installed in one shared place, only one version can be there, and one project breaks.

The fix is like giving each project its own toolbox. A virtual environment (venv) is a folder inside your project that holds its own set of packages. While it's activated, python and pip in that terminal point at the environment. So whatever you install goes into this project only.

python3 -m venv .venv           # create an environment in a folder called .venv
source .venv/bin/activate       # activate it in this terminal
pip install requests            # installs into .venv only
deactivate                      # go back to the system Python

When an environment is active, your prompt usually starts with its name, like (.venv) sam@MacBook sentiment %. Inside it you can type python and pip. Outside it, stick to python3. Activation lasts only for that terminal window, so you activate again each time you open a new one. (On Windows, the activate command is .venv\Scripts\activate.)

Two rules of thumb:

  • One environment per project, created in the project folder, usually named .venv.
  • Never commit it to git. It's large and specific to your machine. Add .venv/ to .gitignore, and let others rebuild it from requirements.txt.
Try it now

After activating, which python points inside .venv, and pip list shows an almost empty environment.

Quick check
You activate project A's virtual environment and run pip install numpy. Later you open a terminal in project B and activate B's environment. Can project B import numpy?
  1. Yes, as long as both projects use python3
  2. No, numpy was installed only into A's environment
  3. Yes, pip installs packages for every project on the computer
  4. Only after restarting the computer
Show the answer

No, numpy was installed only into A's environment

That isolation is the whole point of a virtual environment. B needs its own pip install numpy.

requirements.txt

A project records the packages it needs in a file called requirements.txt. It's like a shopping list: one package per line, usually with versions:

requests==2.32.3
numpy>=2.0
anthropic
  • == pins an exact version.
  • >= sets a minimum.
  • A bare name means "whatever is newest".

Pinning versions makes a project reproducible. Someone installing it next year gets the same code you tested with. That matters in AI work, where libraries change quickly, and an upgrade can change behavior.

Two commands work with the file:

pip freeze                         # print every installed package with its exact version
pip install -r requirements.txt    # install everything the file lists

The usual flow for a project you've just cloned: create a venv, activate it, then pip install -r requirements.txt. The simulator above supports both forms.

Note: You'll also meet other tools that do this same job: uv (a fast pip replacement), conda (common in data science) and poetry. The ideas carry over: an isolated environment, plus a file listing what to install. In Jupyter notebooks you'll see %pip install name, which installs into the notebook's environment.

Quick check
What does this print?
line = "requests==2.32.3"
name, version = line.split("==")
print(version)
  1. 2
  2. requests==2.32.3
  3. requests
  4. 2.32.3
Show the answer

2.32.3

split("==") cuts the line in two at the ==, so name gets requests and version gets 2.32.3. A pinned line names one exact version, so everyone installs the same thing.

Quick check
Your script stops with ModuleNotFoundError: No module named 'requests', but you're sure you ran pip install requests yesterday. What's the most likely cause?
  1. You need to write import Requests with a capital R
  2. The project's virtual environment isn't activated in this terminal, so a different Python is running
  3. requests was removed from PyPI overnight
  4. pip only installs packages for one day
Show the answer

The project's virtual environment isn't activated in this terminal, so a different Python is running

Activation lasts for one terminal window. Open a new terminal, forget source .venv/bin/activate, and the system Python runs without your packages.

Worked example: set up a project you just downloaded

A teammate shares a sentiment project. Its README says "install the requirements and run analyze.py". Here's the whole setup. Lines starting with % are what you type, and the output is trimmed:

% cd ~/projects/sentiment
% python3 -m venv .venv
% source .venv/bin/activate
(.venv) % which python
/Users/sam/projects/sentiment/.venv/bin/python
(.venv) % pip install -r requirements.txt
Successfully installed anthropic requests numpy ...
(.venv) % python analyze.py
Positive: 41  Negative: 9

Step by step:

  1. python3 -m venv .venv creates the project's own environment.
  2. source .venv/bin/activate switches this terminal to it, and the prompt gains (.venv).
  3. which python proves the right Python will run: the path is inside .venv.
  4. pip install -r requirements.txt installs the exact list the project needs.
  5. Now python analyze.py runs with everything it imports.

Tomorrow, in a new terminal, you only need steps 2 and 5.

Quick check
requirements.txt has the line numpy==1.26.4. What does that do?
  1. Installs version 1.26.4 or anything newer
  2. Installs the newest numpy and ignores the number
  3. Installs exactly version 1.26.4, so everyone gets the version the project was tested with
  4. Checks numpy is installed, without installing it
Show the answer

Installs exactly version 1.26.4, so everyone gets the version the project was tested with

== pins an exact version. >= would allow newer ones.

Two surprises when installing

The name you install isn't always the name you import. You install scikit-learn but write import sklearn, so a ModuleNotFoundError doesn't always mean it's missing. And activating an environment only lasts for that terminal window, so a new window needs activating again.

Keep .venv out of git

The .venv folder holds thousands of files and only works on your own computer. Commit requirements.txt, and let others rebuild it.

Practice what you just read

  • 1 code example you can edit and run
  • a practice exercise that checks your code
  • a 5-question quiz
  • 5 review cards for spaced repetition

Start this lesson

Try a quiz question

What does pip install -r requirements.txt do?

  1. Installs every package listed in requirements.txt
  2. Removes the packages listed in requirements.txt
  3. Writes your installed packages into requirements.txt
  4. Reads the file and prints it

Answer it at the end of the lesson to see if you're right, and why.

Before this lesson