Importing local code¶
Importing local Python code lets you reuse functions and classes across notebooks. How you make the code available depends on where the files live and whether the code is packaged.
Import from the notebook's directory¶
Python files alongside a notebook can be imported directly:
In a cell of my_notebook.py:
Import from a source directory¶
If notebooks and source code live in separate directories, tell marimo where
to find your modules. For example, a project might keep its code in src/:
Add src to marimo's Python search path in pyproject.toml:
The notebook can then import my_module. The pythonpath setting applies in
the editor, when serving the notebook as an app, and when running it as a script.
Import a local package¶
If your code is organized as a Python package,
install it in the notebook's environment. For example, a package named
my_package might have this layout:
.
├── notebooks
│ └── my_notebook.py
├── pyproject.toml
└── src
└── my_package
├── __init__.py
└── my_module.py
Once installed, the notebook can import from the package:
An editable installation uses your source files directly, so you don't need to reinstall the package after each change. See working in projects for setup in a shared environment, or sandbox configuration for declaring the local package in the notebook's own requirements.
Picking up source changes
Module autoreloading lets marimo pick up changes to imported code while your notebook is open.