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: don't crash kernel when package managers are missing (#1009)
Akshay Agrawal5ca9737aa6fb
diff --git a/frontend/src/components/editor/package-alert.tsx b/frontend/src/components/editor/package-alert.tsxindex 6ad9bb4544..034f9996fc 100644--- a/frontend/src/components/editor/package-alert.tsx+++ b/frontend/src/components/editor/package-alert.tsx@@ -18,8 +18,6 @@ import { sendInstallMissingPackages } from "@/core/network/requests";import {useAlerts,useAlertActions,- MissingPackageAlert,- InstallingPackageAlert,isMissingPackageAlert,isInstallingPackageAlert,} from "@/core/alerts/state";@@ -41,7 +39,7 @@ import { isPyodide } from "@/core/pyodide/utils";export const PackageAlert: React.FC = (props) => {const { packageAlert } = useAlerts();- const { addPackageAlert, clearPackageAlert } = useAlertActions();+ const { clearPackageAlert } = useAlertActions();const [userConfig] = useUserConfig();if (packageAlert === null) {@@ -88,9 +86,8 @@ export const PackageAlert: React.FC = (props) => {{packageAlert.isolated ? (<><InstallPackagesButton- packages={packageAlert.packages}manager={userConfig.package_management.manager}- addPackageAlert={addPackageAlert}+ clearPackageAlert={() => clearPackageAlert(packageAlert.id)}/>{isPyodide() ? null : (@@ -240,40 +237,27 @@ const ProgressIcon = ({};async function installPackages(- packages: string[],manager: "pip" | "uv" | "rye",- addPackageAlert: (- alert: MissingPackageAlert | InstallingPackageAlert,- ) => void,+ clearPackageAlert: () => void,) {- const packageStatus = Object.fromEntries(- packages.map((pkg) => [pkg, "queued"]),- ) as PackageInstallationStatus;- addPackageAlert({- kind: "installing",- packages: packageStatus,- });+ clearPackageAlert();RuntimeState.INSTANCE.registerRunStart();await sendInstallMissingPackages({ manager: manager });}const InstallPackagesButton = ({- packages,manager,- addPackageAlert,+ clearPackageAlert,}: {- packages: string[];manager: "pip" | "uv" | "rye";- addPackageAlert: (- alert: MissingPackageAlert | InstallingPackageAlert,- ) => void;+ clearPackageAlert: () => void;}) => {return (<Buttonvariant="outline"data-testid="install-packages-button"size="sm"- onClick={() => installPackages(packages, manager, addPackageAlert)}+ onClick={() => installPackages(manager, clearPackageAlert)}><DownloadCloudIcon className="w-4 h-4 mr-2" /><span className="font-semibold">Install</span>diff --git a/marimo/_runtime/packages/package_manager.py b/marimo/_runtime/packages/package_manager.pyindex 5c402fa5fa..ed04fb7743 100644--- a/marimo/_runtime/packages/package_manager.py+++ b/marimo/_runtime/packages/package_manager.py@@ -2,6 +2,7 @@from __future__ import annotationsimport abc+import shutilclass PackageManager(abc.ABC):@@ -19,6 +20,10 @@ class PackageManager(abc.ABC):"""Canonicalizes a package name to a module name."""...+ def is_manager_installed(self) -> bool:+ """Is the package manager is installed on the user machine?"""+ return shutil.which(self.name) is not None+@abc.abstractmethodasync def install(self, package: str) -> bool:"""Attempt to install a package that makes this module available.diff --git a/marimo/_runtime/packages/pypi_package_manager.py b/marimo/_runtime/packages/pypi_package_manager.pyindex 4dcdd70eef..95767445d2 100644--- a/marimo/_runtime/packages/pypi_package_manager.py+++ b/marimo/_runtime/packages/pypi_package_manager.py@@ -63,6 +63,9 @@ class PipPackageManager(PypiPackageManager):class MicropipPackageManager(PypiPackageManager):name = "micropip"+ def is_manager_installed(self) -> bool:+ return is_pyodide()+async def install(self, package: str) -> bool:assert is_pyodide()import micropip # type: ignorediff --git a/marimo/_runtime/runtime.py b/marimo/_runtime/runtime.pyindex 3f519251b7..f308e9138a 100644--- a/marimo/_runtime/runtime.py+++ b/marimo/_runtime/runtime.py@@ -31,6 +31,7 @@ from marimo._messaging.errors import (UnknownError,)from marimo._messaging.ops import (+ Alert,CellOp,CompletedRun,FunctionCallResult,@@ -1178,6 +1179,16 @@ class Kernel:# Swap out the package managerself.package_manager = create_package_manager(request.manager)+ if not self.package_manager.is_manager_installed():+ Alert(+ title="Package manager not installed",+ description=(+ f"{request.manager} is not available on your machine."+ ),+ variant="danger",+ ).broadcast()+ return+# Package manager operates on module namesmissing_modules = list(sorted(self.module_registry.missing_modules()))