Python's Missing Debug Print Command
devtools provides an enhanced debug print command for Python, offering formatted output with file, line number, function information, and variable names. It's designed to be more readable than the standard `print()` function, especially for complex data structures. The library is actively maintained, with its current version being 0.12.2, and has a consistent release cadence.
Warnings
- breaking `Pygments` became a mandatory dependency in `v0.12.0` (August 2023). Environments that previously used `devtools` without `Pygments` installed (and thus without syntax highlighting) will now require `Pygments` for successful installation.
- gotcha The `debug()` function returns its arguments. If a single non-keyword argument is passed, it returns that argument. If multiple arguments are passed (positional or keyword), they are returned as a tuple (with a dictionary for kwargs). This differs from `print()` which always returns `None`.
- gotcha The `debug.install()` method, intended to add `debug` to `__builtins__` for usage without explicit import, does not directly modify your Python environment. Instead, it prints instructions for you to manually create or edit a `sitecustomize.py` file.
- breaking Python-devtools does not currently support Python 3.13, which can lead to unexpected behavior or errors if used with this version.
Install
-
pip install devtools
Imports
- debug
from devtools import debug
Quickstart
from devtools import debug
def example_function(a, b):
result = a * b
debug(a, b, result)
return result
my_list = [1, 2, {'key': 'value', 'nested': [3, 4]}]
debug(my_list)
example_function(10, 5)
class MyClass:
def __init__(self, name):
self.name = name
self.data = {'id': 123, 'status': 'active'}
obj = MyClass('TestObject')
debug(obj.name, obj.data)