Import

Sometimes you just have a fairly nested module hierarchy, and you want to import everything. Don’t worry, Satella’s got you covered. Just use this function

satella.imports.import_from(path: List[str], package_prefix: str, all_: List[str], locals_: Dict[str, Any], recursive: bool = True, fail_on_attributerror: bool = True, create_all: bool = True, skip_single_underscores: bool = True, skip_not_having_all: bool = False) None

Import everything from a given module. Append these module’s all to.

This will examine __all__ of given module (if it has any, else it will just import everything from it, which is probably a bad practice and will heavily pollute the namespace.

As a side effect, this will equip all of your packages with __all__.

Parameters:
  • path – module’s __path__

  • package_prefix – package prefix to import from. Use __name__

  • all – module’s __all__ to append to

  • recursive – whether to import packages as well

  • fail_on_attributerror – whether to fail if a module reports something in their __all__ that is physically not there (ie. getattr() raised AttributeError

  • locals – module’s locals, obtain them by calling locals() in importing module’s context

  • create_all – whether to create artificial __all__’s for modules that don’t have them

  • skip_single_underscores – whether to refrain from importing things that are preceded with a single underscore. Pertains to modules, as well as items

  • skip_not_having_all – skip module’s not having an __all__ entry

Raises:

AttributeError – module’s __all__ contained entry that was not in this module

An example use would be your module’s __init__.py containing following code:

from satella.imports import import_from

__all__ = []

import_from(__path__, __name__, __all__, locals())

In this case, everything will be accessible from this module. This will examine the __all__ of your submodules, and dir() it if __all__’s not available. Note that lack of availability of __all__ will emit a log warning.

And sometimes you just need a particular class loaded, identified by a module path and class name, eg. subprocess.Popen. You can use the following function to get it:

satella.imports.import_class(path: str) type

Import a class identified with given module path and class name

Parameters:

path – path, eg. subprocess.Popen

Returns:

imported class