Routines to support optional packages
optional_package(name[, trip_msg]) | Return package-like thing and module setup for package name |
Return package-like thing and module setup for package name
Parameters: | name : str
trip_msg : None or str
|
---|---|
Returns: | pkg_like : module or TripWire instance
have_pkg : bool
module_setup : function
|
Examples
Typical use would be something like this at the top of a module using an optional package:
>>> from nibabel.optpkg import optional_package
>>> pkg, have_pkg, setup_module = optional_package('not_a_package')
Of course in this case the package doesn’t exist, and so, in the module:
>>> have_pkg
False
and
>>> pkg.some_function()
Traceback (most recent call last):
...
TripWireError: We need package not_a_package for these functions, but ``import not_a_package`` raised an ImportError
Traceback (most recent call last):
...
TripWireError: We need package not_a_package for these functions, but ``import not_a_package`` raised an ImportError
If the module does exist - we get the module
>>> pkg, _, _ = optional_package('os')
>>> hasattr(pkg, 'path')
True
Or a submodule if that’s what we asked for
>>> subpkg, _, _ = optional_package('os.path')
>>> hasattr(subpkg, 'dirname')
True