Article 2: Development Environment Setup

Introduction

A well-configured development environment is like a craftsman's workshopβ€”everything has its place, tools are sharp, and work flows smoothly. Through setting up environments for countless Python projects, I've learned that investing time upfront in proper configuration pays dividends throughout the project lifecycle.

This article covers setting up a professional Python development environment, from virtual environments to IDE configuration, establishing the foundation for everything we build in this series.

Development Environment Overview

spinner

Python Installation

Using pyenv for Version Management

I recommend pyenv for managing Python versionsβ€”it allows multiple versions to coexist and switch between them easily.

# Install pyenv (macOS)
brew install pyenv

# Install pyenv (Linux)
curl https://pyenv.run | bash

# Add to shell configuration (~/.zshrc or ~/.bashrc)
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"

# Restart shell
exec "$SHELL"

Installing Python Versions

Python Version Selection

Version
Status
Recommendation

3.9

Maintenance

Legacy projects only

3.10

Active

Good for compatibility

3.11

Active

Recommended for new projects

3.12

Active

Latest features, good for greenfield

Virtual Environments

Why Virtual Environments?

spinner

Virtual environments:

  • Isolate dependencies - Each project has its own packages

  • Ensure reproducibility - Same versions across environments

  • Prevent conflicts - Different projects can use different versions

  • Keep system Python clean - No global package pollution

Creating Virtual Environments

Using venv (Built-in)

Using uv (Modern, Fast)

uv is a modern Python package installer that's significantly faster than pip:

Virtual Environment Best Practices

Always add virtual environment to .gitignore:

Dependency Management

requirements.txt (Traditional)

Example requirements.txt:

pyproject.toml (Modern)

The modern approach uses pyproject.toml for all project configuration:

Installing with pyproject.toml:

Dependency Version Pinning

Specifier
Meaning
Use Case

>=1.0.0

Minimum version

Flexibility

==1.0.0

Exact version

Production stability

~=1.0.0

Compatible release

Balance

>=1.0.0,<2.0.0

Range

Major version lock

Project Structure

Standard Python Project Layout

The src Layout

I recommend the src/ layout for several reasons:

Without src/:

With src/:

Package Initialization

IDE Configuration (VS Code)

Essential Extensions

Workspace Settings

Launch Configuration

Environment Variables

Using .env Files

Loading Environment Variables

Environment Template

Create a template for other developers:

Add .env to .gitignore:

Pre-commit Hooks

Setting Up Pre-commit

Pre-commit hooks run checks before each commit:

Configuration

Pre-commit Workflow

spinner

Development Workflow

Complete Setup Script

Makefile for Common Tasks

Practical Exercise

Exercise 1: Complete Project Setup

Exercise 2: Configure Development Tools

Exercise 3: First Test

Environment Checklist

Use this checklist for new projects:

Key Takeaways

  1. Use virtual environments - Always isolate project dependencies

  2. Prefer pyproject.toml - Modern, centralized configuration

  3. Configure your IDE - Format on save, linting, testing integration

  4. Automate with pre-commit - Catch issues before they're committed

  5. Document the setup - Make it easy for others (and future you)

What's Next?

With your development environment ready, it's time to add quality tools. In Article 3: Code Quality Tools, we'll configure linting with Ruff, formatting with Black, and type checking with mypy to catch issues early and maintain consistent code.


This article is part of the Software Engineering 101 series.

Last updated