Python Best Practices

Chapter 1 of 8 · 5 practices · 4 min · 4-question quiz

Versions, environments and project setup

The decisions that every Python project makes on day one: which version to run, how to isolate dependencies and how to describe the project so anyone can rebuild it.

Start: Run a Python version that still gets bug fixes

5 practices in this chapter

  1. 1Run a Python version that still gets bug fixesEvery Python feature release gets about two years of bug fixes and then security fixes only, until it reaches end of life five years after release. Running an end-of-life version means security bugs…
  2. 2Give every project its own virtual environmentInstalling packages into the system Python mixes the dependencies of every project on the machine, and on many Linux distributions the system Python belongs to the operating system. A virtual…
  3. 3Describe the project in pyproject.tomlpyproject.toml is the standard place for a project's metadata, dependencies and tool settings. It replaces setup.py, setup.cfg and most tool-specific config files.
  4. 4Commit a lockfile so every install is the samepyproject.toml says which versions are allowed. A lockfile records which exact versions were resolved, including every transitive dependency. Without one, a new release of a package three levels…
  5. 5Use a src layout and keep tests outside the packageIn a flat layout, the package folder sits at the repository root, so Python can import it from the current directory even when it is not installed. Tests then pass locally against files that would…

Check yourself on this chapter

4 questions, right here once you are signed in. Your progress is saved as you read.

Sign up for the quiz