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 --versionRequired: Python 3.8 or later
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-pipStep 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\activateYou’ll see (workshop-env) in your terminal prompt when activated.
Step 3: Install Required Libraries
With your virtual environment activated, install the necessary packages:
pip install ollama chromadb jinja2 jupyterLibrary Purposes:
ollama: Official Ollama Python clientchromadb: Vector database for embeddingsjinja2: Template engine for dynamic promptsjupyter: 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.1Step 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.pyExpected Output:
✓ Connected to Ollama successfully!
Available models:
- llama3.2:latestConnection Failed?
If you see an error:
- Make sure Ollama application is running
- Verify port 11434 is accessible:
curl http://localhost:11434/api/tags - Check firewall settings
- 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 notebookThis opens a browser window with Jupyter interface.
Alternative: JupyterLab
For a more modern interface:
pip install jupyterlab
jupyter labStep 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 notebookNo Git?
Download as ZIP:
- Visit github.com/nishad/llm-workshop-notebooks
- Click “Code” → “Download ZIP”
- Extract and navigate to folder
Complete Setup Checklist
Verify you have everything:
- Python 3.8+ installed
- Virtual environment created and activated
-
ollamalibrary installed (0.6.1+) -
chromadbinstalled -
jinja2installed -
jupyterinstalled (optional but recommended) - Connection test passed
- Workshop notebooks cloned (optional)
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 resultsQuick Reference
Activate environment:
source workshop-env/bin/activate # macOS/Linux
workshop-env\Scripts\activate # WindowsDeactivate environment:
deactivateInstall new package:
pip install package-nameList installed packages:
pip listStart Jupyter:
jupyter notebookNext Steps
Environment set up? Let’s start coding!