Python Environment Setup for AI Engineering
A professional AI automation environment is not just "install Python and pip install." You need reproducible environments, type checking, async support, and structured project layouts that scale.
Step 1: Python Version Management with pyenv
# Install pyenv (macOS)
brew install pyenv
# Install Python 3.12
pyenv install 3.12.3
pyenv global 3.12.3
# Verify
python --version
# Python 3.12.3
Step 2: Project Structure
Every project in this course follows this structure:
ai-automation-project/
├── src/
│ ├── __init__.py
│ ├── config.py # Environment and settings
│ ├── client.py # API client setup
│ ├── pipelines/ # Automation pipelines
│ │ ├── __init__.py
│ │ └── document.py
│ ├── models/ # Data models (Pydantic)
│ │ ├── __init__.py
│ │ └── schemas.py
│ └── utils/ # Shared utilities
│ ├── __init__.py
│ ├── retry.py
│ └── logging.py
├── tests/
│ └── test_pipelines.py
├── .env # API keys (never commit)
├── .env.example # Template for env vars
├── pyproject.toml # Project metadata and deps
├── Dockerfile
└── README.md
Step 3: Dependency Management with uv
We use uv — the fastest Python package manager (written in Rust):
# Install uv
curl -LsSf https://astral.sh/uv/install.sh | sh
# Create project
mkdir ai-automation && cd ai-automation
uv init
# Add core dependencies
uv add anthropic pydantic python-dotenv httpx tenacity structlog
# Add development dependencies
uv add --dev pytest pytest-asyncio ruff mypy
Step 4: Configuration with Pydantic Settings
# src/config.py
from pydantic_settings import BaseSettings
class Settings(BaseSettings):
anthropic_api_key: str
default_model: str = "claude-sonnet-4-5"
max_retries: int = 3
log_level: str = "INFO"
class Config:
env_file = ".env"
env_file_encoding = "utf-8"
settings = Settings()
Step 5: The .env File
# .env
ANTHROPIC_API_KEY=sk-ant-your-key-here
DEFAULT_MODEL=claude-sonnet-4-5
LOG_LEVEL=INFO
Step 6: Structured Logging
# src/utils/logging.py
import structlog
def setup_logging(level: str = "INFO"):
structlog.configure(
processors=[
structlog.processors.TimeStamper(fmt="iso"),
structlog.processors.add_log_level,
structlog.processors.StackInfoRenderer(),
structlog.dev.ConsoleRenderer(),
],
wrapper_class=structlog.make_filtering_bound_logger(level),
)
return structlog.get_logger()
log = setup_logging()
Step 7: Verify Everything Works
# test_setup.py
import anthropic
from src.config import settings
client = anthropic.Anthropic(api_key=settings.anthropic_api_key)
response = client.messages.create(
model=settings.default_model,
max_tokens=100,
messages=[{"role": "user", "content": "Say 'Setup complete!' if you can read this."}],
)
print(response.content[0].text)
# Setup complete!
Run it:
uv run python test_setup.py
If you see "Setup complete!" — your environment is ready. Every project in this course builds on this foundation.