Install Python & Set Up Your Environment
Before you write a single line of code, you need the right tools. This step makes sure Python works on your machine and you can run scripts.
Learning Objectives
- Install Python 3.11 or newer
- Verify the installation works from your terminal
- Install and configure VS Code as your code editor
- Create your first project folder
- Create and activate a virtual environment
- Run a Python script and see the output
- Install a package with pip and save it to requirements.txt
Install Python
Go to python.org/downloads and download the latest Python 3.11+ installer for your OS. During installation, check the box that says "Add Python to PATH" — this is critical. Without it, your terminal won't recognize the python command.
Windows Users: Check the PATH box!
The Windows installer has a checkbox at the bottom: "Add python.exe to PATH". This is unchecked by default. If you miss it, you'll get 'python' is not recognized errors. If you already installed without it, reinstall and check the box.
After installation, open your terminal and verify:
# Check Python version
python --version
# Expected output (your version may differ):
# Python 3.11.5
# Also check pip (Python's package manager)
pip --version
# Expected output:
# pip 23.3.1 from ...
macOS users
macOS may map python to an old Python 2. If python --version shows Python 2.x, try python3 --version. If that works, you can create an alias: add alias python=python3 to your ~/.zshrc file.
Install VS Code
Download Visual Studio Code — it's free, lightweight, and the most popular editor for Python. After installing, open VS Code and install these extensions:
Python # by Microsoft — core Python support
Pylance # by Microsoft — fast language server, autocomplete
Python Debugger # by Microsoft — debugging support
Install extensions by clicking the Extensions icon (square icon in left sidebar) or pressing Ctrl+Shift+X, then search for each name and click Install.
Alternative editors
If you already use PyCharm, Neovim, or another editor, that's fine. VS Code is recommended because it's what most AI engineering teams use, but any editor that supports Python will work.
Create Your Project Folder
Every project needs its own folder. This keeps your code organized and separate from other projects.
# Create a projects directory (if you don't have one)
mkdir -p ~/projects
# Create your Python project folder
mkdir ~/projects/python-basics
cd ~/projects/python-basics
# Open it in VS Code
code .
Create and Activate a Virtual Environment
A virtual environment is an isolated Python environment for your project. It lets you install packages without affecting other projects. This is the most important habit to build from day one.
# Create a virtual environment named 'venv'
python -m venv venv
# Activate it (macOS/Linux)
source venv/bin/activate
# Activate it (Windows)
venv\Scripts\activate
# You should see (venv) in your terminal prompt:
# (venv) user@host:~/projects/python-basics$
Always activate before working!
Every time you open a new terminal to work on this project, you need to run source venv/bin/activate again. If you forget, pip install will install to your system Python instead of the project — this causes version conflicts.
Never commit the venv/ folder to Git. Add it to .gitignore:
venv/
__pycache__/
*.pyc
.env
Run Your First Python Script
Create a file called hello.py in your project folder:
# My first Python script
print("Hello, AI Engineering!")
# Python can do math
print(f"2 + 3 = {2 + 3}")
# Python can use system info
import sys
print(f"Python version: {sys.version}")
Run it from your terminal (make sure venv is activated):
python hello.py
# Expected output:
# Hello, AI Engineering!
# 2 + 3 = 5
# Python version: 3.11.5 ...
If you see the output above, Python is working correctly. 🎉
Install Your First Package
Python has a massive library of third-party packages. You install them with pip. Let's install requests — you'll use it constantly to call APIs.
# Install a package
pip install requests
# Save your project's dependencies
pip freeze > requirements.txt
# requirements.txt now contains:
# requests==2.31.0
# certifi==2023.7.22
# ...
# Later, anyone can recreate your environment:
pip install -r requirements.txt
Why requirements.txt?
When you share code or deploy an app, you need to specify exactly which packages (and versions) it depends on. requirements.txt is that list. Without it, "works on my machine" becomes "broken in production."
🧪 Exercises
Exercise 1 — Print Your Info
Create about_me.py that prints your name, favorite programming language, and one thing you want to build with AI. Use f-strings.
Exercise 2 — System Info
Create sys_info.py that imports sys and prints the Python version, the platform you're on (sys.platform), and the path where Python looks for modules (sys.path).
Exercise 3 — Install & Verify
In a fresh virtual environment, install requests and python-dotenv. Run pip freeze > requirements.txt. Then delete your venv, recreate it, and install from requirements.txt to prove it works.
⚠️ Common Mistakes
Forgetting to activate venv
You install packages, but they go to the global Python. When your code runs later (with venv activated), it can't find them. Always check: do you see (venv) in your prompt?
Using Python 2
On macOS, python may point to Python 2. Always use python3 or create an alias. Python 2 has been deprecated since 2020.
Not saving requirements.txt
You install 10 packages, your code works, and you forget to freeze. Three weeks later you can't reproduce the environment. Run pip freeze > requirements.txt after every install.