Best Python code snippet using lisa_python
package.py
Source:package.py  
...42                f"  loading module from file: {file}, "43                f"full_module_name: '{full_module_name}'",44            )45        importlib.import_module(name=module_name, package=root_package_name)46def _import_root_package(package_name: str, path: Path) -> None:47    # the module can be imported with __init__.py only, but it doesn't need to exist48    init_file = path / "__init__.py"49    spec = importlib.util.spec_from_file_location(50        name=package_name,51        location=init_file,52    )53    assert spec54    module = importlib.util.module_from_spec(spec)55    assert spec.loader56    sys.modules[package_name] = module57    if init_file.exists():58        # if __init__ file exists, execute it's actual import logic.59        spec.loader.exec_module(module)60def import_package(path: Path, package_name: str, enable_log: bool = True) -> None:61    if not path.exists():62        raise FileNotFoundError(f"import module path: {path}")63    if enable_log:64        log: Optional[Logger] = get_logger("init", "module")65        assert log66        log.info(f"loading Python extensions from {path}")67    else:68        log = None69    package_files: Iterable[Path]70    if path.is_file():71        # Import a single module within a package.72        package_dir = path.parent73        package_files = [path]74    else:75        # Import the entire package.76        package_dir = path77        package_files = path.glob("**/*.py")78    # import the package79    _import_root_package(package_name=package_name, path=package_dir)80    # import all the modules in the package81    for file in package_files:82        file_name = file.stem83        # skip test files and __init__.py84        if ("tests" == file.parent.stem and file_name.startswith("test_")) or (85            file.stem == "__init__"86        ):87            continue88        _import_module(89            file=file,90            root_package_name=package_name,91            package_dir=package_dir,92            log=log,93        )Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
