Skip to content

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.

Find first directory parent for `__file__` in `mo.notebook_dir` (#5906)

Trevor Manze34a71d9046b
diff --git a/marimo/_runtime/runtime.py b/marimo/_runtime/runtime.py
index 8c3eb0b60f..fb5c1a20da 100644
--- a/marimo/_runtime/runtime.py
+++ b/marimo/_runtime/runtime.py
@@ -381,7 +381,10 @@ def notebook_dir() -> pathlib.Path | None:
# NB: __file__ is patched by runner, so always bound to be correct.
filename = ctx.globals.get("__file__", None) or ctx.filename
if filename is not None:
- return pathlib.Path(filename).parent.absolute()
+ path = pathlib.Path(filename).resolve()
+ while not path.is_dir():
+ path = path.parent
+ return path
return None
diff --git a/tests/conftest.py b/tests/conftest.py
index 03c4e861a4..a8256c1f99 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -4,7 +4,6 @@ from __future__ import annotations
import dataclasses
import inspect
import os
-import pathlib
import re
import sys
import textwrap
@@ -151,34 +150,8 @@ class MockedKernel:
self.stderr = MockStderr(self.stream)
self.stdin = MockStdin(self.stream)
self._main = sys.modules["__main__"]
- # On Windows, sys.modules["__main__"].__file__ might be something like
- # "pytest.exe\__main__.py" which causes issues. Use the current test file instead.
- test_file = None
- if (
- sys.platform == "win32"
- and hasattr(sys.modules["__main__"], "__file__")
- and "pytest.exe" in str(sys.modules["__main__"].__file__)
- ):
- # Create an actual dummy Python file for multiprocessing to import
- # Use a unique name to avoid conflicts
- dummy_path = (
- pathlib.Path(__file__).parent
- / f"__test_dummy_{os.getpid()}.py"
- )
- if not dummy_path.exists():
- dummy_path.write_text(
- "# Dummy file for Windows pytest compatibility\n"
- )
- test_file = str(dummy_path)
- # Store the path so we can clean it up in teardown
- self._dummy_file = dummy_path
- else:
- # Use the normal file path
- test_file = getattr(sys.modules["__main__"], "__file__", None)
- self._dummy_file = None
-
module = patches.patch_main_module(
- file=test_file,
+ file=None,
input_override=input_override,
print_override=print_override,
)
@@ -219,9 +192,6 @@ class MockedKernel:
if self.k.module_watcher is not None:
self.k.module_watcher.stop()
sys.modules["__main__"] = self._main
- # Clean up dummy file if created
- if self._dummy_file and self._dummy_file.exists():
- self._dummy_file.unlink(missing_ok=True)
# fixture that provides a kernel (and tears it down)