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: invalidate importlib cache after requirecheck fails (#5328)
Myles Scolnick2d0adee9de87
diff --git a/marimo/_dependencies/dependencies.py b/marimo/_dependencies/dependencies.pyindex 92a36fa33e..8b61776ee8 100644--- a/marimo/_dependencies/dependencies.py+++ b/marimo/_dependencies/dependencies.py@@ -68,6 +68,26 @@ class Dependency:if not self.has():message = f"{self.pkg} is required {why}."sys.stderr.write(message + "\n\n")++ # NOTE:+ # If this package is a subpackage (e.g. google.genai),+ # then we need to invalidate the importlib cache, otherwise Python+ # will still not be able to import it.+ #+ # This only happens in the marimo editor, and not the kernel.+ # And only happens for subpackages.+ # .require() is usually followed by an installation, so this is a fine+ # place to invalidate the cache.+ if "." in self.pkg:+ import importlib++ importlib.invalidate_caches()+ # https://docs.python.org/3/library/importlib.html#importlib.invalidate_caches+ # > This function should be called if any modules are created/installed while your+ # > program is running to guarantee all finders will notice the new module's existence.+ if self.pkg in sys.modules:+ del sys.modules[self.pkg]+# Including the `name` helps with auto-installationsraise ModuleNotFoundError(message, name=self.pkg) from Nonediff --git a/tests/_dependencies/test_dependencies.py b/tests/_dependencies/test_dependencies.pyindex c9340a3268..775d510a60 100644--- a/tests/_dependencies/test_dependencies.py+++ b/tests/_dependencies/test_dependencies.py@@ -1,5 +1,8 @@from __future__ import annotations+import sys+from unittest.mock import patch+import pytestfrom marimo._dependencies.dependencies import (@@ -42,6 +45,44 @@ def test_without_dependencies() -> None:assert "for testing" in str(excinfo.value)+def test_subpackage_cache_invalidation() -> None:+ """Test that subpackage dependencies properly invalidate importlib cache."""+ # Create a dependency for a missing subpackage+ missing_subpackage = Dependency("google.missing_package")++ with patch("importlib.invalidate_caches") as mock_invalidate:+ with pytest.raises(ModuleNotFoundError) as excinfo:+ missing_subpackage.require("for testing subpackage handling")++ # Verify the behavior+ assert excinfo.value.name == "google.missing_package"+ assert "for testing subpackage handling" in str(excinfo.value)++ # Verify that invalidate_caches was called for subpackage+ mock_invalidate.assert_called_once()++ # Verify that the module was removed from sys.modules+ assert "google.missing_package" not in sys.modules+++def test_regular_package_no_cache_invalidation() -> None:+ """Test that regular packages (no dot) don't trigger cache invalidation."""++ # Create a dependency for a missing regular package+ missing_package = Dependency("missing_regular_package")++ with patch("importlib.invalidate_caches") as mock_invalidate:+ with pytest.raises(ModuleNotFoundError) as excinfo:+ missing_package.require("for testing regular package")++ # Verify the behavior+ assert excinfo.value.name == "missing_regular_package"+ assert "for testing regular package" in str(excinfo.value)++ # Verify that invalidate_caches was NOT called for regular package+ mock_invalidate.assert_not_called()++@pytest.mark.skipif(not DependencyManager.altair.has(),reason="altair is not installed",