Appendix E: Python Environment Setup Guide
This guide walks you through setting up a Python environment for the optional code examples in this textbook. Python is entirely optional. Every chapter works without running any code. The code examples are there for readers who want to see concepts in action computationally — you will not miss any learning objectives by skipping them.
E.1 Why Python Is Optional
This is a textbook about AI literacy, not programming. The core skills — evaluating claims, identifying bias, understanding how AI systems work and fail, analyzing stakeholder impacts — do not require writing a single line of code.
That said, some concepts click more deeply when you can interact with data and models directly. Seeing how a training-test split works in practice, watching a classifier struggle with ambiguous inputs, or running a simple data exploration can make abstract ideas concrete.
The optional code examples in this book use Python because it is the dominant language in AI and data science, it has excellent libraries for the tasks we cover, and it has a gentle learning curve for basic tasks. If you have never programmed before, you can still follow the examples — each one is short (typically 5-15 lines), heavily commented, and designed to illustrate a concept rather than teach programming.
E.2 Three Ways to Run Python
You do not need to install anything on your computer if you do not want to. Here are three options, from simplest to most involved:
Option 1: Google Colab (Recommended for Beginners)
What it is: A free, browser-based notebook environment hosted by Google. No installation required.
Setup: 1. Go to colab.research.google.com 2. Sign in with a Google account 3. Click "New Notebook" 4. You can now write and run Python code in your browser
Pros: Zero setup, free, runs in any browser, includes most libraries pre-installed. Cons: Requires an internet connection, files are stored in Google Drive, sessions time out after inactivity.
Installing packages in Colab: If a code example requires a package not pre-installed in Colab, add a cell at the top of your notebook with:
!pip install pandas scikit-learn matplotlib
Option 2: Jupyter Notebooks (Local)
What it is: A local notebook environment that runs in your browser but uses your own computer's Python installation.
Setup:
1. Install Python (see Option 3 below for installation instructions)
2. Open a terminal or command prompt
3. Install Jupyter: pip install jupyter
4. Launch it: jupyter notebook
5. Your browser will open with a file navigator — click "New > Python 3" to create a notebook
Pros: Works offline, full control over your environment, no cloud dependency. Cons: Requires local Python installation, some troubleshooting may be needed.
Option 3: Local Python Installation
What it is: Python installed directly on your computer, running scripts from the command line or an editor.
Setup on Windows:
1. Go to python.org/downloads
2. Download the latest Python 3 release (3.10 or later recommended)
3. Important: During installation, check the box that says "Add Python to PATH"
4. Open Command Prompt and type python --version to verify
Setup on macOS:
1. Open Terminal
2. Check if Python 3 is installed: python3 --version
3. If not installed, download from python.org/downloads or install via Homebrew: brew install python
Setup on Linux:
1. Python 3 is typically pre-installed. Check with: python3 --version
2. If needed: sudo apt install python3 python3-pip (Debian/Ubuntu)
E.3 Installing Required Packages
The code examples in this book use a small set of common Python libraries. A requirements.txt file is provided in the textbook's repository.
The Packages
| Package | Version | Used For |
|---|---|---|
pandas |
>= 2.0 | Data loading, exploration, and manipulation |
scikit-learn |
>= 1.3 | Machine learning demos (classification, clustering) |
matplotlib |
>= 3.7 | Charts and visualizations |
requests |
>= 2.31 | API calls (optional LLM examples) |
Installation
From your terminal or command prompt:
pip install pandas scikit-learn matplotlib requests
Or, if you have the textbook's requirements.txt file:
pip install -r requirements.txt
If you see permission errors, try:
pip install --user pandas scikit-learn matplotlib requests
E.4 Testing Your Setup
Create a new Python file or notebook cell and run the following:
# Test that all required packages are installed
import pandas as pd
import sklearn
import matplotlib
import requests
print(f"pandas version: {pd.__version__}")
print(f"scikit-learn version: {sklearn.__version__}")
print(f"matplotlib version: {matplotlib.__version__}")
print(f"requests version: {requests.__version__}")
print("\nAll packages installed successfully!")
If all four version numbers print without errors, you are ready.
E.5 Troubleshooting Common Issues
"python" is not recognized / command not found
- Windows: Reinstall Python and make sure to check "Add Python to PATH"
- macOS/Linux: Try
python3instead ofpython, andpip3instead ofpip
pip install fails with permission errors
- Try
pip install --user <package>to install for your user only - On macOS/Linux, do not use
sudo pip install— it can break system Python
Import errors after installation
- Make sure you installed packages for the same Python version you are running. If you have both Python 2 and Python 3, use
pip3 installexplicitly. - In Jupyter, make sure the notebook kernel matches the Python environment where you installed packages.
Matplotlib charts do not display
- In Jupyter notebooks, add
%matplotlib inlineat the top of your notebook - In Google Colab, charts should display automatically
- In scripts, add
plt.show()after creating a figure
Version conflicts
- If you encounter version conflicts, consider creating a virtual environment:
bash python -m venv ai-literacy-env # On Windows: ai-literacy-env\Scripts\activate # On macOS/Linux: source ai-literacy-env/bin/activate pip install -r requirements.txt
E.6 Chapter Code Reference
The following chapters include optional Python code examples:
| Chapter | Topic | What the Code Does |
|---|---|---|
| Ch. 3 | How Machines Learn | Simple scikit-learn classification demo on the Iris dataset |
| Ch. 4 | Data | Pandas data exploration — loading and examining a dataset |
| Ch. 5 | Large Language Models | Simple API call to a language model |
| Ch. 6 | Computer Vision | Image classification with a pre-trained model |
| Ch. 14 | Using AI Effectively | Prompt template with API call |
| Ch. 18 | AI and the Environment | Simple carbon footprint calculator |
Each code example is self-contained and can be run independently. You do not need to run earlier examples to understand later ones.
E.7 A Note on AI Coding Assistants
You may be tempted to use an AI coding assistant (like GitHub Copilot, ChatGPT, or Claude) to help you with the Python examples. That is perfectly fine for this textbook — the code is a means to an end, not the primary learning objective. However, if you do use an AI assistant, document what you asked it for and evaluate its output. That is, in itself, an exercise in AI literacy (see Chapter 14).