Skip to content

MicropipInstaller

Base classes

Name Children Inherits
PackageInstaller
anyenv.package_install.base
Base class for package installer implementations.

â‹” Inheritance diagram

graph TD
  4105179838["micropip_install.MicropipInstaller"]
  2807983507["base.PackageInstaller"]
  925645236["abc.ABC"]
  2751680898["builtins.object"]
  2807983507 --> 4105179838
  925645236 --> 2807983507
  2751680898 --> 925645236

🛈 DocStrings

Bases: PackageInstaller

Package installer using micropip in Pyodide environments.

Source code in src/anyenv/package_install/micropip_install.py
class MicropipInstaller(PackageInstaller):
    """Package installer using micropip in Pyodide environments."""

    def __init__(self) -> None:
        """Initialize the micropip installer.

        Raises:
            ImportError: If micropip is not available.
        """
        try:
            import micropip  # type: ignore

            self._micropip = micropip
        except ImportError:
            msg = "micropip is not available. This installer only works in Pyodide environments."
            raise ImportError(msg)  # noqa: B904

    async def install(
        self,
        package_name: str,
        version: str | None = None,
        upgrade: bool = False,
    ) -> None:
        """Install a package using micropip.

        Args:
            package_name: Name of the package to install.
            version: Optional version specifier.
            upgrade: Whether to upgrade an existing package.
        """
        # Check if the package is already installed
        try:
            importlib.metadata.distribution(package_name)
            if upgrade:
                # Uninstall the package first if upgrading
                self._micropip.uninstall(package_name)
            else:
                # Package already installed and not upgrading, so nothing to do
                return
        except importlib.metadata.PackageNotFoundError:
            pass  # Package not installed, continue with installation

        # Format the package name with version if specified
        if version:
            if version[0] not in ("=", "<", ">", "!"):
                version = "==" + version
            package_spec = f"{package_name}{version}"
        else:
            package_spec = package_name

        # Install the package
        print(f"Installing {package_spec} with micropip...")
        await self._micropip.install(package_spec)
        print(f"Successfully installed {package_spec}")

__init__

__init__() -> None

Initialize the micropip installer.

Raises:

Type Description
ImportError

If micropip is not available.

Source code in src/anyenv/package_install/micropip_install.py
def __init__(self) -> None:
    """Initialize the micropip installer.

    Raises:
        ImportError: If micropip is not available.
    """
    try:
        import micropip  # type: ignore

        self._micropip = micropip
    except ImportError:
        msg = "micropip is not available. This installer only works in Pyodide environments."
        raise ImportError(msg)  # noqa: B904

install async

install(package_name: str, version: str | None = None, upgrade: bool = False) -> None

Install a package using micropip.

Parameters:

Name Type Description Default
package_name str

Name of the package to install.

required
version str | None

Optional version specifier.

None
upgrade bool

Whether to upgrade an existing package.

False
Source code in src/anyenv/package_install/micropip_install.py
async def install(
    self,
    package_name: str,
    version: str | None = None,
    upgrade: bool = False,
) -> None:
    """Install a package using micropip.

    Args:
        package_name: Name of the package to install.
        version: Optional version specifier.
        upgrade: Whether to upgrade an existing package.
    """
    # Check if the package is already installed
    try:
        importlib.metadata.distribution(package_name)
        if upgrade:
            # Uninstall the package first if upgrading
            self._micropip.uninstall(package_name)
        else:
            # Package already installed and not upgrading, so nothing to do
            return
    except importlib.metadata.PackageNotFoundError:
        pass  # Package not installed, continue with installation

    # Format the package name with version if specified
    if version:
        if version[0] not in ("=", "<", ">", "!"):
            version = "==" + version
        package_spec = f"{package_name}{version}"
    else:
        package_spec = package_name

    # Install the package
    print(f"Installing {package_spec} with micropip...")
    await self._micropip.install(package_spec)
    print(f"Successfully installed {package_spec}")

:fa-brands-github: Show source on GitHub