Dynamic Importing¶
Functions for dynamically importing from Python modules.
- concoursetools.importing.import_single_class_from_module(file_path, parent_class, class_name=None)[source]¶
Import the resource class from the module.
Similar to
import_classes_from_module(), but ensures only one class is returned.- Parameters:
- Return type:
- Returns:
The extracted class.
- Raises:
RuntimeError – If too many or too few classes are available in the module, unless the class name is specified.
- concoursetools.importing.import_classes_from_module(file_path, parent_class)[source]¶
Import all available resource classes from the module.
- concoursetools.importing.file_path_to_import_path(file_path)[source]¶
Convert a file path to an import path.
- Parameters:
file_path (
Path) – The path to a Python file.- Raises:
ValueError – If the path doesn’t end in a ‘.py’ extension.
- Example:
>>> file_path_to_import_path(Path("module.py")) 'module' >>> file_path_to_import_path(Path("path/to/module.py")) 'path.to.module'
- Return type:
- concoursetools.importing.import_py_file(import_path, file_path)[source]¶
Import a .py file as a module.
This is done using a standard Python recipe via
importlib.util.- Parameters:
import_path (
str) – The import path added tosys.modules.file_path (
Path) – The path to the .py module.
- Return type:
- Returns:
The imported module.
- Raises:
FileNotFoundError – If the path does not exist.