latest (dev)
Copy
Latest development documentation · Updated 2026-10-08
Importer
- class tensorplay.package.Importer[source]
Represents an environment to import modules from.
By default, you can figure out what module an object belongs by checking __module__ and importing the result using __import__ or importlib.import_module.
tensorplay.package introduces module importers other than the default one. Each PackageImporter introduces a new namespace. Potentially a single name (e.g. ‘foo.bar’) is present in multiple namespaces.
- It supports two main operations:
import_module: module_name -> module object get_name: object -> (parent module name, name of obj within module)
- The guarantee is that following round-trip will succeed or throw an ObjNotFoundError/ObjMisMatchError.
module_name, obj_name = env.get_name(obj) module = env.import_module(module_name) obj2 = getattr(module, obj_name) assert obj1 is obj2
- get_name(obj: Any, name: str | None = None) tuple[str, str][source]
Given an object, return a name that can be used to retrieve the object from this environment.
- Parameters:
obj – An object to get the module-environment-relative name for.
name – If set, use this name instead of looking up
__name__or__qualname__onobj. This is only here to match how Pickler handles__reduce__functions that return a string, don’t use otherwise.
- Returns:
A tuple (parent_module_name, attr_name) that can be used to retrieve
objfrom this environment. To use it:mod = importer.import_module(parent_module_name) obj = getattr(mod, attr_name)- Raises:
ObjNotFoundError – we couldn’t retrieve
objby name.ObjMisMatchError – we found a different object with the same name as
obj.
- abstractmethod import_module(module_name: str) ModuleType[source]
Import module_name from this environment.
The contract is the same as for importlib.import_module.
- whichmodule(obj: Any, name: str) str[source]
Find the module name an object belongs to.
This should be considered internal for end-users, but developers of an importer can override it to customize the behavior.
Based on the classic pickle.py approach, but modified to exclude the search into sys.modules.
Help improve this page
Found an error, an unclear step, or a missing example?

