# PackageExporter Source: https://www.tensorplay.cn/docs/generated/tensorplay.package.PackageExporter.html ```python class tensorplay.package.PackageExporter(f: str | ~os.PathLike[str] | ~typing.IO[bytes], importer: ~tensorplay.package.importer.Importer | ~collections.abc.Sequence[~tensorplay.package.importer.Importer] = , debug: bool = False) ``` Exporters allow you to write packages of code, pickled Python data, and arbitrary binary and text resources into a self-contained package. Imports can load this code in a hermetic way, such that code is loaded from the package rather than the normal Python import system. This allows for the packaging of model code and data so that it can be run on a server or used in the future for transfer learning. The code contained in packages is copied file-by-file from the original source when it is created, and the file format is a specially organized zip file. Future users of the package can unzip the package, and edit the code in order to perform custom modifications to it. The importer for packages ensures that code in the module can only be loaded from within the package, except for modules explicitly listed as external using [extern()](#tensorplay.package.PackageExporter.extern). The file extern_modules in the zip archive lists all the modules that a package externally depends on. This prevents “implicit” dependencies where the package runs locally because it is importing a locally-installed package, but then fails when the package is copied to another machine. When source code is added to the package, the exporter can optionally scan it for further code dependencies (dependencies=True). It looks for import statements, resolves relative references to qualified module names, and performs an action specified by the user (See: [extern()](#tensorplay.package.PackageExporter.extern), [mock()](#tensorplay.package.PackageExporter.mock), and [intern()](#tensorplay.package.PackageExporter.intern)). Only plain Python modules are supported: C extension modules and other compiled or interpreter-specific artifacts cannot be interned. Objects embedded with [save_pickle()](#tensorplay.package.PackageExporter.save_pickle) serialize tensors through tensorplay’s own pickling support. ```python add_dependency(module_name: str, dependencies=True) ``` Given a module, add it to the dependency graph according to patterns specified by the user. ```python all_paths(src: str, dst: str) → str ``` Return a dot representation of the subgraph that has all paths from src to dst. Returns: A dot representation containing all paths from src to dst. ([https://graphviz.org/doc/info/lang.html](https://graphviz.org/doc/info/lang.html)) ```python close() ``` Write the package to the filesystem. Any calls after [close()](#tensorplay.package.PackageExporter.close) are now invalid. It is preferable to use resource guard syntax instead: ``` with PackageExporter("file.zip") as e: ... ``` ```python denied_modules() → list[str] ``` Return all modules that are currently denied. Returns: A list containing the names of modules which will be denied in this package. ```python deny(include: str | Iterable[str], *, exclude: str | Iterable[str] = ()) ``` Blocklist modules whose names match the given glob patterns from the list of modules the package can import. If a dependency on any matching packages is found, a [PackagingError](/docs/generated/tensorplay.package.PackagingError.html#tensorplay.package.PackagingError) is raised. Parameters: - include ([list](https://docs.python.org/3/builtins/stdtypes.html#list)[[str](https://docs.python.org/3/builtins/stdtypes.html#str)] | [str](https://docs.python.org/3/builtins/stdtypes.html#str)) – A string e.g. "my_package.my_subpackage", or list of strings for the names of the modules to be externed. This can also be a glob-style pattern, as described in [mock()](#tensorplay.package.PackageExporter.mock). - exclude ([list](https://docs.python.org/3/builtins/stdtypes.html#list)[[str](https://docs.python.org/3/builtins/stdtypes.html#str)] | [str](https://docs.python.org/3/builtins/stdtypes.html#str)) – An optional pattern that excludes some patterns that match the include string. ```python dependency_graph_string() → str ``` Returns digraph string representation of dependencies in package. Returns: A string representation of dependencies in package. ```python extern(include: str | Iterable[str], *, exclude: str | Iterable[str] = (), allow_empty: bool = True) ``` Include module in the list of external modules the package can import. This will prevent dependency discovery from saving it in the package. The importer will load an external module directly from the standard import system. Code for extern modules must also exist in the process loading the package. Parameters: - include ([list](https://docs.python.org/3/builtins/stdtypes.html#list)[[str](https://docs.python.org/3/builtins/stdtypes.html#str)] | [str](https://docs.python.org/3/builtins/stdtypes.html#str)) – A string e.g. "my_package.my_subpackage", or list of strings for the names of the modules to be externed. This can also be a glob-style pattern, as described in [mock()](#tensorplay.package.PackageExporter.mock). - exclude ([list](https://docs.python.org/3/builtins/stdtypes.html#list)[[str](https://docs.python.org/3/builtins/stdtypes.html#str)] | [str](https://docs.python.org/3/builtins/stdtypes.html#str)) – An optional pattern that excludes some patterns that match the include string. - allow_empty ([bool](https://docs.python.org/3/builtins/functions.html#bool)) – An optional flag that specifies whether the extern modules specified by this call to the extern method must be matched to some module during packaging. If an extern module glob pattern is added with allow_empty=False, and [close()](#tensorplay.package.PackageExporter.close) is called (either explicitly or via __exit__) before any modules match that pattern, an exception is thrown. If allow_empty=True, no such exception is thrown. ```python externed_modules() → list[str] ``` Return all modules that are currently externed. Returns: A list containing the names of modules which will be externed in this package. ```python get_rdeps(module_name: str) → list[str] ``` Return a list of all modules which depend on the module module_name. Returns: A list containing the names of modules which depend on module_name. ```python get_unique_id() → str ``` Get an id. This id is guaranteed to only be handed out once for this package. ```python intern(include: str | Iterable[str], *, exclude: str | Iterable[str] = (), allow_empty: bool = True) ``` Specify modules that should be packaged. A module must match some intern pattern in order to be included in the package and have its dependencies processed recursively. Parameters: - include ([list](https://docs.python.org/3/builtins/stdtypes.html#list)[[str](https://docs.python.org/3/builtins/stdtypes.html#str)] | [str](https://docs.python.org/3/builtins/stdtypes.html#str)) – A string e.g. “my_package.my_subpackage”, or list of strings for the names of the modules to be externed. This can also be a glob-style pattern, as described in [mock()](#tensorplay.package.PackageExporter.mock). - exclude ([list](https://docs.python.org/3/builtins/stdtypes.html#list)[[str](https://docs.python.org/3/builtins/stdtypes.html#str)] | [str](https://docs.python.org/3/builtins/stdtypes.html#str)) – An optional pattern that excludes some patterns that match the include string. - allow_empty ([bool](https://docs.python.org/3/builtins/functions.html#bool)) – An optional flag that specifies whether the intern modules specified by this call to the intern method must be matched to some module during packaging. If an intern module glob pattern is added with allow_empty=False, and [close()](#tensorplay.package.PackageExporter.close) is called (either explicitly or via __exit__) before any modules match that pattern, an exception is thrown. If allow_empty=True, no such exception is thrown. ```python interned_modules() → list[str] ``` Return all modules that are currently interned. Returns: A list containing the names of modules which will be interned in this package. ```python mock(include: str | Iterable[str], *, exclude: str | Iterable[str] = (), allow_empty: bool = True) ``` Replace some required modules with a mock implementation. Mocked modules will return a fake object for any attribute accessed from it. Because we copy file-by-file, the dependency resolution will sometimes find files that are imported by model files but whose functionality is never used (e.g. custom serialization code or training helpers). Use this function to mock this functionality out without having to modify the original code. Parameters: - include ([list](https://docs.python.org/3/builtins/stdtypes.html#list)[[str](https://docs.python.org/3/builtins/stdtypes.html#str)] | [str](https://docs.python.org/3/builtins/stdtypes.html#str)) – A string e.g. "my_package.my_subpackage", or list of strings for the names of the modules to be mocked out. Strings can also be a glob-style pattern string that may match multiple modules. Any required dependencies that match this pattern string will be mocked out automatically. Examples :'tensorplay.**' – matches tensorplay and all submodules of tensorplay, e.g. 'tensorplay.nn' and 'tensorplay.nn.functional' 'tensorplay.*' – matches 'tensorplay.nn' or 'tensorplay.functional', but not 'tensorplay.nn.functional' - exclude ([list](https://docs.python.org/3/builtins/stdtypes.html#list)[[str](https://docs.python.org/3/builtins/stdtypes.html#str)] | [str](https://docs.python.org/3/builtins/stdtypes.html#str)) – An optional pattern that excludes some patterns that match the include string. e.g. include='tensorplay.**', exclude='tensorplay.foo' will mock all tensorplay packages except 'tensorplay.foo', Default: is []. - allow_empty ([bool](https://docs.python.org/3/builtins/functions.html#bool)) – An optional flag that specifies whether the mock implementation(s) specified by this call to the [mock()](#tensorplay.package.PackageExporter.mock) method must be matched to some module during packaging. If a mock is added with allow_empty=False, and [close()](#tensorplay.package.PackageExporter.close) is called (either explicitly or via __exit__) and the mock has not been matched to a module used by the package being exported, an exception is thrown. If allow_empty=True, no such exception is thrown. ```python mocked_modules() → list[str] ``` Return all modules that are currently mocked. Returns: A list containing the names of modules which will be mocked in this package. ```python register_extern_hook(hook: Callable[[PackageExporter, str], None]) → RemovableHandle ``` Registers an extern hook on the exporter. The hook will be called each time a module matches against an [extern()](#tensorplay.package.PackageExporter.extern) pattern. It should have the following signature: ``` hook(exporter: PackageExporter, module_name: str) -> None ``` Hooks will be called in order of registration. Returns: A handle that can be used to remove the added hook by calling handle.remove(). Return type: tensorplay.utils.hooks.RemovableHandle ```python register_intern_hook(hook: Callable[[PackageExporter, str], None]) → RemovableHandle ``` Registers an intern hook on the exporter. The hook will be called each time a module matches against an [intern()](#tensorplay.package.PackageExporter.intern) pattern. It should have the following signature: ``` hook(exporter: PackageExporter, module_name: str) -> None ``` Hooks will be called in order of registration. Returns: A handle that can be used to remove the added hook by calling handle.remove(). Return type: tensorplay.utils.hooks.RemovableHandle ```python register_mock_hook(hook: Callable[[PackageExporter, str], None]) → RemovableHandle ``` Registers a mock hook on the exporter. The hook will be called each time a module matches against a [mock()](#tensorplay.package.PackageExporter.mock) pattern. It should have the following signature: ``` hook(exporter: PackageExporter, module_name: str) -> None ``` Hooks will be called in order of registration. Returns: A handle that can be used to remove the added hook by calling handle.remove(). Return type: tensorplay.utils.hooks.RemovableHandle ```python save_binary(package, resource, binary: bytes) ``` Save raw bytes to the package. Parameters: - package ([str](https://docs.python.org/3/builtins/stdtypes.html#str)) – The name of module package this resource should go in (e.g. "my_package.my_subpackage"). - resource ([str](https://docs.python.org/3/builtins/stdtypes.html#str)) – A unique name for the resource, used to identify it to load. - binary ([str](https://docs.python.org/3/builtins/stdtypes.html#str)) – The data to save. ```python save_module(module_name: str, dependencies=True) ``` Save the code for module into the package. Code for the module is resolved using the importers path to find the module object, and then using its __file__ attribute to find the source code. Parameters: - module_name ([str](https://docs.python.org/3/builtins/stdtypes.html#str)) – e.g. my_package.my_subpackage, code will be saved to provide code for this package. - dependencies ([bool](https://docs.python.org/3/builtins/functions.html#bool), optional) – If True, we scan the source for dependencies. ```python save_pickle(package: str, resource: str, obj: Any, dependencies: bool = True, pickle_protocol: int = 3) ``` Save a python object to the archive using pickle. Equivalent to tensorplay.save() but saving into the archive rather than a stand-alone file. Standard pickle does not save the code, only the objects. If dependencies is true, this method will also scan the pickled objects for which modules are required to reconstruct them and save the relevant code. To be able to save an object where type(obj).__name__ is my_module.MyObject, my_module.MyObject must resolve to the class of the object according to the importer order. When saving objects that have previously been packaged, the importer’s import_module method will need to be present in the importer list for this to work. Tensors inside obj are serialized by tensorplay’s own pickling support. Parameters: - package ([str](https://docs.python.org/3/builtins/stdtypes.html#str)) – The name of module package this resource should go in (e.g. "my_package.my_subpackage"). - resource ([str](https://docs.python.org/3/builtins/stdtypes.html#str)) – A unique name for the resource, used to identify it to load. - obj (Any) – The object to save, must be picklable. - dependencies ([bool](https://docs.python.org/3/builtins/functions.html#bool), optional) – If True, we scan the source for dependencies. ```python save_source_file(module_name: str, file_or_directory: str, dependencies=True) ``` Adds the local file system file_or_directory to the source package to provide the code for module_name. Parameters: - module_name ([str](https://docs.python.org/3/builtins/stdtypes.html#str)) – e.g. "my_package.my_subpackage", code will be saved to provide code for this package. - file_or_directory ([str](https://docs.python.org/3/builtins/stdtypes.html#str)) – the path to a file or directory of code. When a directory, all python files in the directory are recursively copied using [save_source_file()](#tensorplay.package.PackageExporter.save_source_file). If a file is named "/__init__.py" the code is treated as a package. - dependencies ([bool](https://docs.python.org/3/builtins/functions.html#bool), optional) – If True, we scan the source for dependencies. ```python save_source_string(module_name: str, src: str, is_package: bool = False, dependencies: bool = True) ``` Adds src as the source code for module_name in the exported package. Parameters: - module_name ([str](https://docs.python.org/3/builtins/stdtypes.html#str)) – e.g. my_package.my_subpackage, code will be saved to provide code for this package. - src ([str](https://docs.python.org/3/builtins/stdtypes.html#str)) – The Python source code to save for this package. - is_package ([bool](https://docs.python.org/3/builtins/functions.html#bool), optional) – If True, this module is treated as a package. Packages are allowed to have submodules (e.g. my_package.my_subpackage.my_subsubpackage), and resources can be saved inside them. Defaults to False. - dependencies ([bool](https://docs.python.org/3/builtins/functions.html#bool), optional) – If True, we scan the source for dependencies. ```python save_text(package: str, resource: str, text: str) ``` Save text data to the package. Parameters: - package ([str](https://docs.python.org/3/builtins/stdtypes.html#str)) – The name of module package this resource should go in (e.g. "my_package.my_subpackage"). - resource ([str](https://docs.python.org/3/builtins/stdtypes.html#str)) – A unique name for the resource, used to identify it to load. - text ([str](https://docs.python.org/3/builtins/stdtypes.html#str)) – The contents to save.