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.

improvement: improve uv projects detection (#5123)

Myles Scolnickcf78ada1477b
diff --git a/marimo/_runtime/packages/pypi_package_manager.py b/marimo/_runtime/packages/pypi_package_manager.py
index ce9100bacd..2f3c36b36a 100644
--- a/marimo/_runtime/packages/pypi_package_manager.py
+++ b/marimo/_runtime/packages/pypi_package_manager.py
@@ -321,6 +321,9 @@ class UvPackageManager(PypiPackageManager):
- The "uv.lock" file exists where the "VIRTUAL_ENV" is
- The "pyproject.toml" file exists where the "VIRTUAL_ENV" is
+ OR
+ - The "UV_PROJECT_ENVIRONMENT" is equal to "VIRTUAL_ENV"
+
If at least one of these conditions are not met,
we are in a temporary virtual environment (e.g. `uvx marimo edit` or `uv --with=marimo run marimo edit`)
or in the currently activated virtual environment (e.g. `uv venv`).
@@ -329,6 +332,12 @@ class UvPackageManager(PypiPackageManager):
venv_path = os.environ.get("VIRTUAL_ENV", None)
if not venv_path:
return False
+
+ # Check that the "UV_PROJECT_ENVIRONMENT" is equal to "VIRTUAL_ENV"
+ uv_project_environment = os.environ.get("UV_PROJECT_ENVIRONMENT", None)
+ if uv_project_environment == venv_path:
+ return True
+
# Check that the `UV` environment variable is set
# This tells us that marimo was run by uv
uv_env_exists = os.environ.get("UV", None)
diff --git a/marimo/_runtime/packages/utils.py b/marimo/_runtime/packages/utils.py
index 24697408c8..d0c14169a5 100644
--- a/marimo/_runtime/packages/utils.py
+++ b/marimo/_runtime/packages/utils.py
@@ -27,6 +27,10 @@ def is_dockerized() -> bool:
return os.path.exists("/.dockerenv")
+def is_modal_image() -> bool:
+ return os.environ.get("MODAL_TASK_ID") is not None
+
+
def is_python_isolated() -> bool:
"""Returns True if not using system Python"""
return (
@@ -34,6 +38,7 @@ def is_python_isolated() -> bool:
or in_conda_env()
or is_pyodide()
or is_dockerized()
+ or is_modal_image()
)
diff --git a/tests/_runtime/packages/test_pypi_package_manager.py b/tests/_runtime/packages/test_pypi_package_manager.py
index 978d822ca7..b77f5272f3 100644
--- a/tests/_runtime/packages/test_pypi_package_manager.py
+++ b/tests/_runtime/packages/test_pypi_package_manager.py
@@ -285,72 +285,29 @@ def test_uv_list_packages(mock_run: MagicMock):
assert packages[1] == PackageDescription(name="package2", version="2.1.0")
-@patch("subprocess.run")
[email protected](PipPackageManager, "is_manager_installed", return_value=True)
-async def test_install_with_upgrade(
- mock_is_installed: MagicMock, mock_run: MagicMock
-):
- del mock_is_installed
- mock_run.return_value = MagicMock(returncode=0)
-
- result = await manager._install("package1", upgrade=True)
-
- mock_run.assert_called_once_with(
- ["pip", "--python", PY_EXE, "install", "--upgrade", "package1"],
- )
- assert result is True
-
-
-@patch("subprocess.run")
[email protected](PipPackageManager, "is_manager_installed", return_value=True)
-async def test_install_without_upgrade(
- mock_is_installed: MagicMock, mock_run: MagicMock
-):
- del mock_is_installed
- mock_run.return_value = MagicMock(returncode=0)
-
- result = await manager._install("package1", upgrade=False)
-
- mock_run.assert_called_once_with(
- ["pip", "--python", PY_EXE, "install", "package1"],
- )
- assert result is True
-
-
-@patch("subprocess.run")
[email protected](UvPackageManager, "is_in_uv_project", False)
-async def test_uv_install_with_upgrade_not_in_project(mock_run: MagicMock):
- """Test UV install with upgrade uses pip subcommand when not in UV project"""
- mock_run.return_value = MagicMock(returncode=0)
+ "os.environ",
+ {
+ "VIRTUAL_ENV": "/path/to/venv",
+ "UV_PROJECT_ENVIRONMENT": "/path/to/venv",
+ },
+ clear=True,
+)
+def test_uv_is_in_uv_project_uv_project_environment_match():
+ """Test is_in_uv_project returns True when UV_PROJECT_ENVIRONMENT equals VIRTUAL_ENV"""
mgr = UvPackageManager()
-
- result = await mgr._install("package1", upgrade=True)
-
- mock_run.assert_called_once_with(
- [
- "uv",
- "pip",
- "install",
- "--upgrade",
- "--compile",
- "package1",
- "-p",
- PY_EXE,
- ]
- )
- assert result is True
+ assert mgr.is_in_uv_project is True
-@patch("subprocess.run")
[email protected](UvPackageManager, "is_in_uv_project", True)
-async def test_uv_install_with_upgrade_in_project(mock_run: MagicMock):
- """Test UV install with upgrade uses add subcommand when in UV project"""
- mock_run.return_value = MagicMock(returncode=0)
+ "os.environ",
+ {
+ "VIRTUAL_ENV": "/path/to/venv",
+ "UV_PROJECT_ENVIRONMENT": "/different/path",
+ },
+ clear=True,
+)
+def test_uv_is_in_uv_project_uv_project_environment_mismatch():
+ """Test is_in_uv_project returns False when UV_PROJECT_ENVIRONMENT doesn't match VIRTUAL_ENV"""
mgr = UvPackageManager()
-
- result = await mgr._install("package1", upgrade=True)
-
- mock_run.assert_called_once_with(
- ["uv", "add", "--upgrade", "--compile", "package1", "-p", PY_EXE]
- )
- assert result is True
+ assert mgr.is_in_uv_project is False