1  Environment Setup

In this chapter, we’ll set up your Python development environment using modern tools. Setting up a proper development environment is crucial for Python programming because it ensures you have the right tools to write, test, and manage your code effectively. Think of it like setting up a workshop before starting a woodworking project - having the right tools makes everything much easier.

1.1 Python Installation

We recommend Python 3.12 or higher for the best type hinting support. Python 3.12 includes significant performance improvements and enhanced type hints that make your code more reliable and easier to debug. It’s like upgrading from a basic calculator to a scientific calculator - you get more features and better accuracy.

1.1.1 Windows

# Using winget (Windows Package Manager - like apt for Windows)
winget install Python.Python.3.12

# Using Microsoft Store (easiest for beginners)
# Search for "Python 3.12" in Microsoft Store

What these commands do: winget is Windows’ modern package manager that automatically downloads and installs Python with proper PATH configuration. The Microsoft Store version is sandboxed and automatically updates, making it perfect for beginners who want a hassle-free installation.

1.1.2 macOS

# Using Homebrew (package manager for macOS)
brew install python@3.12

# Using pyenv (Python version manager)
pyenv install 3.12.8
pyenv global 3.12.8

What these commands do: Homebrew installs Python to /opt/homebrew/bin and manages dependencies automatically. pyenv is more advanced - it lets you install multiple Python versions and switch between them easily. Think of pyenv like having multiple toolboxes, where you can pick the right tools (Python version) for each project.

1.1.3 Linux

# Ubuntu/Debian (uses APT package manager)
sudo apt update                      # Update package list
sudo apt install python3.12 python3.12-venv  # Install Python + virtual env support

# Fedora (uses DNF package manager)
sudo dnf install python3.12

# Arch Linux (uses Pacman package manager)
sudo pacman -S python               # Always installs latest Python

What these commands do: Each Linux distribution uses a different package manager, but they all do the same thing - download, install, and configure Python. The sudo command gives administrator privileges needed to install system-wide packages. We also install python3.12-venv on Ubuntu/Debian to enable virtual environments (isolated Python environments for each project).

1.2 Environment Managers

Environment managers solve a crucial problem: different Python projects need different packages and versions. Without proper management, you might install packages globally and create conflicts between projects. Environment managers create isolated “bubbles” for each project with its own dependencies.

1.2.2 Poetry (Alternative)

Poetry provides dependency management and packaging with a focus on deterministic builds. It’s like a master chef who not only cooks but also writes down exact recipes so anyone can recreate the same dish perfectly.

1.2.2.1 Installation

# Download and install Poetry (official installer)
curl -sSL https://install.python-poetry.org | python3 -

What this does: Downloads Poetry’s official installer and runs it with Python. Poetry installs itself in an isolated environment to avoid conflicts with your projects.

1.2.2.2 Usage

# Create project (creates full package structure)
poetry new my-project
cd my-project

# Add dependencies (automatically creates virtual environment)
poetry add requests pandas

# Install dependencies (from pyproject.toml)
poetry install

# Run commands (in isolated environment)
poetry run python script.py

Key difference from uv: Poetry focuses more on packaging and publishing libraries, while uv is optimized for speed and simplicity. Choose Poetry if you plan to publish packages to PyPI.

1.2.3 Miniforge (For Data Science)

Miniforge provides conda with conda-forge as default channel. It’s specifically designed for data science workflows where you need complex dependencies like NumPy, SciPy, and machine learning libraries that have C/C++ components.

1.2.3.1 Installation

# Download from https://github.com/conda-forge/miniforge
# Run installer script (Linux example)
bash Miniforge3-Linux-x86_64.sh

Why Miniforge: Unlike pip, conda can install non-Python dependencies (like CUDA for GPU computing). It’s essential for data science because many libraries depend on optimized C/Fortran code that pip can’t handle.

1.2.3.2 Usage

# Create environment (isolated conda environment)
conda create -n myenv python=3.12
conda activate myenv                    # Activate environment

# Install packages (with optimized binaries)
conda install pandas numpy matplotlib

Environment workflow: Unlike pip environments, conda environments are completely isolated, including the Python interpreter itself. This prevents many compatibility issues with data science libraries.

1.3 Exercises

  1. Install Python 3.12 on your system - Verify with python --version
  2. Install uv and create a test project - Practice the complete workflow
  3. Create virtual environment and add pandas - Learn dependency isolation
  4. Run simple Python script using uv run - Experience the development workflow

Learning goals: By completing these exercises, you’ll understand the foundation of modern Python development: isolated environments, dependency management, and reproducible project setup.

1.4 Next Steps

Now that your environment is ready, let’s learn about Git and GitHub for version control.