Python for AI: Setup
Python is the main programming language used for AI and machine learning. It is used to prepare data, call model APIs, train models and build AI applications. Before writing any AI code, you need a working Python installation and a virtual environment for each project.
Advitha sets up Campus Helper's data project in San Francisco this way, and the same four steps work on any computer.
What Is a Virtual Environment
A virtual environment is a private folder of Python packages for one project. It is used to keep each project's package versions separate, so installing a package for one project never breaks another.
Features of a virtual environment:
- Isolated: packages installed inside it don't affect other projects.
- Reproducible: a list of its packages can recreate it on another computer.
- Disposable: if something breaks, delete the folder and create it again.
Setup Steps
| Step | Command | Description |
|---|---|---|
| 1. Check Python | python3 --version | It is used to confirm Python 3.13 or newer is installed |
| 2. Create | python3 -m venv .venv | It is used to create a virtual environment in the .venv folder |
| 3. Activate | source .venv/bin/activate | It is used to switch the terminal to the environment (Windows: .venv\Scripts\activate) |
| 4. Install | pip install numpy pandas matplotlib | It is used to install the data packages into the environment |
| 5. Save | pip freeze > requirements.txt | It is used to record exact versions so others can recreate it |
Example: Checking the Installation
After installing, this short program confirms each package loads:
import sys
import numpy, pandas, matplotlib
print("Python", sys.version.split()[0])
print("numpy", numpy.__version__, "| pandas", pandas.__version__, "| matplotlib", matplotlib.__version__)Python 3.14.0
numpy 2.5.5 | pandas 3.1.1 | matplotlib 3.11.2Explanation: sys.version holds the Python version, and each package has a __version__ attribute. This run used Python 3.14.0, which is newer than the 3.13 the course requires; your versions may be newer still. If an import fails, the environment is probably not activated.
Common Mistakes
Installing packages without activating the environment. They go into the system Python instead.
Committing the .venv folder. It is large and specific to one computer; commit requirements.txt instead.
Mixing pip and python3 from different installations. Use python3 -m pip install ... if you're unsure which pip runs.
Interview Corner
Why use a virtual environment?
To isolate each project's packages and versions, so projects don't break each other, and so the environment can be recreated exactly from a requirements file.
How do you recreate a project's environment on another computer?
Create and activate a new virtual environment, then run pip install -r requirements.txt, using the file saved with pip freeze.
Practice
Exercise: set up your AI workspace. Create a folder called ai-practice, make a virtual environment in it, activate it, install NumPy, pandas and matplotlib, and run the version check. Then save requirements.txt, delete .venv, and recreate it from the file.
Check your understanding:
- What is a virtual environment used for?
- Which command creates one?
- Why commit
requirements.txtbut not.venv?
Key Takeaways
Quick reference
| Command | Use |
|---|---|
python3 -m venv .venv | Create |
source .venv/bin/activate | Activate (macOS, Linux) |
pip install <package> | Install |
pip freeze > requirements.txt | Save versions |
pip install -r requirements.txt | Recreate |
Was this tutorial helpful?