execnb
raw JSON → 0.2.2 verified Mon Apr 27 auth: no python
A Python library for executing Jupyter notebooks in non-interactive environments. Supports capturing outputs, rendering inline plots, and running notebooks from the command line. Current version 0.2.2, released March 2024. Maintained by AnswerDotAI, release cadence irregular.
pip install execnb Common errors
error ModuleNotFoundError: No module named 'fastcore' ↓
cause fastcore is a dependency that must be installed separately.
fix
Run: pip install fastcore
error ImportError: cannot import name 'exec_nb' from 'execnb' ↓
cause The exec_nb function was moved to the nbio submodule in v0.2.0.
fix
Use: from execnb.nbio import exec_nb
error AttributeError: 'NoneType' object has no attribute 'text' ↓
cause Cell output may be a different type (e.g., display data) without a text attribute.
fix
Check output type before accessing .text, e.g., output.get('text') or if hasattr(output, 'text')
Warnings
breaking The nbio module was moved from execnb.nbio to fastcore.nbio in version 0.2.0. Code using old imports will break. ↓
fix Update imports to use execnb.nbio for exec_nb, read_nb, NBIO. If using fastcore.nbio directly, adjust accordingly.
deprecated The old exec_nb function without specifying mpl_format may set up inline matplotlib automatically, which can interfere with headless environments. ↓
fix Use mpl_format=None in exec_nb to skip matplotlib setup when not needed.
gotcha Executing notebooks that use interactive widgets or require user input will fail. execnb is designed for non-interactive execution. ↓
fix Ensure notebooks are prepared for batch execution, avoiding input() calls or interactive widgets.
gotcha Cell IDs are not extracted from cell metadata automatically in versions prior to 0.1.17. This can cause duplicate or missing cell identifiers. ↓
fix Upgrade to >=0.1.17 or manually set cell IDs in the notebook metadata.
Imports
- exec_nb wrong
from execnb import exec_nbcorrectfrom execnb.nbio import exec_nb - read_nb wrong
from execnb import read_nbcorrectfrom execnb.nbio import read_nb - NBIO
from execnb.nbio import NBIO
Quickstart
from execnb.nbio import exec_nb
from pathlib import Path
# Execute a notebook and capture outputs
nb = exec_nb('notebook.ipynb')
for cell in nb.cells:
if cell.outputs:
for output in cell.outputs:
print(output.text if hasattr(output, 'text') else output)