Packages and environments
Why this 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.
- 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
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-learngives youimport sklearn, andpip install python-dotenvgives youimport 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.