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
fix: add notebook's current directory to path (#1132)
Akshay Agrawal2948061c5178
diff --git a/marimo/_runtime/runtime.py b/marimo/_runtime/runtime.pyindex c2fbaf497e..ac7fd014ab 100644--- a/marimo/_runtime/runtime.py+++ b/marimo/_runtime/runtime.py@@ -8,6 +8,7 @@ import dataclassesimport ioimport itertoolsimport os+import pathlibimport signalimport sysimport threading@@ -273,6 +274,17 @@ class Kernel:self._module = patches.patch_main_module(file=self.app_metadata.filename, input_override=input_override)+ if self.app_metadata.filename is not None:+ try:+ notebook_directory = str(+ pathlib.Path(self.app_metadata.filename).parent.absolute()+ )+ if notebook_directory not in sys.path:+ sys.path.insert(0, notebook_directory)+ except Exception as e:+ LOGGER.warning(+ "Failed to add directory to path (error %e)", str(e)+ )self.graph = dataflow.DirectedGraph()self.cell_metadata: dict[CellId_t, CellMetadata] = {diff --git a/tests/_runtime/test_runtime.py b/tests/_runtime/test_runtime.pyindex 41fb2e482b..8671b59737 100644--- a/tests/_runtime/test_runtime.py+++ b/tests/_runtime/test_runtime.py@@ -2,19 +2,23 @@from __future__ import annotationsimport sys-from typing import Sequence+from types import ModuleType+from typing import TYPE_CHECKING, Sequenceimport pytest+from marimo._config.config import DEFAULT_CONFIGfrom marimo._messaging.errors import (CycleError,DeleteNonlocalError,Error,MultipleDefinitionError,)+from marimo._messaging.types import NoopStreamfrom marimo._plugins.ui._core.ids import IDProviderfrom marimo._runtime.dataflow import Edgefrom marimo._runtime.requests import (+ AppMetadata,CreationRequest,DeleteRequest,ExecutionRequest,@@ -24,6 +28,9 @@ from marimo._runtime.requests import (from marimo._runtime.runtime import Kernelfrom tests.conftest import ExecReqProvider+if TYPE_CHECKING:+ import pathlib+def _check_edges(error: Error, expected_edges: Sequence[Edge]) -> None:assert isinstance(error, CycleError)@@ -859,6 +866,31 @@ async def test_set_ui_element_value_with_cell_run(assert k.globals["counter"][0] == 1+def test_sys_path_updated(tmp_path: pathlib.Path):+ main: ModuleType | None = None+ try:+ filename = str(tmp_path / "notebook.py")+ if "__main__" in sys.modules:+ # kernel patches __main__; need to reset it after test+ main = sys.modules["__main__"]+ Kernel(+ stream=NoopStream(),+ stdout=None,+ stderr=None,+ stdin=None,+ cell_configs={},+ user_config=DEFAULT_CONFIG,+ app_metadata=AppMetadata(query_params={}, filename=filename),+ )+ assert str(tmp_path) in sys.path+ assert str(tmp_path) == sys.path[0]+ finally:+ if str(tmp_path) in sys.path:+ sys.path.remove(str(tmp_path))+ if main is not None:+ sys.modules["__main__"] = main++class TestAsyncIO:@staticmethodasync def test_toplevel_await_allowed(