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: allow more package installation rules in the pkg management UI (#2448)

Myles Scolnick93620f29b847
diff --git a/marimo/_runtime/packages/utils.py b/marimo/_runtime/packages/utils.py
index 3bfd7ce98e..9c7686894b 100644
--- a/marimo/_runtime/packages/utils.py
+++ b/marimo/_runtime/packages/utils.py
@@ -40,4 +40,51 @@ def append_version(pkg_name: str, version: Optional[str]) -> str:
def split_packages(package: str) -> List[str]:
- return [pkg.strip() for pkg in package.split()]
+ """
+ Splits a package string into a list of packages.
+
+ This can handle editable packages (i.e. local directories)
+
+ e.g.
+ "package1 package2" -> ["package1", "package2"]
+ "package1==1.0.0 package2==2.0.0" -> ["package1==1.0.0", "package2==2.0.0"]
+ "package1 -e /path/to/package1" -> ["package1 -e /path/to/package1"]
+ "package1 --editable /path/to/package1" -> ["package1 --editable /path/to/package1"]
+ "package1 -e /path/to/package1 package2" -> ["package1 -e /path/to/package1", "package2"]
+ "package1 @ /path/to/package1" -> ["package1 @ /path/to/package1"]
+ "foo==1.0; python_version>'3.6' bar==2.0; sys_platform=='win32'" -> ["foo==1.0; python_version>'3.6'", "bar==2.0; sys_platform=='win32'"]
+ """ # noqa: E501
+ packages: List[str] = []
+ current_package: List[str] = []
+ in_environment_marker = False
+
+ for part in package.split():
+ if part in ["-e", "--editable", "@"]:
+ current_package.append(part)
+ elif current_package and current_package[-1] in [
+ "-e",
+ "--editable",
+ "@",
+ ]:
+ current_package.append(part)
+ elif part.endswith(";"):
+ if current_package:
+ packages.append(" ".join(current_package))
+ current_package = []
+ in_environment_marker = True
+ current_package.append(part)
+ elif in_environment_marker:
+ current_package.append(part)
+ if part.endswith("'") or part.endswith('"'):
+ in_environment_marker = False
+ packages.append(" ".join(current_package))
+ current_package = []
+ else:
+ if current_package:
+ packages.append(" ".join(current_package))
+ current_package = [part]
+
+ if current_package:
+ packages.append(" ".join(current_package))
+
+ return [pkg.strip() for pkg in packages]
diff --git a/tests/_runtime/packages/test_package_utils.py b/tests/_runtime/packages/test_package_utils.py
index 762838345c..f3487de5ee 100644
--- a/tests/_runtime/packages/test_package_utils.py
+++ b/tests/_runtime/packages/test_package_utils.py
@@ -4,7 +4,11 @@ import sys
import pytest
-from marimo._runtime.packages.utils import append_version, is_python_isolated
+from marimo._runtime.packages.utils import (
+ append_version,
+ is_python_isolated,
+ split_packages,
+)
# TODO(akshayka): virtualenv not activating on windows CI
@@ -20,3 +24,94 @@ def test_append_version() -> None:
assert append_version("foo", None) == "foo"
assert append_version("foo", "") == "foo"
assert append_version("foo", "latest") == "foo"
+
+
+def test_split_packages() -> None:
+ assert split_packages("foo") == ["foo"]
+ assert split_packages("foo bar") == ["foo", "bar"]
+ assert split_packages("foo==1.2.3 bar==4.5.6") == [
+ "foo==1.2.3",
+ "bar==4.5.6",
+ ]
+ assert split_packages("foo -e /path/to/foo") == ["foo -e /path/to/foo"]
+ assert split_packages("foo @ /path/to/foo") == ["foo @ /path/to/foo"]
+ assert split_packages("foo -e /path/to/foo bar") == [
+ "foo -e /path/to/foo",
+ "bar",
+ ]
+ assert split_packages("foo -e /path/to/foo bar -e /path/to/bar") == [
+ "foo -e /path/to/foo",
+ "bar -e /path/to/bar",
+ ]
+ assert split_packages("foo @ /path/to/foo bar @ /path/to/bar") == [
+ "foo @ /path/to/foo",
+ "bar @ /path/to/bar",
+ ]
+ assert split_packages("foo -e /path/to/foo bar @ /path/to/bar") == [
+ "foo -e /path/to/foo",
+ "bar @ /path/to/bar",
+ ]
+ assert split_packages("foo @ /path/to/foo bar -e /path/to/bar") == [
+ "foo @ /path/to/foo",
+ "bar -e /path/to/bar",
+ ]
+ assert split_packages("foo[extra1,extra2]==1.2.3") == [
+ "foo[extra1,extra2]==1.2.3"
+ ]
+ assert split_packages("foo[extra1,extra2]==1.2.3 bar[extra3]==4.5.6") == [
+ "foo[extra1,extra2]==1.2.3",
+ "bar[extra3]==4.5.6",
+ ]
+ assert split_packages("foo>=1.0,<2.0 bar>3.0") == [
+ "foo>=1.0,<2.0",
+ "bar>3.0",
+ ]
+ assert split_packages("foo~=1.0 bar!=2.0") == ["foo~=1.0", "bar!=2.0"]
+ assert split_packages("foo==1.0+local.version bar===1.0") == [
+ "foo==1.0+local.version",
+ "bar===1.0",
+ ]
+ assert split_packages(
+ "foo @ git+https://github.com/user/repo.git@main"
+ ) == ["foo @ git+https://github.com/user/repo.git@main"]
+ assert split_packages(
+ "foo @ git+https://github.com/user/repo.git@main bar==1.0"
+ ) == [
+ "foo @ git+https://github.com/user/repo.git@main",
+ "bar==1.0",
+ ]
+ assert split_packages(
+ "foo[extra1,extra2] @ git+https://github.com/user/repo.git@main"
+ ) == ["foo[extra1,extra2] @ git+https://github.com/user/repo.git@main"]
+ assert split_packages("foo-bar==1.0 baz_qux==2.0") == [
+ "foo-bar==1.0",
+ "baz_qux==2.0",
+ ]
+ assert split_packages("foo.bar==1.0 baz-qux==2.0") == [
+ "foo.bar==1.0",
+ "baz-qux==2.0",
+ ]
+ assert split_packages(
+ "foo[extra1,extra2] @ file:///path/to/foo bar @ file:///path/to/bar"
+ ) == [
+ "foo[extra1,extra2] @ file:///path/to/foo",
+ "bar @ file:///path/to/bar",
+ ]
+ assert split_packages(
+ "foo @ https://example.com/foo.tar.gz bar @ https://example.com/bar.whl"
+ ) == [
+ "foo @ https://example.com/foo.tar.gz",
+ "bar @ https://example.com/bar.whl",
+ ]
+ assert split_packages(
+ "foo==1.0; python_version>'3.6' bar==2.0; sys_platform=='win32'"
+ ) == [
+ "foo==1.0; python_version>'3.6'",
+ "bar==2.0; sys_platform=='win32'",
+ ]
+ assert split_packages(
+ "foo==1.0; python_version=='3.7.*' bar==2.0; implementation_name=='cpython'" # noqa: E501
+ ) == [
+ "foo==1.0; python_version=='3.7.*'",
+ "bar==2.0; implementation_name=='cpython'",
+ ]