tblib is a Python library that allows for the serialization of exceptions and tracebacks, enabling them to be pickled and unpickled across different processes. This is particularly useful for robust error handling in multiprocessing or distributed systems like Celery. It also provides methods to create tracebacks from strings or serialize them to and from dictionaries. The current version is 3.2.2, with releases occurring periodically, often for bug fixes and Python version support updates.
pip install tblibVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to serialize an exception and its traceback using `tblib` and Python's `pickle` module. First, `pickling_support.install()` is called to enable traceback pickling. Then, `sys.exc_info()` captures the current exception, which is then serialized. Finally, the serialized exception is unpickled and its traceback can be inspected or re-raised. Note that `pickling_support.install()` typically needs to be called in the environment where the exceptions are *created* for full functionality.
Ensure your project uses Python 3.9 or newer if using tblib >= 3.2.2. Refer to the changelog for specific Python version compatibility per tblib release.
Test your custom exception handling with tblib >= 3.2.0. If you encounter issues, consider explicitly defining `__reduce__` methods for complex custom exceptions, or using the `@pickling_support.install` decorator directly on your custom exception classes.
Only unpickle data that you trust. tblib itself notes that 'You are solely responsible for security problems should you decide to use the pickle support.'
Be aware of this limitation; `tblib` is primarily for re-raising and printing tracebacks, not for full debugging inspection of runtime state across processes.
Call `tblib.pickling_support.install()` at the beginning of your script or process to enable tblib's custom pickling handlers for tracebacks.
Install `tblib` using pip: `pip install tblib`. Ensure the import statement is `import tblib.pickling_support` or `from tblib.pickling_support import install`.
Ensure that `tblib.Traceback()` is called only when an exception is actively being handled, so `sys.exc_info()[2]` yields a valid traceback object.
Ensure you have either `import tblib.pickling_support` (and then call `tblib.pickling_support.install()`) or `from tblib.pickling_support import install` (and then call `install()`).
Iterate through the `tblib.Traceback` object's `frames` attribute and access frame-specific information from each frame object: `for frame in tb_obj.frames: print(frame.lineno)`.