Skip to content
5 min read·Lesson 1 of 10

Setting Up Python and Your First Scripts

Install a modern Python, pick a sensible editor, and write your first scripts the way DevOps engineers actually do.

Python is the duct tape of cloud and DevOps work — small enough to write a quick script, capable enough to build a real tool. This first lesson sets you up with the version, editor, and patterns you'll use throughout the rest of the course.

Which Python?

Use Python 3.11 or newer. Older 3.x versions still work, but newer ones bring meaningful speedups, better error messages, and standard-library improvements (like tomllib). Don't rely on whatever Python ships with your OS — distros use it for system tooling and you'll break things by upgrading it.

Recommended installers

  • pyenv (Linux/macOS) — installs and switches Python versions per project
  • pyenv-win or the official installer (Windows)
  • uv — the new Rust-based tool from Astral; can install Python and resolve dependencies at speed
  • Homebrew on macOS for a quick "just give me Python" install
# Install pyenv on macOS
brew install pyenv
pyenv install 3.12.5
pyenv global 3.12.5
python --version
# Python 3.12.5

Editor

Pick one and stick with it long enough to learn its shortcuts. Solid choices:

  • VS Code with the Microsoft Python and Pylance extensions — free, lightweight, ubiquitous
  • PyCharm Community — heavier, batteries included
  • Neovim/Vim with pyright LSP — for terminal diehards

What you actually want from an editor: jump-to-definition, autocomplete, in-line type errors, debugging, and an integrated terminal. All three options provide all of that.

Your First Script

#!/usr/bin/env python3
"""Greet someone passed on the command line."""

import sys


def main() -> int:
    name = sys.argv[1] if len(sys.argv) > 1 else "world"
    print(f"hello, {name}")
    return 0


if __name__ == "__main__":
    sys.exit(main())

Save as hello.py, then:

chmod +x hello.py
./hello.py
# hello, world
./hello.py alex
# hello, alex

Three things happening here that you'll see throughout the course:

  1. The shebang #!/usr/bin/env python3 tells the OS how to run the file.
  2. sys.argv is the list of command-line arguments. Element 0 is the script name; the rest are user input.
  3. The if __name__ == "__main__": guard runs main() when executed directly, but not when the file is imported. This is the conventional shape of any script that might also be reused as a module.

Running Python Code Three Ways

ModeWhen to use
python script.pyNormal script execution
python (REPL)Interactive exploration; quick experiments
python -c "code"One-liners in shell pipelines

Better yet, install ipython (pip install ipython) for a vastly better REPL with tab completion, syntax highlighting, and easy editing of multi-line code.

Formatting and Linting

Don't argue about style — let tools handle it. The current consensus stack:

  • ruff — extremely fast linter and formatter (replacing flake8, isort, and now black)
  • mypy or pyright — static type checking
pip install ruff
ruff format hello.py
ruff check hello.py

Add these as pre-commit hooks once you start collaborating; you'll never have to manually fix indentation again.

Reading Errors

Python tracebacks are read bottom-up: the last line is the actual exception, and the lines above show how you got there. Modern Python (3.11+) underlines the exact expression that failed, which makes debugging dramatically friendlier:

Traceback (most recent call last):
  File "/tmp/x.py", line 5, in <module>
    print(items[10])
          ~~~~~^^^^
IndexError: list index out of range

Don't panic at red text — read it. The traceback nearly always points at the precise problem.

Key Takeaways

  • Use a recent Python (3.11+) — not the system Python that ships with the OS.
  • pyenv (or uv) lets you install and switch between Python versions cleanly.
  • A shebang plus chmod +x makes a script directly executable on Linux/macOS.
  • VS Code with the Python extension gives you debugging, formatting, and type checks for free.
  • print, sys.argv, and a dunder main are the building blocks of any script.

Test your knowledge

Try exam-style practice questions to reinforce what you've learned.

Practice Questions →