A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
improvement: add pythonpath confiugration (#3969)
Myles Scolnick48e57bdb48ca
diff --git a/docs/guides/configuration/runtime_configuration.md b/docs/guides/configuration/runtime_configuration.mdindex a0dff51cfd..e74038b564 100644--- a/docs/guides/configuration/runtime_configuration.md+++ b/docs/guides/configuration/runtime_configuration.md@@ -83,3 +83,17 @@ Autoreloading comes in two types:<video controls loop width="100%" height="100%" align="center" src="/_static/docs-module-reloading-lazy.mp4"> </video><figcaption align="center">When set to lazy, marimo's reloader marks cells as stale when you edit Python files.</figcaption></figure>++## Python path++By default, marimo will not add any additional directories to the Python path. This keeps the behavior of `marimo edit nb.py` and `python nb.py` consistent.++You can add directories to the Python path by setting the `pythonpath` key in the+runtime configuration. These directories will be added to the head of `sys.path`,+similar to how the `PYTHONPATH` environment variable works. This allows Python to+find and import modules from these directories.++```toml title="pyproject.toml"+[tool.marimo.runtime]+pythonpath = ["project/src"]+```diff --git a/marimo/_config/config.py b/marimo/_config/config.pyindex ecee64197c..17cb2ad44c 100644--- a/marimo/_config/config.py+++ b/marimo/_config/config.py@@ -108,6 +108,10 @@ class RuntimeConfig(TypedDict):values may affect frontend performance- `std_stream_max_bytes`: the maximum size in bytes of console outputs;larger values may affect frontend performance+ - `pythonpath`: a list of directories to add to the Python search path.+ Directories will be added to the head of sys.path. Similar to the+ `PYTHONPATH` environment variable, the directories will be included in+ where Python will look for imported modules."""auto_instantiate: bool@@ -116,6 +120,7 @@ class RuntimeConfig(TypedDict):watcher_on_save: Literal["lazy", "autorun"]output_max_bytes: intstd_stream_max_bytes: int+ pythonpath: list[str]# TODO(akshayka): remove normal, migrate to compact@@ -333,6 +338,7 @@ DEFAULT_CONFIG: MarimoConfig = {"std_stream_max_bytes": int(os.getenv("MARIMO_STD_STREAM_MAX_BYTES", 1_000_000)),+ "pythonpath": [],},"save": {"autosave": "after_delay",diff --git a/marimo/_runtime/runtime.py b/marimo/_runtime/runtime.pyindex 422c36b142..9f96b55c7f 100644--- a/marimo/_runtime/runtime.py+++ b/marimo/_runtime/runtime.py@@ -458,6 +458,13 @@ class Kernel:# timestamp, to save the user from having to spam the interrupt buttonself.last_interrupt_timestamp: Optional[float] = None+ # Apply pythonpath from config at initialization+ pythonpath = user_config["runtime"].get("pythonpath")+ if pythonpath:+ for path in pythonpath:+ if path not in sys.path:+ sys.path.insert(0, path)+self._preparation_hooks = (preparation_hooksif preparation_hooks is not Nonediff --git a/openapi/api.yaml b/openapi/api.yamlindex 4417d21bb7..fb199caf4a 100644--- a/openapi/api.yaml+++ b/openapi/api.yaml@@ -1369,6 +1369,10 @@ components:type: stringoutput_max_bytes:type: integer+ pythonpath:+ items:+ type: string+ type: arraystd_stream_max_bytes:type: integerwatcher_on_save:@@ -1383,6 +1387,7 @@ components:- watcher_on_save- output_max_bytes- std_stream_max_bytes+ - pythonpathtype: objectsave:properties:@@ -2259,7 +2264,7 @@ components:type: objectinfo:title: marimo API- version: 0.11.12+ version: 0.11.14openapi: 3.1.0paths:/@file/{filename_and_length}:diff --git a/openapi/src/api.ts b/openapi/src/api.tsindex 7d1f6ea822..a7147f66ac 100644--- a/openapi/src/api.ts+++ b/openapi/src/api.ts@@ -2730,6 +2730,7 @@ export interface components {/** @enum {string} */on_cell_change: "lazy" | "autorun";output_max_bytes: number;+ pythonpath: string[];std_stream_max_bytes: number;/** @enum {string} */watcher_on_save: "lazy" | "autorun";diff --git a/tests/_runtime/test_runtime.py b/tests/_runtime/test_runtime.pyindex 7222abfd3f..262e6e0462 100644--- a/tests/_runtime/test_runtime.py+++ b/tests/_runtime/test_runtime.py@@ -1227,6 +1227,58 @@ class TestExecution:if str(tmp_path) in sys.path:sys.path.remove(str(tmp_path))+ async def test_sys_path_updated_with_exec_req(+ self, tmp_path: pathlib.Path, exec_req: ExecReqProvider+ ) -> None:+ custom_path = pathlib.Path("some") / "path"+ filename = tmp_path / "notebook.py"++ try:+ k = Kernel(+ stream=NoopStream(),+ stdout=None,+ stderr=None,+ stdin=None,+ cell_configs={},+ user_config={+ **DEFAULT_CONFIG,+ "runtime": {+ **DEFAULT_CONFIG["runtime"],+ "pythonpath": [str(custom_path)],+ },+ },+ app_metadata=AppMetadata(+ query_params={}, filename=str(filename), cli_args={}+ ),+ enqueue_control_request=lambda _: None,+ module=create_main_module(None, None, None),+ )+ initialize_kernel_context(+ kernel=k,+ stream=k.stream,+ stdout=k.stdout,+ stderr=k.stderr,+ virtual_files_supported=True,+ mode=SessionMode.EDIT,+ )++ # Verify the path was added using exec_req+ await k.run(+ [+ exec_req.get("import sys"),+ exec_req.get("paths = list(sys.path)"),+ ]+ )++ assert str(custom_path) in k.globals["paths"]+ assert str(filename.parent) in k.globals["paths"]+ finally:+ teardown_context()+ if str(tmp_path) in sys.path:+ sys.path.remove(str(tmp_path))+ if str(custom_path) in sys.path:+ sys.path.remove(str(custom_path))+async def test_set_config_before_registering_cell(self, any_kernel: Kernel, exec_req: ExecReqProvider) -> None:diff --git a/tests/_server/templates/snapshots/export1.txt b/tests/_server/templates/snapshots/export1.txtindex a285c4e80d..a12df1d317 100644--- a/tests/_server/templates/snapshots/export1.txt+++ b/tests/_server/templates/snapshots/export1.txt@@ -55,7 +55,7 @@<marimo-filename hidden>notebook.py</marimo-filename><marimo-mode data-mode='read' hidden></marimo-mode><marimo-version data-version='0.0.0' hidden></marimo-version>- <marimo-user-config data-config='{"completion": {"activate_on_typing": true, "copilot": false}, "display": {"cell_output": "above", "code_editor_font_size": 14, "dataframes": "rich", "default_width": "medium", "theme": "light"}, "formatting": {"line_length": 79}, "keymap": {"overrides": {}, "preset": "default"}, "package_management": {"manager": "uv"}, "runtime": {"auto_instantiate": true, "auto_reload": "off", "on_cell_change": "autorun", "output_max_bytes": 8000000, "std_stream_max_bytes": 1000000, "watcher_on_save": "lazy"}, "save": {"autosave": "after_delay", "autosave_delay": 1000, "format_on_save": false}, "server": {"browser": "default", "follow_symlink": false}, "snippets": {"custom_paths": [], "include_default_snippets": true}}' data-overrides='{"formatting": {"line_length": 100}}' hidden></marimo-user-config>+ <marimo-user-config data-config='{"completion": {"activate_on_typing": true, "copilot": false}, "display": {"cell_output": "above", "code_editor_font_size": 14, "dataframes": "rich", "default_width": "medium", "theme": "light"}, "formatting": {"line_length": 79}, "keymap": {"overrides": {}, "preset": "default"}, "package_management": {"manager": "uv"}, "runtime": {"auto_instantiate": true, "auto_reload": "off", "on_cell_change": "autorun", "output_max_bytes": 8000000, "pythonpath": [], "std_stream_max_bytes": 1000000, "watcher_on_save": "lazy"}, "save": {"autosave": "after_delay", "autosave_delay": 1000, "format_on_save": false}, "server": {"browser": "default", "follow_symlink": false}, "snippets": {"custom_paths": [], "include_default_snippets": true}}' data-overrides='{"formatting": {"line_length": 100}}' hidden></marimo-user-config><marimo-app-config data-config='{"width": "compact"}' hidden></marimo-app-config><marimo-server-token data-token='token' hidden></marimo-server-token><title>notebook</title>diff --git a/tests/_server/templates/snapshots/export2.txt b/tests/_server/templates/snapshots/export2.txtindex 2226a479d1..375bf44a7a 100644--- a/tests/_server/templates/snapshots/export2.txt+++ b/tests/_server/templates/snapshots/export2.txt@@ -55,7 +55,7 @@<marimo-filename hidden></marimo-filename><marimo-mode data-mode='read' hidden></marimo-mode><marimo-version data-version='0.0.0' hidden></marimo-version>- <marimo-user-config data-config='{"completion": {"activate_on_typing": true, "copilot": false}, "display": {"cell_output": "above", "code_editor_font_size": 14, "dataframes": "rich", "default_width": "medium", "theme": "light"}, "formatting": {"line_length": 79}, "keymap": {"overrides": {}, "preset": "default"}, "package_management": {"manager": "uv"}, "runtime": {"auto_instantiate": true, "auto_reload": "off", "on_cell_change": "autorun", "output_max_bytes": 8000000, "std_stream_max_bytes": 1000000, "watcher_on_save": "lazy"}, "save": {"autosave": "after_delay", "autosave_delay": 1000, "format_on_save": false}, "server": {"browser": "default", "follow_symlink": false}, "snippets": {"custom_paths": [], "include_default_snippets": true}}' data-overrides='{"formatting": {"line_length": 100}}' hidden></marimo-user-config>+ <marimo-user-config data-config='{"completion": {"activate_on_typing": true, "copilot": false}, "display": {"cell_output": "above", "code_editor_font_size": 14, "dataframes": "rich", "default_width": "medium", "theme": "light"}, "formatting": {"line_length": 79}, "keymap": {"overrides": {}, "preset": "default"}, "package_management": {"manager": "uv"}, "runtime": {"auto_instantiate": true, "auto_reload": "off", "on_cell_change": "autorun", "output_max_bytes": 8000000, "pythonpath": [], "std_stream_max_bytes": 1000000, "watcher_on_save": "lazy"}, "save": {"autosave": "after_delay", "autosave_delay": 1000, "format_on_save": false}, "server": {"browser": "default", "follow_symlink": false}, "snippets": {"custom_paths": [], "include_default_snippets": true}}' data-overrides='{"formatting": {"line_length": 100}}' hidden></marimo-user-config><marimo-app-config data-config='{"width": "compact"}' hidden></marimo-app-config><marimo-server-token data-token='token' hidden></marimo-server-token><title>marimo</title>diff --git a/tests/_server/templates/snapshots/export3.txt b/tests/_server/templates/snapshots/export3.txtindex 952f7b8fc5..f85f9b4e73 100644--- a/tests/_server/templates/snapshots/export3.txt+++ b/tests/_server/templates/snapshots/export3.txt@@ -55,7 +55,7 @@<marimo-filename hidden>notebook.py</marimo-filename><marimo-mode data-mode='read' hidden></marimo-mode><marimo-version data-version='0.0.0' hidden></marimo-version>- <marimo-user-config data-config='{"completion": {"activate_on_typing": true, "copilot": false}, "display": {"cell_output": "above", "code_editor_font_size": 14, "dataframes": "rich", "default_width": "medium", "theme": "light"}, "formatting": {"line_length": 79}, "keymap": {"overrides": {}, "preset": "default"}, "package_management": {"manager": "uv"}, "runtime": {"auto_instantiate": true, "auto_reload": "off", "on_cell_change": "autorun", "output_max_bytes": 8000000, "std_stream_max_bytes": 1000000, "watcher_on_save": "lazy"}, "save": {"autosave": "after_delay", "autosave_delay": 1000, "format_on_save": false}, "server": {"browser": "default", "follow_symlink": false}, "snippets": {"custom_paths": [], "include_default_snippets": true}}' data-overrides='{"formatting": {"line_length": 100}}' hidden></marimo-user-config>+ <marimo-user-config data-config='{"completion": {"activate_on_typing": true, "copilot": false}, "display": {"cell_output": "above", "code_editor_font_size": 14, "dataframes": "rich", "default_width": "medium", "theme": "light"}, "formatting": {"line_length": 79}, "keymap": {"overrides": {}, "preset": "default"}, "package_management": {"manager": "uv"}, "runtime": {"auto_instantiate": true, "auto_reload": "off", "on_cell_change": "autorun", "output_max_bytes": 8000000, "pythonpath": [], "std_stream_max_bytes": 1000000, "watcher_on_save": "lazy"}, "save": {"autosave": "after_delay", "autosave_delay": 1000, "format_on_save": false}, "server": {"browser": "default", "follow_symlink": false}, "snippets": {"custom_paths": [], "include_default_snippets": true}}' data-overrides='{"formatting": {"line_length": 100}}' hidden></marimo-user-config><marimo-app-config data-config='{"width": "compact"}' hidden></marimo-app-config><marimo-server-token data-token='token' hidden></marimo-server-token><title>notebook</title>diff --git a/tests/_server/templates/snapshots/export4.txt b/tests/_server/templates/snapshots/export4.txtindex 37e1bf8996..d1e533a52b 100644--- a/tests/_server/templates/snapshots/export4.txt+++ b/tests/_server/templates/snapshots/export4.txt@@ -55,7 +55,7 @@<marimo-filename hidden>notebook.py</marimo-filename><marimo-mode data-mode='read' hidden></marimo-mode><marimo-version data-version='0.0.0' hidden></marimo-version>- <marimo-user-config data-config='{"completion": {"activate_on_typing": true, "copilot": false}, "display": {"cell_output": "above", "code_editor_font_size": 14, "dataframes": "rich", "default_width": "medium", "theme": "light"}, "formatting": {"line_length": 79}, "keymap": {"overrides": {}, "preset": "default"}, "package_management": {"manager": "uv"}, "runtime": {"auto_instantiate": true, "auto_reload": "off", "on_cell_change": "autorun", "output_max_bytes": 8000000, "std_stream_max_bytes": 1000000, "watcher_on_save": "lazy"}, "save": {"autosave": "after_delay", "autosave_delay": 1000, "format_on_save": false}, "server": {"browser": "default", "follow_symlink": false}, "snippets": {"custom_paths": [], "include_default_snippets": true}}' data-overrides='{"formatting": {"line_length": 100}}' hidden></marimo-user-config>+ <marimo-user-config data-config='{"completion": {"activate_on_typing": true, "copilot": false}, "display": {"cell_output": "above", "code_editor_font_size": 14, "dataframes": "rich", "default_width": "medium", "theme": "light"}, "formatting": {"line_length": 79}, "keymap": {"overrides": {}, "preset": "default"}, "package_management": {"manager": "uv"}, "runtime": {"auto_instantiate": true, "auto_reload": "off", "on_cell_change": "autorun", "output_max_bytes": 8000000, "pythonpath": [], "std_stream_max_bytes": 1000000, "watcher_on_save": "lazy"}, "save": {"autosave": "after_delay", "autosave_delay": 1000, "format_on_save": false}, "server": {"browser": "default", "follow_symlink": false}, "snippets": {"custom_paths": [], "include_default_snippets": true}}' data-overrides='{"formatting": {"line_length": 100}}' hidden></marimo-user-config><marimo-app-config data-config='{"css_file": "custom.css", "width": "compact"}' hidden></marimo-app-config><marimo-server-token data-token='token' hidden></marimo-server-token><title>notebook</title>diff --git a/tests/_server/templates/snapshots/export5.txt b/tests/_server/templates/snapshots/export5.txtindex 5e837ae9d2..b6d9964bbf 100644--- a/tests/_server/templates/snapshots/export5.txt+++ b/tests/_server/templates/snapshots/export5.txt@@ -55,7 +55,7 @@<marimo-filename hidden>notebook.py</marimo-filename><marimo-mode data-mode='read' hidden></marimo-mode><marimo-version data-version='0.0.0' hidden></marimo-version>- <marimo-user-config data-config='{"completion": {"activate_on_typing": true, "copilot": false}, "display": {"cell_output": "above", "code_editor_font_size": 14, "dataframes": "rich", "default_width": "medium", "theme": "light"}, "formatting": {"line_length": 79}, "keymap": {"overrides": {}, "preset": "default"}, "package_management": {"manager": "uv"}, "runtime": {"auto_instantiate": true, "auto_reload": "off", "on_cell_change": "autorun", "output_max_bytes": 8000000, "std_stream_max_bytes": 1000000, "watcher_on_save": "lazy"}, "save": {"autosave": "after_delay", "autosave_delay": 1000, "format_on_save": false}, "server": {"browser": "default", "follow_symlink": false}, "snippets": {"custom_paths": [], "include_default_snippets": true}}' data-overrides='{"formatting": {"line_length": 100}}' hidden></marimo-user-config>+ <marimo-user-config data-config='{"completion": {"activate_on_typing": true, "copilot": false}, "display": {"cell_output": "above", "code_editor_font_size": 14, "dataframes": "rich", "default_width": "medium", "theme": "light"}, "formatting": {"line_length": 79}, "keymap": {"overrides": {}, "preset": "default"}, "package_management": {"manager": "uv"}, "runtime": {"auto_instantiate": true, "auto_reload": "off", "on_cell_change": "autorun", "output_max_bytes": 8000000, "pythonpath": [], "std_stream_max_bytes": 1000000, "watcher_on_save": "lazy"}, "save": {"autosave": "after_delay", "autosave_delay": 1000, "format_on_save": false}, "server": {"browser": "default", "follow_symlink": false}, "snippets": {"custom_paths": [], "include_default_snippets": true}}' data-overrides='{"formatting": {"line_length": 100}}' hidden></marimo-user-config><marimo-app-config data-config='{"app_title": "My App", "html_head_file": "head.html", "width": "compact"}' hidden></marimo-app-config><marimo-server-token data-token='token' hidden></marimo-server-token><title>My App</title>