Previous lesson. Calculus for Machine Learning · Next lesson. Advanced Prerequisites Topics
Environment Setup for Machine Learning
Set up a local Python environment for the curriculum lessons and projects.
Table of Contents
- Installing Python
- Virtual Environments
- Installing Essential Libraries
- Jupyter Notebook Setup
- IDE Setup
- Git & GitHub Setup
- Verification
- Troubleshooting
Installing Python
Windows
Method 1: Official Installer (Recommended)
- Download Python from python.org
- Run installer
- Important: Check "Add Python to PATH"
- Click "Install Now"
- Verify installation:
python --version
# Output: Python 3.11.x
Method 2: Using Microsoft Store
# Open Microsoft Store
# Search for "Python 3.11"
# Click Install
Mac
Method 1: Official Installer
- Download from python.org
- Run installer
- Verify:
python3 --version
Method 2: Using Homebrew (Recommended)
# Install Homebrew first (if not installed)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# Install Python
brew install python
# Verify
python3 --version
Linux (Ubuntu/Debian)
# Update package list
sudo apt update
# Install Python
sudo apt install python3 python3-pip
# Verify
python3 --version
Verify Installation
# Check Python version
python --version # or python3 --version
# Check pip (package manager)
pip --version # or pip3 --version
# Should see something like:
# Python 3.11.5
# pip 23.2.1
Virtual Environments
Why Virtual Environments?
Virtual environments isolate project dependencies, preventing conflicts between different projects.
Creating Virtual Environment
Windows:
# Create virtual environment
python -m venv ml-env
# Activate
ml-env\Scripts\activate
# You should see (ml-env) in your prompt
Mac/Linux:
# Create virtual environment
python3 -m venv ml-env
# Activate
source ml-env/bin/activate
# You should see (ml-env) in your prompt
Using Virtual Environment
# Activate (do this every time you work on project)
# Windows: ml-env\Scripts\activate
# Mac/Linux: source ml-env/bin/activate
# Install packages (they'll be isolated to this environment)
pip install numpy pandas
# Deactivate when done
deactivate
Best Practices
- One environment per project
- Always activate before working
- Create requirements.txt:
pip freeze > requirements.txt
- Share requirements.txt with your project
- Recreate environment from requirements:
pip install -r requirements.txt
Recall ::
Why create a virtual environment before installing ML packages when other projects share the same machine?
Isolates package versions per project so one install does not break another project's dependencies.
Installing Essential Libraries
Core Data Science Libraries
# Activate your virtual environment first!
# NumPy - Numerical computing
pip install numpy
# Pandas - Data manipulation
pip install pandas
# Matplotlib - Plotting
pip install matplotlib
# Seaborn - Statistical visualization
pip install seaborn
# Scikit-learn - Machine learning
pip install scikit-learn
Install All at Once
# Create requirements.txt with:
numpy>=1.24.0
pandas>=2.0.0
matplotlib>=3.7.0
seaborn>=0.12.0
scikit-learn>=1.3.0
# Install all
pip install -r requirements.txt
Verify Installations
# Test in Python
python
>>> import numpy as np
>>> import pandas as pd
>>> import matplotlib.pyplot as plt
>>> import seaborn as sns
>>> from sklearn import datasets
>>> print(np.__version__) # Should print version number
>>> print(pd.__version__)
>>> # If no errors, everything is installed correctly!
Jupyter Notebook Setup
What is Jupyter Notebook?
Interactive environment for data science. Allows you to write code, see results, and add documentation in one place.
Installation
# Install Jupyter
pip install jupyter notebook
# Or install JupyterLab (more features)
pip install jupyterlab
Launching Jupyter
# Start Jupyter Notebook
jupyter notebook
# Or JupyterLab
jupyter lab
# Browser will open automatically
# If not, go to http://localhost:8888
Creating Your First Notebook
- Click "New", "Python 3"
- Write code in cells
- Press
Shift + Enterto run cell - Add markdown cells for documentation
Useful Jupyter Shortcuts
Shift + Enter: Run cell and move to nextCtrl + Enter: Run cell and stayA: Insert cell aboveB: Insert cell belowDD: Delete cellM: Convert to markdownY: Convert to code
Installing Jupyter Extensions (Optional)
# Install extensions
pip install jupyter_contrib_nbextensions
# Enable extensions
jupyter contrib nbextension install --user
IDE Setup
VS Code (Recommended)
Installation:
- Download from code.visualstudio.com
- Install Python extension
- Install Jupyter extension
Setup:
- Open VS Code
- Install extensions:
- Python (by Microsoft)
- Jupyter (by Microsoft)
- Pylance (by Microsoft)
- Select Python interpreter:
Ctrl + Shift + P(Windows) orCmd + Shift + P(Mac)- Type "Python: Select Interpreter"
- Choose your virtual environment
Using Jupyter in VS Code:
- Create
.ipynbfile - VS Code will recognize it
- Run cells with play button or
Shift + Enter
PyCharm
Installation:
- Download from jetbrains.com/pycharm
- Choose Community Edition (free)
Setup:
- Create new project
- Set Python interpreter to virtual environment
- Install packages through PyCharm's package manager
Google Colab (Cloud Alternative)
No installation needed!
- Go to colab.research.google.com
- Sign in with Google account
- Create new notebook
- Free GPU access available!
Git & GitHub Setup
Installing Git
Windows:
- Download from git-scm.com
- Run installer (use default options)
- Verify:
git --version
Mac:
# Using Homebrew
brew install git
# Or download from git-scm.com
Linux:
sudo apt install git
Initial Git Configuration
# Set your name
git config --global user.name "Your Name"
# Set your email
git config --global user.email "your.email@example.com"
# Set default branch name
git config --global init.defaultBranch main
# Verify
git config --list
GitHub Setup
- Create account at github.com
- Generate SSH key (optional but recommended):
# Generate SSH key
ssh-keygen -t ed25519 -C "your.email@example.com"
# Add to GitHub:
# 1. Copy public key: cat ~/.ssh/id_ed25519.pub
# 2. Go to GitHub → Settings → SSH Keys → New SSH Key
# 3. Paste and save
First Repository
# Create project directory
mkdir my-ml-project
cd my-ml-project
# Initialize Git
git init
# Create .gitignore
echo "ml-env/" >> .gitignore
echo "__pycache__/" >> .gitignore
echo "*.pyc" >> .gitignore
# Add files
git add .
# First commit
git commit -m "Initial commit"
# Connect to GitHub (create repo on GitHub first)
git remote add origin https://github.com/yourusername/my-ml-project.git
git push -u origin main
See Complete Git Guide for detailed Git tutorial.
Verification
Complete Setup Check
Run this Python script to verify everything:
# verification.py
import sys
print("Python version:", sys.version)
print("\nChecking libraries...")
try:
import numpy as np
print("OK: NumPy:", np.__version__)
except ImportError:
print("FAIL: NumPy not installed")
try:
import pandas as pd
print("OK: Pandas:", pd.__version__)
except ImportError:
print("FAIL: Pandas not installed")
try:
import matplotlib
print("OK: Matplotlib:", matplotlib.__version__)
except ImportError:
print("FAIL: Matplotlib not installed")
try:
import seaborn as sns
print("OK: Seaborn:", sns.__version__)
except ImportError:
print("FAIL: Seaborn not installed")
try:
import sklearn
print("OK: Scikit-learn:", sklearn.__version__)
except ImportError:
print("FAIL: Scikit-learn not installed")
print("\nSetup complete.")
Run:
python verification.py
Troubleshooting
Problem: "python is not recognized"
Solution:
- Python not in PATH
- Use
python3instead ofpython - Reinstall Python with "Add to PATH" checked
Problem: "pip is not recognized"
Solution:
# Use pip3 instead
pip3 install numpy
# Or install pip
python -m ensurepip --upgrade
Problem: "Permission denied" when installing
Solution:
- Don't use
sudowith pip in virtual environment - Make sure virtual environment is activated
- Use
--userflag if needed:pip install --user numpy
Problem: Jupyter won't start
Solution:
# Reinstall Jupyter
pip install --upgrade jupyter
# Clear Jupyter cache
jupyter --paths
# Delete cache directories if needed
Problem: Import errors in Jupyter
Solution:
- Make sure you installed packages in the same environment
- Check which Python Jupyter is using:
import sys
print(sys.executable)
Quick Start Checklist
- Python installed and verified
- Virtual environment created and activated
- Core libraries installed (NumPy, Pandas, etc.)
- Jupyter Notebook installed and working
- IDE set up (VS Code or PyCharm)
- Git installed and configured
- GitHub account created
- Verification script runs successfully
Next Steps
- Practice: Create a test notebook and import all libraries
- Explore: Try loading a dataset with Pandas
- Move Forward: Proceed to 01-python-for-data-science
You're now ready to start your ML journey!
Additional Resources
Try next: Copy the full traceback into search. Fix the first root cause, not the last symptom.
Previous lesson. Calculus for Machine Learning · Next lesson. Advanced Prerequisites Topics