importnb

raw JSON →
2023.11.1 verified Fri May 01 auth: no python

Import Jupyter Notebooks as Python modules and scripts. Supports Python 3.8+, and allows importing .ipynb files directly. Version 2023.11.1 adds Python 3.12 compatibility, top-level await async support, and improved entry points. Release cadence is sporadic, with minor updates as needed.

pip install importnb
error ModuleNotFoundError: No module named 'importnb.notebook'
cause Incorrect import path: trying to import submodule that does not exist.
fix
Use: from importnb import Notebook
error ValueError: The notebook file 'my_notebook.ipynb' does not exist.
cause The file is missing or not in the current working directory.
fix
Ensure the .ipynb file is in the same directory as your script or use sys.path.append().
error SynaxError: invalid syntax in cell (notebook)
cause The notebook contains Python code that is not valid Python 3.x (e.g., syntax from a future version).
fix
Check notebook cells for syntax errors and ensure compatibility with your Python version.
gotcha importing a notebook with the same name as a standard library module (e.g., `json`) will shadow the standard library. Ensure filenames are unique.
fix Rename your notebook file to avoid conflicts.
deprecated The `parameterize` feature (from importnb.parameterize) is optional and may be removed in future versions. It is no longer maintained.
fix Avoid relying on importnb.parameterize; use pytest or parametrize directly.
gotcha Notebooks must have a `.ipynb` extension; files with other extensions (e.g., `.py`) are not supported.
fix Ensure your notebook files end with .ipynb.

Basic usage: use Notebook context manager to import .ipynb files as modules.

import os
os.environ['SOME_KEY'] = 'test'  # placeholder auth
import importnb
from importnb import Notebook
with Notebook():
    import my_notebook  # my_notebook.ipynb in the same directory