importbyname(module=None, variable=None, path=None, namespace=None, lazy=False, overwrite=True, die=True, verbose=True, **kwargs)[source]#

Import modules by name.

See for a proposal for incorporating something similar into Python by default.

  • module (str) – name of the module to import

  • variable (str) – the name of the variable to assign the module to (by default, the module’s name)

  • path (str/path) – optionally load from path instead of by name

  • namespace (dict) – the namespace to load the modules into (by default, globals)

  • lazy (bool) – whether to create a LazyModule object instead of load the actual module

  • overwrite (bool) – whether to allow overwriting an existing variable (by default, yes)

  • die (bool) – whether to raise an exception if encountered

  • verbose (bool) – whether to print a warning if an module can’t be imported

  • **kwargs (dict) – additional variable:modules pairs to import (see examples below)


np = sc.importbyname('numpy') # Standard usage
sc.importbyname(pd='pandas', np='numpy') # Use dictionary syntax to assign to namespace
plt = sc.importbyname(plt='matplotlib.pyplot', lazy=True) # Won't actually import until e.g. pl.figure() is called
mymod = sc.importbyname(path='/path/to/mymod') # Import by path rather than name

See also sc.importbypath().

New in version 2.1.0: “verbose” argument
New in version 3.0.0: “path” argument