Sefism is free for learners. We are here to help you grow, one step at a time.Join for free
Sefism.

Best practices · Stacks and tools

Python Best Practices

Opens with credits

A practical guide to Python best practices for people who write applications, services and scripts that other people depend on. It covers supported versions and project setup with pyproject.toml and a lockfile, style with PEP 8 and Ruff, type hints and data modelling, error handling, logging, concurrency and performance, testing with pytest, and the security mistakes Python makes easy.

Written by Tauseef Fayyaz8 chapters, 37 practicesAbout 22 min29 quiz questionsReviewed 13 October 2026

What you will learn

8 chapters · about 22 min

  1. 1Versions, environments and project setupThe 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.5 practices · 4 min
  2. 2Style, formatting and lintingHow to make Python code look the same across a team without arguing about it, and the idioms that make it easier to read.5 practices · 3 min
  3. Type hints and data modellingHow to use type hints and a type checker to catch bugs before runtime, and how to model data so it is not a dictionary of unknown shape.4 practices · 2 minOpens with credits
  4. Errors and resource handlingHow to raise, catch and report exceptions so failures are visible and specific, and how to make sure files, connections and locks are always released.5 practices · 3 minOpens with credits
  5. Logging and configurationHow to log so problems can be diagnosed in production, and how to load settings so the same code runs in every environment.4 practices · 2 minOpens with credits
  6. Concurrency and performanceChoosing between asyncio, threads and processes, keeping async code from blocking, and measuring before you optimise.5 practices · 3 minOpens with credits
  7. Testing with pytestHow to set up pytest so tests are fast, isolated and run against the code users install.4 practices · 2 minOpens with credits
  8. Security pitfalls in PythonThe standard library and common packages make a few dangerous things very easy. These practices close the most common holes.5 practices · 3 minOpens with credits

About this guide

Python best practices are mostly about the code you will read again in six months. Python lets you write something that works in ten minutes, and the same flexibility lets a project drift into unpinned dependencies, untyped dictionaries passed everywhere and bare except: blocks that hide real failures. None of that shows up in a demo. It shows up when someone else has to change the code.

This guide starts with the setup that every project needs: a supported Python version, an isolated environment, pyproject.toml and a committed lockfile. It then covers style and linting with PEP 8 and Ruff, type hints and how to model data, handling errors and resources, logging and configuration, concurrency and performance, testing with pytest, and the handful of standard library calls that turn into security holes when fed untrusted input.

Everything here was checked against the Python documentation for 3.14 and 3.15. Python 3.15.0 shipped on October 9, 2026, and brings lazy imports, UTF-8 as the default encoding and a new sampling profiler; 3.14 added deferred annotations, template strings and official support for the free-threaded build. Where a practice depends on a version, it says so.

Common questions

What is the latest version of Python?

Python 3.15.0 was released on October 9, 2026. Python 3.14 is also in bug fix support, with 3.14.8 as its latest release. Versions 3.11 to 3.13 receive security fixes only. Check the status of Python versions page in the Python developer guide for the current table.

Should I use uv, pip or Poetry for Python projects?

Any of them works if the project has a pyproject.toml, a committed lockfile and an isolated environment. uv is fast and combines environments, installs and a universal lockfile in one tool. pip with a pinned requirements file is the most universal. Pick one per project and use it in CI too.

Are Python type hints checked at runtime?

No. The interpreter ignores annotations apart from storing them. A type checker such as mypy or pyright reads them before the code runs. Validate untrusted input at runtime with a library such as Pydantic or with explicit checks.

What is the difference between Ruff and Black?

Black is a code formatter. Ruff is a linter that also includes a formatter with Black-compatible output, and it replaces tools such as Flake8 and isort. Many projects now use Ruff alone for both jobs.

Does Python still have the GIL?

The standard build does. Since Python 3.14 the free-threaded build, which removes the GIL, is officially supported, but it is a separate build and some C extensions do not support it yet. Test your dependencies before switching.

When should I use asyncio instead of threads in Python?

Use asyncio when you make many network calls and your framework and libraries are async, such as an async web API. Use threads when you call blocking libraries a few dozen at a time. Neither speeds up CPU-heavy pure-Python code on the standard build; use processes for that.

Sources (8)

Checked against these, last on 13 October 2026.

  1. Status of Python versions, Python Developer Guidedevguide.python.org/versions/
  2. What's new in Python 3.15, Python docsdocs.python.org/3.15/whatsnew/3.15.html
  3. PEP 8: Style Guide for Python Code, Python PEPspeps.python.org/pep-0008/
  4. Writing your pyproject.toml, Python Packaging User Guidepackaging.python.org/en/latest/guides/writing-pyproject-toml/
  5. Project structure and files, uv docsdocs.astral.sh/uv/concepts/projects/layout/
  6. Ruff, Astraldocs.astral.sh/ruff/
  7. Good Integration Practices, pytest docsdocs.pytest.org/en/stable/explanation/goodpractices.html
  8. Logging HOWTO, Python docsdocs.python.org/3/howto/logging.html

Picked from the other libraries by shared topics.

Work with me

Stuck on something specific?

Writing only gets you so far. If you want an answer to your situation rather than the general case, book a session and we will work through it together. Sessions are free for approved Sefism members, and a few slots open each week.

  • 1:1 Mentorship
  • Fresh Graduate Mentorship
  • Career Guidance

Brand deals, promos and partnerships: tauseeffayyaz@gmail.com