Environment Setup — From Zero to Your First Automation Script
Chapter 1: Environment Setup — From Zero to Your First Automation Script
A well-configured development environment prevents 90% of the problems beginners encounter. This chapter walks from Python version selection through virtual environments, editor configuration, and package management — ending with a real automation script to validate everything. Complete this chapter, and every code example in the rest of the book will run without friction.
Python Version Selection
Recommended: Python 3.11 — it delivers ~25% average performance improvement over 3.10 (per official benchmarks) with mature third-party library support. Python 3.12 works too, but some libraries haven't caught up yet.
Why not 3.9 or below?
- Python 3.10 introduced structural pattern matching (
match-case) and cleaner type syntax (int | Noneinstead ofOptional[int]) - Python 3.8 reached end-of-life in October 2024 — no more security patches
- Modern automation libraries like Playwright require Python 3.9+ minimum
Check your current version: Run
python3 --version(macOS/Linux) orpython --version(Windows). If it shows 3.11+, skip installation.
Installation Methods Compared
| Method | Best For | Pros | Notes |
|---|---|---|---|
| Official installer python.org | Windows, beginners | Simplest, wizard-based | Check "Add Python to PATH" during install |
| Homebrew (macOS) | macOS developers | Easy version management, brew upgrade python |
Install Homebrew first from brew.sh |
| winget (Windows) | Windows 10/11 developers | Command-line install, scriptable | Requires Windows 10 2004+ |
| conda/miniconda | Data science, multi-version needs | Complete environment isolation, pre-bundled scientific packages | Heavier than venv; don't use unless needed |
Virtual Environments: Why venv Is Non-Negotiable
Virtual environments solve a classic problem: Project A needs requests==2.28, Project B needs requests==2.31. Installing globally means they fight over the same space. venv gives each project its own isolated Python installation.
macOS / Linux
# Create virtual environment in project directory
python3 -m venv .venv
# Activate it
source .venv/bin/activate
# Prompt changes to (.venv) — packages install here, not globally
# Deactivate when done
deactivate
Windows
# Create
python -m venv .venv
# Activate (CMD)
.venv\Scripts\activate.bat
# Activate (PowerShell)
.venv\Scripts\Activate.ps1
# If PowerShell blocks execution, run as admin:
# Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
VS Code Configuration
Essential extensions:
- Python (Microsoft): syntax highlighting, IntelliSense, debugger
- Pylance: advanced type checking and autocomplete
- Ruff: ultra-fast linter + formatter (replaces Black + flake8)
Select your virtual environment interpreter: Press Ctrl+Shift+P (Mac: Cmd+Shift+P), type Python: Select Interpreter, choose .venv/bin/python.
Debug Configuration
Create .vscode/launch.json with the same JSON shown in the Chinese section above (the structure is identical). Press F5 to debug the current file with full breakpoint and step-through support.
Package Management: pip and requirements.txt
Shell
# Install a package
pip install requests
# Install specific version
pip install requests==2.31.0
# Install multiple packages
pip install requests openpyxl pandas
# List installed packages
pip list
# Export current environment to requirements.txt
pip freeze > requirements.txt
# Recreate environment from requirements.txt
pip install -r requirements.txt
# Upgrade pip itself
pip install --upgrade pip
First Automation Script: Bulk Project Directory Creator
Every time you start a new project you manually create the same folder structure. This script creates it in one command — the Chinese section above contains the full annotated code. Here is the usage and key takeaways:
Shell
# Create a project named "my-automation"
python create_project.py my-automation
# Create inside a specific directory
python create_project.py my-automation --dir ~/projects
The script uses three modules you will encounter repeatedly throughout this book:
- pathlib.Path — cross-platform path manipulation;
mkdir(parents=True)creates intermediate directories automatically - argparse — standard library CLI argument parser; provides
--help, validation, and default values - Type hints —
project_name: str,base_dir: Path,-> Paththroughout; IDE autocomplete and mypy both use these
Checkpoint: If the script runs without errors and produces the directory structure, your environment is correctly configured. Every code example in the rest of the book should work in this same setup.
Previous Chapter
Next Chapter
Chapter 2: Core Python for Automation