Python for AI: Setup

Updated · 4 min read
easyPython 3.13aipythonsetupvirtual-environmentspip

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.
Install, create, activate, install packages, check

Setup Steps

StepCommandDescription
1. Check Pythonpython3 --versionIt is used to confirm Python 3.13 or newer is installed
2. Createpython3 -m venv .venvIt is used to create a virtual environment in the .venv folder
3. Activatesource .venv/bin/activateIt is used to switch the terminal to the environment (Windows: .venv\Scripts\activate)
4. Installpip install numpy pandas matplotlibIt is used to install the data packages into the environment
5. Savepip freeze > requirements.txtIt is used to record exact versions so others can recreate it

Example: Checking the Installation

After installing, this short program confirms each package loads:

Python
import sys
import numpy, pandas, matplotlib
 
print("Python", sys.version.split()[0])
print("numpy", numpy.__version__, "| pandas", pandas.__version__, "| matplotlib", matplotlib.__version__)
Output
Python 3.14.0
numpy 2.5.5 | pandas 3.1.1 | matplotlib 3.11.2

Explanation: 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:

  1. What is a virtual environment used for?
  2. Which command creates one?
  3. Why commit requirements.txt but not .venv?

Quick check: Python setup

4 questions
1What is a virtual environment?
2Which command creates a virtual environment called .venv?
3What does pip freeze > requirements.txt do?
4An import fails right after installing a package. What is the most likely cause?

Key Takeaways

Quick reference

CommandUse
python3 -m venv .venvCreate
source .venv/bin/activateActivate (macOS, Linux)
pip install <package>Install
pip freeze > requirements.txtSave versions
pip install -r requirements.txtRecreate

Was this tutorial helpful?

Lesson 76 of 186