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: use python -m pip instead of pip --python for PipPackageManager (#8840)
Dylan Madisetti34db4f2f894f
diff --git a/marimo/_runtime/packages/pypi_package_manager.py b/marimo/_runtime/packages/pypi_package_manager.pyindex d58749bf78..b064dc32ed 100644--- a/marimo/_runtime/packages/pypi_package_manager.py+++ b/marimo/_runtime/packages/pypi_package_manager.py@@ -11,6 +11,7 @@ from pathlib import Pathfrom typing import Optionalfrom marimo import _loggers+from marimo._dependencies.dependencies import DependencyManagerfrom marimo._runtime.packages.module_name_to_pypi_name import (module_name_to_pypi_name,)@@ -121,15 +122,47 @@ class PipPackageManager(PypiPackageManager):name = "pip"docs_url = "https://pip.pypa.io/"+ def is_manager_installed(self) -> bool:+ """Check if pip is available.++ On some platforms (e.g. macOS with pip-installed Python from python.org),+ only `pip3` is available in PATH, not `pip`. We first try the method we+ actually use to invoke pip (`python -m pip`), then fall back to a PATH+ check for compatibility.+ """+ # Primary check: use the same invocation method we actually use+ # (python -m pip) rather than relying on PATH pip, which could be+ # a different Python's pip than self._python_exe+ try:+ proc = subprocess.run(+ [self._python_exe, "-m", "pip", "--version"],+ capture_output=True,+ text=True,+ timeout=10,+ )+ if proc.returncode == 0:+ return True+ except (subprocess.TimeoutExpired, FileNotFoundError, OSError):+ pass+ # Fallback: check if pip is on PATH (handles cases where pip is+ # available but python -m pip is not desired/needed)+ if DependencyManager.which(self.name):+ return True+ LOGGER.error(+ f"{self.name} is not available. "+ f"Check out the docs for installation instructions: {self.docs_url}"+ )+ return False+def install_command(self, package: str, *, upgrade: bool, group: Optional[str] = None) -> list[str]:# The `group` parameter is accepted for interface compatibility, but is ignored.del groupreturn [- "pip",- "--python",self._python_exe,+ "-m",+ "pip","install",*(["--upgrade"] if upgrade else []),*split_packages(package),@@ -143,9 +176,9 @@ class PipPackageManager(PypiPackageManager):LOGGER.info(f"Uninstalling {package} with pip")return await self.run([- "pip",- "--python",self._python_exe,+ "-m",+ "pip","uninstall","-y",*split_packages(package),@@ -155,9 +188,9 @@ class PipPackageManager(PypiPackageManager):def list_packages(self) -> list[PackageDescription]:cmd = [- "pip",- "--python",self._python_exe,+ "-m",+ "pip","list","--format=json",]diff --git a/tests/_runtime/packages/test_pypi_package_manager.py b/tests/_runtime/packages/test_pypi_package_manager.pyindex 012be903f7..b388c0879b 100644--- a/tests/_runtime/packages/test_pypi_package_manager.py+++ b/tests/_runtime/packages/test_pypi_package_manager.py@@ -63,6 +63,57 @@ async def test_failed_install_returns_false() -> None:assert not await mgr.install("asdfasdfasdfasdfqwerty", version=None)+@patch("subprocess.run")+def test_pip_is_manager_installed_primary_check_success(mock_run: MagicMock):+ """Primary python -m pip check succeeds → returns True."""+ mock_run.return_value = MagicMock(returncode=0)+ mgr = PipPackageManager()+ assert mgr.is_manager_installed() is True+ mock_run.assert_called_once()+ call_args = mock_run.call_args[0][0]+ assert call_args == [PY_EXE, "-m", "pip", "--version"]+++@patch("subprocess.run")+@patch("marimo._dependencies.dependencies.DependencyManager.which")+def test_pip_is_manager_installed_fallback_check(+ mock_which: MagicMock, mock_run: MagicMock+):+ """Primary python -m pip fails but pip is on PATH → returns True (fallback)."""+ mock_run.return_value = MagicMock(returncode=1)+ mock_which.return_value = True+ mgr = PipPackageManager()+ assert mgr.is_manager_installed() is True+ # Primary check was called and failed+ mock_run.assert_called_once()+ # Fallback which() was also called+ mock_which.assert_called_once_with("pip")+++@patch("subprocess.run")+@patch("marimo._dependencies.dependencies.DependencyManager.which")+def test_pip_is_manager_installed_permission_error(+ mock_which: MagicMock, mock_run: MagicMock+):+ """python -m pip raises OSError (e.g. PermissionError) → returns False."""+ mock_run.side_effect = PermissionError("python not executable")+ mock_which.return_value = False+ mgr = PipPackageManager()+ assert mgr.is_manager_installed() is False+++@patch("subprocess.run")+@patch("marimo._dependencies.dependencies.DependencyManager.which")+def test_pip_is_manager_installed_both_fail(+ mock_which: MagicMock, mock_run: MagicMock+):+ """Both python -m pip and which("pip") fail → returns False."""+ mock_run.return_value = MagicMock(returncode=1)+ mock_which.return_value = False+ mgr = PipPackageManager()+ assert mgr.is_manager_installed() is False++manager = PipPackageManager()@@ -76,7 +127,7 @@ async def test_install(mock_run: MagicMock):)mock_run.assert_called_once_with(- ["pip", "--python", PY_EXE, "install", "package1", "package2"],+ [PY_EXE, "-m", "pip", "install", "package1", "package2"],)assert result is True@@ -101,9 +152,9 @@ async def test_uninstall(mock_run: MagicMock):mock_run.assert_called_once_with([- "pip",- "--python",PY_EXE,+ "-m",+ "pip","uninstall","-y","package1",@@ -127,7 +178,7 @@ def test_list_packages(mock_run: MagicMock):packages = manager.list_packages()mock_run.assert_called_once_with(- ["pip", "--python", PY_EXE, "list", "--format=json"],+ [PY_EXE, "-m", "pip", "list", "--format=json"],capture_output=True,text=True,encoding="utf-8",