Python Setup

Setting Up Python Environment for Ollama

This page guides you through setting up Python and the necessary libraries for working with Ollama programmatically.


Step 1: Verify Python Installation

First, check if Python is installed and which version you have:

python --version
# or
python3 --version

Required: Python 3.8 or later

macOS/Linux Note: You might need to use python3 instead of python depending on your system configuration.

Install Python (if needed)

If Python isn’t installed or the version is too old:

macOS:

# Using Homebrew
brew install [email protected]

Windows:

  • Download from python.org
  • Important: Check “Add Python to PATH” during installation

Linux (Ubuntu/Debian):

sudo apt update
sudo apt install python3 python3-pip

Step 2: Create a Virtual Environment (Recommended)

Virtual environments keep project dependencies isolated:

# Create a virtual environment
python3 -m venv workshop-env

# Activate it
# macOS/Linux:
source workshop-env/bin/activate

# Windows:
workshop-env\Scripts\activate

You’ll see (workshop-env) in your terminal prompt when activated.

Always activate your virtual environment before installing packages or running scripts for this workshop.

Step 3: Install Required Libraries

With your virtual environment activated, install the necessary packages:

pip install ollama chromadb jinja2 jupyter

Library Purposes:

  • ollama: Official Ollama Python client
  • chromadb: Vector database for embeddings
  • jinja2: Template engine for dynamic prompts
  • jupyter: Interactive notebooks for experimentation

Verify Installation

pip list | grep -E 'ollama|chromadb|jinja2|jupyter'

Expected output (versions may vary):

chromadb        0.4.22
jinja2          3.1.2
jupyter         1.0.0
ollama          0.6.1

Step 4: Test Ollama Connection

Create a test script to verify everything works:

test_ollama.py:

import ollama

def test_connection():
    """Test connection to Ollama server."""
    try:
        # List available models
        models = ollama.list()
        print("✓ Connected to Ollama successfully!")
        print(f"\nAvailable models:")
        for model in models['models']:
            print(f"  - {model['name']}")
        return True
    except Exception as e:
        print(f"✗ Connection failed: {e}")
        print("\nTroubleshooting:")
        print("1. Make sure Ollama is running")
        print("2. Check if you can access http://localhost:11434")
        print("3. Try restarting Ollama")
        return False

if __name__ == "__main__":
    test_connection()

Run the test:

python test_ollama.py

Expected Output:

✓ Connected to Ollama successfully!

Available models:
  - llama3.2:latest

Connection Failed?

If you see an error:

  1. Make sure Ollama application is running
  2. Verify port 11434 is accessible: curl http://localhost:11434/api/tags
  3. Check firewall settings
  4. Restart Ollama application

See Troubleshooting for more help.


Step 5: Install Jupyter Notebook

If you want to follow along with the workshop notebooks:

# Install Jupyter (already done above)
pip install jupyter

# Start Jupyter
jupyter notebook

This opens a browser window with Jupyter interface.

Alternative: JupyterLab

For a more modern interface:

pip install jupyterlab
jupyter lab

Step 6: Clone Workshop Notebooks

Get the companion notebooks for hands-on practice:

# Clone the repository
git clone https://github.com/nishad/llm-workshop-notebooks.git

# Navigate to it
cd llm-workshop-notebooks

# Install requirements (if not already done)
pip install -r requirements.txt

# Start Jupyter
jupyter notebook

No Git?

Download as ZIP:

  1. Visit github.com/nishad/llm-workshop-notebooks
  2. Click “Code” → “Download ZIP”
  3. Extract and navigate to folder

Complete Setup Checklist

Verify you have everything:

  • Python 3.8+ installed
  • Virtual environment created and activated
  • ollama library installed (0.6.1+)
  • chromadb installed
  • jinja2 installed
  • jupyter installed (optional but recommended)
  • Connection test passed
  • Workshop notebooks cloned (optional)
Ready to Go! If all items are checked, you’re set up for Part 2 of the workshop.

Project Structure Suggestion

Organize your workshop files like this:

workshop/
├── workshop-env/          # Virtual environment
├── notebooks/             # Jupyter notebooks
├── scripts/               # Python scripts
├── data/                  # Sample data files
└── outputs/               # Generated results

Quick Reference

Activate environment:

source workshop-env/bin/activate  # macOS/Linux
workshop-env\Scripts\activate     # Windows

Deactivate environment:

deactivate

Install new package:

pip install package-name

List installed packages:

pip list

Start Jupyter:

jupyter notebook

Next Steps

Environment set up? Let’s start coding!