Best practices · Stacks and tools
Python Best Practices
Opens with creditsA 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.
What you will learn
8 chapters · about 22 min
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
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
- 1Follow PEP 8 and let a formatter apply it
- 2Lint in CI and fail the build on new warnings
- 3Compare to None with is, and check types with isinstance
- 4Use pathlib for paths and name the encoding when you open text
- 5Write docstrings for public functions, classes and modules
5 in this chapterOpen this chapter- 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
- 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
- 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
- Concurrency and performanceChoosing between asyncio, threads and processes, keeping async code from blocking, and measuring before you optimise.5 practices · 3 minOpens with credits
- 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
- 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.
- Status of Python versions, Python Developer Guidedevguide.python.org/versions/
- What's new in Python 3.15, Python docsdocs.python.org/3.15/whatsnew/3.15.html
- PEP 8: Style Guide for Python Code, Python PEPspeps.python.org/pep-0008/
- Writing your pyproject.toml, Python Packaging User Guidepackaging.python.org/en/latest/guides/writing-pyproject-toml/
- Project structure and files, uv docsdocs.astral.sh/uv/concepts/projects/layout/
- Ruff, Astraldocs.astral.sh/ruff/
- Good Integration Practices, pytest docsdocs.pytest.org/en/stable/explanation/goodpractices.html
- Logging HOWTO, Python docsdocs.python.org/3/howto/logging.html
Related on Sefism
Picked from the other libraries by shared topics.
Roadmaps
See allCheat sheets
See allBest practices
See all- TypeScript Best PracticesModern TypeScript best practices for 2026: strict settings, TypeScript 7, modeling data with unions, safe narrowing, runtime validation, generics and typed linting.
- Secure Coding Best PracticesWrite code that resists attack: untrusted input, validation, parameterized queries, output encoding, safe parsing, uploads, SSRF and errors that fail closed.
- Spring Boot Best PracticesBuild Spring Boot 4 services that are easy to change and safe to run: injection, configuration, REST layer, JPA, virtual threads, Actuator, tests and shutdown.
Checklists
See all- Pull Request Checklist: Before You Open a PRA pull request checklist to run before you ask for review: scope and size, a clean diff, tests, a clear description, safe releases and a tidy branch, each with a way to verify it.
- Career Switch into Tech ChecklistMove into software from another field step by step: choose a direction, follow a learning plan, build proof of skill, use your previous domain, network, plan your runway and target a first role.
Project structures
See all- Python package and CLI project structureA command line tool published to PyPI, laid out the way the Python Packaging User Guide and uv recommend: a src layout, one pyproject.toml for metadata and every tool, a thin Typer CLI over plain, testable modules, and releases uploaded by GitHub Actions with trusted publishing. The example is mdlinks, a tool that finds broken links in Markdown files. For students who have written Python scripts and want to ship one that strangers can
pip install. - TypeScript npm library project structureA small, typed npm package (a retry helper) built the way modern libraries ship in 2026: TypeScript source in
src, bundled by tsdown intodistwith type declarations and anexportsmap, tested with Vitest, checked with publint and Are the Types Wrong, versioned with Changesets and published from CI with npm provenance. For students who want to publish their first package, and for anyone whose package works locally but breaks for users. - Flutter project structureFor anyone building one Dart app for Android, iOS and the web. The tree starts from what flutter create gives you and grows into a feature-first lib/ folder, where each feature keeps its own data, domain and presentation code, then adds tests at every level, flavors, release signing, crash reporting and, for large teams, a melos workspace of local packages.
Career questions
See all- Is .NET worth learning in 2026?Yes, for the right market. .NET is a modern, open-source, cross-platform stack with steady demand in enterprise, finance, government, healthcare and Microsoft-heavy companies, including many in Pakistan, the Gulf, the UK and Europe. .NET 10 is the current long-term support release. Check job posts where you want to work; if C# and .NET show up often, it is a solid choice.
- Should I learn Rust or Go in 2026?For most backend, cloud and DevOps work, learn Go first: it is small, quick to get productive in and common in cloud tooling. Choose Rust if you want systems programming, performance-critical code, embedded or you enjoy going deep on how memory works. Neither is a good first language if you cannot yet build software in another one.
- What should I learn first to become a software engineer in 2026?Learn one language properly, then Git, the command line, how the web works and basic SQL, and build small things with them from week one. AI tools change how you write code, not what you need to understand. Pick a direction only after the foundations feel boring.
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