Study notes · 10% of the exam

Standard Library, Packaging and Tooling

Know how imports really work, how to isolate and pin dependencies, and the standard-library traps (datetimes, decimals, JSON, regex, subprocess, encodings) that interviewers love.

Key points

  1. 1

    A module runs once per process and is cached in sys.modules. A script runs as __main__, so importing it by name executes it a second time.

  2. 2

    Use python -m pkg.module for code that uses relative imports: it imports the package first and puts the working directory on sys.path[0].

  3. 3

    Isolate projects in a virtual environment, use pipx for CLI tools, and pin transitive dependencies with a lock file (uv.lock, poetry.lock or pip-tools output).

  4. 4

    Keep datetimes timezone-aware (datetime.now(UTC), zoneinfo). Same-tzinfo arithmetic is wall-clock, so convert to UTC to measure real elapsed time.

  5. 5

    Build Decimal values from strings, and choose the rounding mode explicitly; the default is half-to-even, like round().

  6. 6

    Run subprocesses with a list of arguments, check=True and communicate()/run(), never shell=True with user input.

  7. 7

    Always pass encoding="utf-8" to open(); the default is the locale encoding until UTF-8 mode becomes the default in 3.15 (PEP 686).

Common traps

  • from config import X copies the value, so importlib.reload(config) does not update X.

  • $ in a regex matches before a trailing newline; use re.fullmatch or \Z for validation.

  • Path("/srv") / user_value drops the base when user_value is absolute.

Test yourself on Standard Library, Packaging and Tooling

Ten questions, with the answer and explanation after each one.