modules#

This module contains methods concerning modules. As of now, only a single method for import backed with installation is implemented.

fetch_module#

pocketwelt.modules.fetch_module(package: str, alias: str | None = None) None#

Try importing a package and install it if it’s not available.

Parameters:
  • package (str) – Name of the package to import and potentially install.

  • alias (str) – Alias for module used in code (e.g. np is an alias for numpy).

Raises:

ImportError – If the package cannot be imported after installation attempt.

Note

This function modifies the global namespace by adding the imported module.

Example

>>> fetch_module('numpy')
# If numpy is not installed, it will be installed and then imported.
# If numpy is already installed, it will just be imported.