Production-Grade Python

Write Python that runs unattended: virtual environments, dependency pinning, project layout, configuration, logging, type hints and tests.

Intermediate · about 8 hours · difficulty 3/5

Learn first: Ingesting Data from APIs, Git and Collaborative Workflows

A notebook that works once is not a pipeline. Production code runs at 3 a.m. on a machine you cannot see, so it has to be reproducible, observable and tested.

Environments and dependencies

  • Use a virtual environment per project (venv, or tools like uv or Poetry).
  • Pin dependencies in a lock file so the server installs exactly what you tested.
  • Structure code as a package (src/your_pipeline/) rather than a folder of loose scripts.

Configuration and secrets

Read settings from environment variables or config files, never hard-coded. Different values for dev, staging and prod; same code.

Logging

Use the logging module, not print. Log the start and end of each step, row counts, and the parameters (date, source) the job ran with. Structured (JSON) logs are easier to search in the cloud.

Type hints and linting

Type hints plus a checker (mypy or pyright) catch whole classes of bugs. A linter and formatter (ruff, black) keeps code consistent across the team.

Testing

  • Unit tests with pytest for transformation functions: given these input rows, expect these output rows.
  • Fixtures with tiny sample data, including nulls, duplicates and weird encodings.
  • Tests run in CI on every pull request.

Pitfalls

  • "Works on my machine" because a library version differs on the server.
  • Logging secrets or full customer records; mask PII in logs.
  • Only testing the happy path. Bugs live in empty files, nulls and duplicates.

You are ready to move on when your API extractor is a pinned, installable package with logging, config from environment variables, and a pytest suite that runs in GitHub Actions.

Key concepts

  • Virtual environments
  • Dependency pinning and lock files
  • Package layout
  • Config via environment variables
  • logging module
  • Type hints and mypy
  • pytest and fixtures
  • ruff or black

Mini project

Refactor your API extractor into a package with a lock file, environment-based config, structured logging and at least five pytest tests (including empty response and duplicate records). Run the tests in a GitHub Actions workflow.

Check yourself

Your job passes locally but fails on the server with an ImportError. What are the likely causes and how does a lock file help?

Free resources