robot.utils.importer
¶
Importer
¶
Utility that can import modules and classes based on names and paths.
Imported classes can optionally be instantiated automatically.
:param type:
Type of the thing being imported. Used in error and log messages.
:param logger:
Logger to be notified about successful imports and other events.
Currently only needs the info
method, but other level specific
methods may be needed in the future. If not given, logging is disabled.
Source code in src/robot/utils/importer.py
import_class_or_module
¶
Imports Python class or module based on the given name or path.
:param name_or_path: Name or path of the module or class to import. :param instantiate_with_args: When arguments are given, imported classes are automatically initialized using them. :param return_source: When true, returns a tuple containing the imported module or class and a path to it. By default, returns only the imported module or class.
The class or module to import can be specified either as a name, in which
case it must be in the module search path, or as a path to the file or
directory implementing the module. See :meth:import_class_or_module_by_path
for more information about importing classes and modules by path.
Classes can be imported from the module search path using name like
modulename.ClassName
. If the class name and module name are same, using
just CommonName
is enough. When importing a class by a path, the class
name and the module name must match.
Optional arguments to use when creating an instance are given as a list.
Starting from Robot Framework 4.0, both positional and named arguments are
supported (e.g. ['positional', 'name=value']
) and arguments are converted
automatically based on type hints and default values.
If arguments needed when creating an instance are initially embedded into
the name or path like Example:arg1:arg2
, separate
:func:~robot.utils.text.split_args_from_name_or_path
function can be
used to split them before calling this method.
Use :meth:import_module
if only a module needs to be imported.
Source code in src/robot/utils/importer.py
import_module
¶
Imports Python module based on the given name or path.
:param name_or_path: Name or path of the module to import.
The module to import can be specified either as a name, in which
case it must be in the module search path, or as a path to the file or
directory implementing the module. See :meth:import_class_or_module_by_path
for more information about importing modules by path.
Use :meth:import_class_or_module
if it is desired to get a class
from the imported module automatically.
New in Robot Framework 6.0.
Source code in src/robot/utils/importer.py
import_class_or_module_by_path
¶
Import a Python module or class using a file system path.
:param path: Path to the module or class to import. :param instantiate_with_args: When arguments are given, imported classes are automatically initialized using them.
When importing a Python file, the path must end with :file:.py
and the
actual file must also exist.
Use :meth:import_class_or_module
to support importing also using name,
not only path. See the documentation of that function for more information
about creating instances automatically.