Introduction

Setup Instructions

Get your DSPy environment ready in 15-30 minutes with step-by-step setup instructions.

Section 4 of 4 ~15 min read

๐Ÿš€ Setup Instructions

Get your DSPy environment ready in 15-30 minutes

Your Setup Roadmap

1

Python Check

Verify Python 3.9+

1-2 min
2

Project Setup

Create directory

1 min
3

Virtual Env

Isolate dependencies

2-3 min
4

Installation

Install DSPy

2-5 min
5

API Config

Set up keys

3-5 min
6

Testing

Verify setup

2-3 min
โœ“

Ready!

Start learning

Step 1: Verify Python Installation

Open your terminal and run:
python3 --version

Expected outputs (any of these is good):

Python 3.9.0 โœ… Minimum required Python 3.10.8 โœ… Good choice Python 3.11.5 โœ… Latest stable Python 3.12.0 โœ… Cutting edge
๐Ÿ’ก

Pro Tip: If python3 doesn't work, try python. Different systems use different commands.

Need to Install/Upgrade Python?

๐ŸชŸ Windows Python Installer
๐ŸŽ macOS brew install python
๐Ÿง Linux sudo apt install python3.11

Step 2: Create Your Project Directory

๐ŸŽ๐Ÿง macOS / Linux
๐ŸชŸ Windows CMD
๐ŸชŸ PowerShell
# Create the directory
mkdir ~/dspy-learning

# Navigate into it
cd ~/dspy-learning

# Verify location
pwd
๐ŸŽฏ

Success: You should see your DSPy project directory path!

Step 3: Set Up Virtual Environment

Why Virtual Environments?

๐Ÿ“ฆ Isolate Dependencies
๐Ÿ›ก๏ธ Avoid Conflicts
๐Ÿงน Clean Management
Create the environment:
python3 -m venv venv

Activate the Environment

macOS / Linux source venv/bin/activate
Windows (CMD) venv\Scripts\activate
Windows (PowerShell) venv\Scripts\Activate.ps1

Verify activation: Your prompt should now show (venv) at the beginning!

๐Ÿšจ

Important: Always activate your virtual environment before working on DSPy projects!

Step 4: Install DSPy and Dependencies

1. Start with fresh pip:
pip install --upgrade pip
2. Install DSPy core:
pip install dspy-ai

What you get:

  • โœ… DSPy framework
  • โœ… Core dependencies
  • โœ… Language model adapters
3. Verify installation:
python3 -c "import dspy; print(f'DSPy version: {dspy.__version__}')"
DSPy version: 2.5.x
4. Install additional dependencies:
pip install openai anthropic python-dotenv
openai OpenAI API client
anthropic Claude API client
python-dotenv Environment variable management

Step 5: Configure API Access

Recommended ๐Ÿ’ณ OpenAI
๐Ÿค– Claude
Free ๐Ÿ  Local

Getting Your OpenAI API Key

  1. Visit: platform.openai.com
  2. Sign up or log in
  3. Navigate to API Keys section
  4. Create new secret key
  5. Copy your key (starts with sk-)
Add to your .env file:
OPENAI_API_KEY=sk-your-actual-api-key-here
Create a .env file:
touch .env
๐Ÿšจ

Security Warning: Never commit API keys to Git or share them publicly!

Step 6: Test Your Setup

๐Ÿงช

The Moment of Truth!

Create test_setup.py:
"""
โœจ DSPy Setup Verification Script โœจ
Tests your installation and API connectivity
"""

import os
from dotenv import load_dotenv
import dspy

# Load environment variables
load_dotenv()

def test_setup():
    print("๐Ÿš€ DSPy Setup Verification")
    print("=" * 50)
    
    # Check DSPy
    print(f"โœ… DSPy version: {dspy.__version__}")
    
    # Check API key
    api_key = os.getenv("OPENAI_API_KEY")
    if api_key:
        print("โœ… OpenAI API key found")
        
        # Configure and test
        lm = dspy.LM(
            model="openai/gpt-4o-mini",
            api_key=api_key
        )
        dspy.configure(lm=lm)
        
        # Simple test
        class SimpleQA(dspy.Signature):
            question: str = dspy.InputField()
            answer: str = dspy.OutputField()
        
        predictor = dspy.Predict(SimpleQA)
        result = predictor(question="What is 2 + 2?")
        
        print(f"โœ… API Test: 2 + 2 = {result.answer}")
        print("=" * 50)
        print("๐ŸŽ‰ SUCCESS! Your DSPy environment is ready!")
    else:
        print("โš ๏ธ No API key found. Add to .env file.")

if __name__ == "__main__":
    test_setup()
Run the test:
python3 test_setup.py

Expected Successful Output:

๐Ÿš€ DSPy Setup Verification
==================================================
โœ… DSPy version: 2.5.x
โœ… OpenAI API key found
โœ… API Test: 2 + 2 = 4
==================================================
๐ŸŽ‰ SUCCESS! Your DSPy environment is ready!

Troubleshooting Common Issues

"No module named 'dspy'" Activate venv: source venv/bin/activate
"API key not found" Create .env file with your key
"Invalid API key" Verify key in provider dashboard

Step 7: Get Book Examples (Optional)

Clone the book repository:
# Go to your learning directory
cd ~/dspy-learning

# Clone the book repository
git clone https://github.com/dustinober1/Ebook_DSPy.git

# Enter the directory
cd Ebook_DSPy

# Install all dependencies
pip install -r requirements.txt

Repository Structure

Ebook_DSPy/
โ”œโ”€โ”€ examples/          ๐Ÿ“ All code examples by chapter
โ”‚   โ”œโ”€โ”€ chapter01/
โ”‚   โ”œโ”€โ”€ chapter02/
โ”‚   โ””โ”€โ”€ ...
โ”œโ”€โ”€ exercises/         โœ๏ธ Exercise starter code & solutions
โ”œโ”€โ”€ assets/           ๐Ÿ“Š Datasets and resources
โ””โ”€โ”€ scripts/          ๐Ÿ› ๏ธ Utility scripts

๐Ÿ“‹ Quick Reference Card

Activate venv source venv/bin/activate
Deactivate venv deactivate
Install package pip install package-name
Run Python script python3 script.py
Check DSPy version python3 -c "import dspy; print(dspy.__version__)"
๐ŸŽ‰

Congratulations!

Your DSPy environment is ready. You can now start Chapter 1!