Optional imports¶
Peek at JAX / Torch / TensorFlow (and any other module) without importing them. Used so extras stay unloaded until the caller imports them.
anytensor.optional
¶
Peek at optional libraries that are already imported — never import them.
AnyTensor does not import JAX / Torch / TensorFlow unless the caller already
did. Use :func:module_if_loaded instead of try: import … when a missing extra
must stay unloaded (broken installs, memory, import order).
A callback can run now if the module is present, or later when it is first
imported in this process — the pattern used by TorchScript divert and by
JAX pytree registration on structured types. raises=True still registers
that callback before raising, so a later import can complete the side effect.
module_if_loaded
¶
Return name if it is already imported, else None.
Never imports name. If callback is given and the module is
already loaded, it is invoked immediately with the module. If not,
callback is registered first and invoked later when that module is
imported in this process (including when a submodule import loads the
parent).
If raises is true and the module is still absent after that
registration, raise :class:RuntimeError. The pending callback is kept,
so a later import still runs it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Absolute module name ( |
required |
callback
|
Optional[_Callback]
|
Optional |
None
|
raises
|
bool
|
If true, raise when |
False
|
Returns:
| Type | Description |
|---|---|
ModuleType | None
|
The loaded module, or |
ModuleType | None
|
(only when |
Examples:
Check without importing::
torch = module_if_loaded("torch")
if torch is None:
return
Require an extra that must already be imported::
jax = module_if_loaded("jax", raises=True)
Register a side effect for now-or-later (JAX pytree, TorchScript)::
def _register_jax_pytree(jax):
jax.tree_util.register_pytree_node(Ragged, flatten, unflatten)
module_if_loaded("jax", _register_jax_pytree)
# or module_if_loaded("jax", _register_jax_pytree, raises=True)
# to fail now while still running the callback on a later import.