Skip to content

Commit 1b01523

Browse files
authored
improve: respect current Python version for sandbox (#6924)
When --sandbox is used but no Python version is specified in the notebook file, respect the current Python version. This is useful when creating new notebooks with ``` uvx --python 3.13 marimo edit --sandbox my_notebook.py ``` which is itself useful since many packages do not yet work in 3.14.
1 parent 15addba commit 1b01523

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

marimo/_cli/sandbox.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import atexit
55
import os
6+
import platform
67
import signal
78
import subprocess
89
import sys
@@ -259,27 +260,26 @@ def construct_uv_flags(
259260
if len(additional_deps) > 0:
260261
uv_flags.extend(["--with", ",".join(additional_deps)])
261262

262-
# Add refresh
263263
if uv_needs_refresh:
264264
uv_flags.append("--refresh")
265265

266-
# Add Python version if specified
266+
# We use the specified Python version (if any), otherwise
267+
# the current Python version
267268
python_version = pyproject.python_version
268269
if python_version:
269270
uv_flags.extend(["--python", python_version])
271+
else:
272+
uv_flags.extend(["--python", platform.python_version()])
270273

271-
# Add index URL if specified
272274
index_url = pyproject.index_url
273275
if index_url:
274276
uv_flags.extend(["--index-url", index_url])
275277

276-
# Add extra-index-urls if specified
277278
extra_index_urls = pyproject.extra_index_urls
278279
if extra_index_urls:
279280
for url in extra_index_urls:
280281
uv_flags.extend(["--extra-index-url", url])
281282

282-
# Add index configs if specified
283283
index_configs = pyproject.index_configs
284284
if index_configs:
285285
for config in index_configs:

tests/_cli/test_sandbox.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,3 +483,28 @@ def test_should_run_in_sandbox_dangerous_sandbox(tmp_path: Path) -> None:
483483
name=str(dir_path),
484484
)
485485
assert result
486+
487+
488+
def test_construct_uv_cmd_without_python_version(tmp_path: Path) -> None:
489+
"""Test that current Python version is used when not specified."""
490+
import platform
491+
492+
# Create a script without requires-python
493+
script_path = tmp_path / "test.py"
494+
script_path.write_text(
495+
"""
496+
# /// script
497+
# dependencies = ["numpy"]
498+
# ///
499+
import marimo
500+
"""
501+
)
502+
uv_cmd = construct_uv_command(
503+
["edit", str(script_path)],
504+
str(script_path),
505+
additional_features=[],
506+
additional_deps=[],
507+
)
508+
assert "--python" in uv_cmd
509+
python_idx = uv_cmd.index("--python")
510+
assert uv_cmd[python_idx + 1] == platform.python_version()

0 commit comments

Comments
 (0)