Skip to main content

impynb

Import Jupyter notebooks as Python modules!

Installation

pip install impynb

Usage

Assume your project looks like this:

my-project/
  my_project/
    __init__.py
    my_notebook.ipynb
    my_module.py
    util.py
  pyproject.toml

You can import notebooks from Python modules:

# my_module.py
import impynb
from .my_notebook import some_function  # <-- import notebook
some_function(3, 4)

Importing local Python modules from notebooks also works:

# cell in my_notebook.ipynb
import impynb
from .util import add_two_numbers  # <-- import local Python module

def some_function(a, b):
  return add_two_numbers(a, b)

Features

  • Understands package initializers: Module initializers (__init__.py) can also be written as Python notebooks (__init__.ipynb). If a package contains both files, only __init__.py will be used and __init__.ipynb is ignored for consistency reasons.
    # my-package/my_package/my_module/__init__.ipynb (first cell)
    variable = 42
    
    # my-package/my_package/main.py
    import impynb
    from . import my_module
    my_module.variable  # 42
    
  • Async code: Notebook cells containing async code will be executed in the default event loop:
    # my_notebook.ipynb
    from asyncio import sleep
    await sleep(0.1)
    variable = 42
    
    # my_module.py
    import impynb
    from . import my_notebook
    my_notebook.variable  # 42
    
    The event loop can be configured (see below).
  • Exception handling: Exceptions thrown in notebooks are displayed correctly in the stack trace
    # my_notebook.ipynb (first cell)
    raise NotImplementedError()
    
    # my_module.py
    import impynb
    from .my_notebook import *
    # raises an exception containing the problematic cell:
    #
    #   File ".../my_module.py", line 2
    #     from .my_notebook import *
    #     ^^^^^^^^^^^^^^^^^^^^^^^^^^
    #   ...
    #   File ".../my_notebook.ipynb[cell-0]", line 1, in <module>
    #     raise NotImplementedError()
    
  • Code Cell Filtering: Notebook cells marked with a comment #!ignore at the top will be skipped when importing the notebook.
    #!ignore
    # this code will not run when importing the notebook
    some_heavy_computation()
    
  • Advanced configuration: The module can be configured to use different markers for ignorable cells or a custom event loop (see below).

Advanced configuration

The package can be configured using the init method:

import impynb

impynb.init(
    skip_cell_tags=["test"],
    # ...
)

# configuration only applies for imports *after* init() call
from .my_notebook import nice_function

Provide a notebook path

If you like to use impynb with a tool that gives no information about the path to the current notebook file (e.g. papermill), then you need to call init() at the start of the notebook and pass the notebook path as argument:

# my_notebook.ipynb (first cell)
import os
import impynb
impynb.init(os.environ.get("NOTEBOOK_PATH"))

# my_module.py
import os
import papermill as pm

pm.execute_notebook(
    "my_notebook.ipynb",
    "result_file.ipynb",
    kernel_name="python3",
    parameters={"NOTEBOOK_PATH": os.path.abspath(".")},
)

Skip tagged cells

Cells in Jupyter notebooks can be tagged. You can configure impynb to skip executing cells having a specific tag on them:

import impynb
impynb.init(skip_cell_tags=["test"])

Custom event loop

Executing async code will run in the default event loop if not specified otherwise. However, impynb can be configured to use a different event loop:

import asyncio
import impynb

my_event_loop = asyncio.new_event_loop()
impynb.init(event_loop=my_event_loop)

# async cells will be run in the custom event loop
from . import my_notebook

Context manager

The package provides a context manager for a temporary reconfiguration of import settings:

import impynb

with impynb.configure(skip_cell_tags=["test"]):
  # will ignore cells tagged as "test"
  from . import my_notebook

# will not ignore cells anymore
import . from my_other_notebook

All arguments of the impynb.init method (except notebook_path) are available in impynb.configure.

Compatibility

impynb was tested on the following Jupyter platforms:

  • ✅ VSCode: via __vsc_ipynb_file__ variable (source)
  • ✅ Jupyter Notebooks: via JPY_SESSION_NAME environment variable or __session__ as fallback (source)
  • ✅ Jupyter Lab: same as Jupyter Notebooks
  • ✅ Google Colab: creates a virtual Python module with a name based on the COLAB_NOTEBOOK_ID env variable in the working directory (usually /content). Since a valid Python module requires an __init__.py (or __init__.ipynb) file, you need to create one of these files in the working directory to import modules from there.
  • ⚪ Papermill: provides no indication about the current notebook name or path (as far as I know) and the user needs to provide this manually (see above)

License

This project is licensed under the BSD 3-Clause "New" or "Revised" License. See the LICENSE file for details.

Metadata

Release files for impynb 0.1.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for impynb 0.1.7
File Size Uploaded Provenance Actions
impynb-0.1.7.tar.gz 10.7 kB With provenance Details for impynb-0.1.7.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for impynb 0.1.7
File Interpreter ABI Platform Actions
impynb-0.1.7-py3-none-any.whl Python 3 none any Details for impynb-0.1.7-py3-none-any.whl

Total release size: 21.9 kB

Release files / impynb-0.1.7.tar.gz

Download URL impynb-0.1.7.tar.gz
Size 10.7 kB
Tags Source
SHA-256 checksum
How to use checksums
b8a6b2a8989dbbf6826c5d43baf2fe4ad90eac266fe575abec7121a7cc3182f5
BLAKE2b-256 checksum
How to use checksums
e7f03e93d97314faf4f2fd4ef839951f035ff744d8709e770f7b01a2564a21e4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jan 5, 2026.

Transparency log

Release files / impynb-0.1.7-py3-none-any.whl

Download URL impynb-0.1.7-py3-none-any.whl
Size 11.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5c5b5846b5ede924483a163e1baf169f6b19e35b678c656b73f96847fab20007
BLAKE2b-256 checksum
How to use checksums
a32ee4d66b95017bb456382f380d79eabfcbef94e387f296182a049cd95f9f7f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jan 5, 2026.

Transparency log

Security for impynb 0.1.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for impynb-0.1.7.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jan 5, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page