importlib.metadata.version()
2025-07-30
Sometimes, itâs useful to branch the behaviour of your code based on the version of a package that you have installed. This may be to support an upgrade in your project, or for your own package to support different versions of a dependency.
Python: fixBrokenPipeError
when piping output to other commands2025-07-20
If youâve written a Python script that outputs a lot of data, then piped that output into another command that only reads part of it, you might have encountered a BrokenPipeError
. For example, take this script:
A neat testing pattern is writing common tests in a base class and then applying them to multiple objects through subclassing. Doing so can help you test smarter and cover more code with less boilerplate.
Python: a quick cProfile recipe with pstats2025-05-20Python comes with two built-in profilers for measuring the performance of your code: cProfile and profile. They have the same API, but cProfile is a C extension, while profile is implemented in Python. You nearly always want to use cProfile, as itâs faster and doesnât skew measurements as much.
pre-commit: install with uv2025-05-07pre-commit is my favourite Git-integrated ârun things on commitâ tool. It acts as a kind of package manager, installing tools as necessary from their Git repositories. This makes it fairly easy to set up: all you need to install is pre-commit itself, and it takes things from there.
Python type hints: mixin classes2025-05-01In Python, a mixin class is a class that is not intended to be used directly, but instead âmixed inâ to other classes through multiple inheritance. Mixins are not really a language feature but more of a conventional pattern allowed by Pythonâs multiple inheritance rules. Unfortunately, adding type hints to mixin classes can be tricky because they implicitly require subclasses to fit a certain shape.
Python: fuss-free use of Homebrew libraries for package dependencies2025-01-10Some Python packages require native libraries to be installed on your system when you install them. For example, mysqlclient requires libssl and libcrypto. If those libraries are missing at install time, clang, the C compiler, fails with a message like:
Python: test for unraisable exceptions withunittest.mock
2025-01-07
A few days ago, I blogged about debugging unraisable exceptions with Rich. Hereâs a sequel on testing that some block of code doesnât trigger any unraisable exceptions.
Python: debug unraisable exceptions with Rich2025-01-02Take this Python class:
Python: spy for changes withsys.monitoring
2024-12-30
Python 3.12 introduced sys.monitoring
, a new framework for âmonitoringâ tools like debuggers and profilers to hook into. It provides fine-grained control so tools can listen only to certain events on specific lines of code. The framework came from PEP 669, thanks to Mark Shannon of the Faster CPython team.
Sometimes, tests need temporary files or directories. You can do this in Pythonâs unittest
with the standard library tempfile
module. Letâs look at some recipes to do so within individual tests and setUp()
.
uv is a new, fast Python packaging tool. Since the August 20 update, uv can now manage both versions of Python and Python-powered tools.
Python: profile total memory allocated with tracemalloc2024-08-30tracemalloc is Pythonâs standard library module for tracking memory allocations. It has many bells and whistles for detailed analysis, allowing you to slice allocations by file and line or compare snapshots. But for simple purposes, displaying the total memory allocated is sufficient, which is what this recipe does:
Python: Fail in three characters with1/0
2024-06-18
Hereâs a code snippet I often type:
Python: Import by string withpkgutil.resolve_name()
2024-06-17
Django and other frameworks often allow you to configure classes, functions, or modules to import using strings. To do the same in your own code, use pkgutil.resolve_name()
from the standard library (added in Python 3.9):
Sometimes, you want to test a function that uses an inner import and mock that imported object. For example, say you wanted to mock dig()
in this example:
class.__subclasses__()
returns a list of the direct subclasses of a given class:
Copy-paste-tweaking library code feels like a dirty but inevitable programming practice. Often driven by deadlines or other constraints, it seems all projects end up with something copy-pasted in and tweaked for one specific use case.
Python: Make line number paths withinspect
2024-04-25
Many terminals and text editors support what Iâll call âline number pathsâ of the form <filename>:<lineno>
. Opening that path, whether by clicking or passing to a CLI, opens the given file at that line.
Earlier this week, I made another podcast appearance on The Python Show, episode 22: Git and Django with Adam Johnson. As fellow authors, Mike and I talked a lot about the writing process, on topics like:
My appearance on the PyBites Podcast2023-11-13Last week, I made another podcast appearance on the PyBites Podcast, episode 139: Maximizing Your Developer Experience (DX) with Adam Johnson. We talked about various topics, including:
My appearance on The Real Python Podcast 1792023-11-09I had the pleasure of returning to The Real Python podcast in last weekâs episode 179, Improving Your Git Developer Experience in Python. It was great to catch up with host Christopher Bailey and chat about:
GitHub Actions: Faster Python runs with cached virtual environments2023-11-02Most projects I work on use Python, good olâ Pip, and pip-tools. Below is a pattern Iâve used to speed up the GitHub Actions workflow runs on several such projects. On larger projects with many dependencies, it can save tens of seconds per run.
Introducing flake8-logging2023-09-07The Python standard libraryâs logging
module is a go-to for adding observability to applications. Many tools also integrated with it or enhance its capabilities, such as structlog and Sentry.
When trying to improve a slow function or module, itâs always a good idea to profile it. Hereâs a snippet for quickly profiling a section of code with Pythonâs cProfile module, in two flavours. Itâs adapted from the cProfile documentationâs Profile
example. I have used versions of this snippet over the years to narrow in on performance issues.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4