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: install packages not modules with micropip eager install (#2634)
Myles Scolnicka1ea5b08c0d9
diff --git a/frontend/src/core/wasm/worker/bootstrap.ts b/frontend/src/core/wasm/worker/bootstrap.tsindex ec17eddd60..ea8910c117 100644--- a/frontend/src/core/wasm/worker/bootstrap.ts+++ b/frontend/src/core/wasm/worker/bootstrap.ts@@ -160,7 +160,7 @@ export class DefaultWasmController implements WasmController {const span = t.startSpan("startSession.runPython");const nbFilename = filename || WasmFileSystem.NOTEBOOK_FILENAME;- const [bridge, init, imports] = this.requirePyodide.runPython(+ const [bridge, init, packages] = this.requirePyodide.runPython(`print("[py] Starting marimo...")import asyncio@@ -182,28 +182,27 @@ export class DefaultWasmController implements WasmController {instantiate(session)asyncio.create_task(session.start())- # Load the imports- import pyodide.code+ # Find the packages to installwith open("${nbFilename}", "r") as f:- imports = pyodide.code.find_imports(f.read())+ packages = session.find_packages(f.read())- bridge, init, imports`,+ bridge, init, packages`,);span.end();- const moduleImports = new Set<string>(imports.toJs());+ const foundPackages = new Set<string>(packages.toJs());// Fire and forgot load packages and instantiation// We don't want to wait for this to finish,// as it blocks the initial code from being shown.- void this.loadNotebookDeps(code, moduleImports).then(() => {+ void this.loadNotebookDeps(code, foundPackages).then(() => {return init(userConfig.runtime.auto_instantiate);});return bridge;}- private async loadNotebookDeps(code: string, foundImports: Set<string>) {+ private async loadNotebookDeps(code: string, foundPackages: Set<string>) {const pyodide = this.requirePyodide;if (code.includes("mo.sql")) {@@ -217,14 +216,14 @@ export class DefaultWasmController implements WasmController {// 2. pyodide-http, a patch to make basic http requests work in pyodide//// These packages are included with Pyodide, which is why we don't add them- // to `foundImports`:+ // to `foundPackages`:// https://pyodide.org/en/stable/usage/packages-in-pyodide.htmlcode = `import docutils\n${code}`;code = `import pygments\n${code}`;code = `import jedi\n${code}`;code = `import pyodide_http\n${code}`;- const imports = [...foundImports];+ const imports = [...foundPackages];// Load from pyodidelet loadSpan = t.startSpan("pyodide.loadPackage");diff --git a/marimo/_pyodide/pyodide_session.py b/marimo/_pyodide/pyodide_session.pyindex 253730d6a7..7c0238befd 100644--- a/marimo/_pyodide/pyodide_session.py+++ b/marimo/_pyodide/pyodide_session.py@@ -22,6 +22,9 @@ from marimo._runtime import handlers, patches, requestsfrom marimo._runtime.context.kernel_context import initialize_kernel_contextfrom marimo._runtime.input_override import input_overridefrom marimo._runtime.marimo_pdb import MarimoPdb+from marimo._runtime.packages.pypi_package_manager import (+ MicropipPackageManager,+)from marimo._runtime.requests import (AppMetadata,CodeCompletionRequest,@@ -148,6 +151,20 @@ class PyodideSession:def put_input(self, text: str) -> None:self._queue_manager.input_queue.put_nowait(text)+ def find_packages(self, code: str) -> list[str]:+ """+ Find the packages in the code based on the imports,+ and mapping from module names to package names.+ """+ import pyodide.code # type: ignore++ imports: list[str] = pyodide.code.find_imports(code) # type: ignore+ if not isinstance(imports, list):+ return []++ package_manager = MicropipPackageManager()+ return [package_manager.module_to_package(im) for im in imports]+class PyodideBridge:def __init__(diff --git a/marimo/_runtime/packages/module_name_to_conda_name.py b/marimo/_runtime/packages/module_name_to_conda_name.pyindex 8e241b7986..1bc77385fe 100644--- a/marimo/_runtime/packages/module_name_to_conda_name.py+++ b/marimo/_runtime/packages/module_name_to_conda_name.py@@ -11,4 +11,5 @@ def module_name_to_conda_name() -> dict[str, str]:# that known to be incorrect; this doesn't handle channels ...mapping = module_name_to_pypi_name()mapping["cv2"] = "opencv"+ mapping["ibis"] = "ibis-duckdb"return mapping