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.

Fix missing `!pip` transform in notebook converter (#5841)

Trevor Manz36be39b10c53
diff --git a/marimo/_convert/ipynb.py b/marimo/_convert/ipynb.py
index c17dd544bc..660397f841 100644
--- a/marimo/_convert/ipynb.py
+++ b/marimo/_convert/ipynb.py
@@ -718,6 +718,7 @@ def _transform_sources(
source_transforms: list[Transform] = [
transform_strip_whitespace,
transform_magic_commands,
+ transform_exclamation_mark,
transform_remove_duplicate_imports,
transform_fixup_multiple_definitions,
transform_duplicate_definitions,
diff --git a/scripts/generate_ipynb_fixtures.py b/scripts/generate_ipynb_fixtures.py
index 6b0955037e..20f0797924 100644
--- a/scripts/generate_ipynb_fixtures.py
+++ b/scripts/generate_ipynb_fixtures.py
@@ -156,6 +156,17 @@ for i_1 in range(X_1.shape[0]):
],
)
+ create_notebook_fixture(
+ "pip_commands",
+ [
+ "!pip install transformers",
+ "!pip install pandas numpy matplotlib",
+ "# Mixed cell with pip and other commands\n!pip install scikit-learn\nimport numpy as np\n!pip install seaborn",
+ "# Non-pip exclamation commands should remain unchanged\n!ls -la\n!echo 'Hello World'",
+ "# Magic pip command should also be handled\n%pip install requests",
+ ],
+ )
+
if __name__ == "__main__":
main()
diff --git a/tests/_convert/ipynb_data/pip_commands.ipynb b/tests/_convert/ipynb_data/pip_commands.ipynb
new file mode 100644
index 0000000000..63f02c0897
--- /dev/null
+++ b/tests/_convert/ipynb_data/pip_commands.ipynb
@@ -0,0 +1,63 @@
+{
+ "cells": [
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "0",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "!pip install transformers"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "1",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "!pip install pandas numpy matplotlib"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "2",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# Mixed cell with pip and other commands\n",
+ "!pip install scikit-learn\n",
+ "import numpy as np\n",
+ "!pip install seaborn"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "3",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# Non-pip exclamation commands should remain unchanged\n",
+ "!ls -la\n",
+ "!echo 'Hello World'"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "4",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# Magic pip command should also be handled\n",
+ "%pip install requests"
+ ]
+ }
+ ],
+ "metadata": {},
+ "nbformat": 4,
+ "nbformat_minor": 5
+}
\ No newline at end of file
diff --git a/tests/_convert/snapshots/convert_pip_commands.py.txt b/tests/_convert/snapshots/convert_pip_commands.py.txt
new file mode 100644
index 0000000000..d4a568552f
--- /dev/null
+++ b/tests/_convert/snapshots/convert_pip_commands.py.txt
@@ -0,0 +1,45 @@
+import marimo
+
+app = marimo.App()
+
+
+def _():
+ # (use marimo's built-in package management features instead) !pip install transformers
+ return
+
+
+def _():
+ # (use marimo's built-in package management features instead) !pip install pandas numpy matplotlib
+ return
+
+
+def _():
+ # Mixed cell with pip and other commands
+ # (use marimo's built-in package management features instead) !pip install scikit-learn
+ import numpy as np
+ # (use marimo's built-in package management features instead) !pip install seaborn
+ return
+
+
+app._unparsable_cell(
+ r"""
+ # Non-pip exclamation commands should remain unchanged
+ !ls -la
+ !echo 'Hello World'
+ """,
+ name="_"
+)
+
+
+def _():
+ # Magic pip command should also be handled
+ # '%pip install requests' command supported automatically in marimo
+ return
+
+
+if __name__ == "__main__":
+ app.run()