optional_import

function of dascore.utils.misc source

optional_import(
    package_name: str ,
)-> ‘ModuleType’

Import a module and return the module object if installed, else raise error.

Parameters

Parameter Description
package_name The name of the package which may or may not be installed. Can
also be sub-packages/modules (eg dascore.core).

Raises

MissingOptionalDependency if the package is not installed.

Examples

from dascore.utils.misc import optional_import
from dascore.exceptions import MissingOptionalDependencyError
# import a module (this is the same as import dascore as dc)
dc = optional_import('dascore')
try:
    optional_import('boblib5')  # doesn't exist so this raises
except MissingOptionalDependencyError:
    pass