Ejemplo n.º 1
0
def import_module(uri,
                  name=DEFAULT_MODULE_NAME,
                  cache=None):  # type: (str, str, bool) -> module
    """Download, prepare and install a compressed tar file from S3 or provided directory as a
    module.
    SageMaker Python SDK saves the user provided scripts as compressed tar files in S3
    https://github.com/aws/sagemaker-python-sdk.
    This function downloads this compressed file, if provided, and transforms it as a module, and
    installs it.
    Args:
        name (str): name of the script or module.
        uri (str): the location of the module.
        cache (bool): default True. It will not download and install the module again if it is
                      already installed.
    Returns:
        (module): the imported module
    """
    _warning_cache_deprecation(cache)
    _files.download_and_extract(uri, _env.code_dir)

    prepare(_env.code_dir, name)
    install(_env.code_dir)
    try:
        module = importlib.import_module(name)
        six.moves.reload_module(module)  # pylint: disable=too-many-function-args

        return module
    except Exception as e:  # pylint: disable=broad-except
        six.reraise(_errors.ImportModuleError, _errors.ImportModuleError(e),
                    sys.exc_info()[2])
Ejemplo n.º 2
0
def import_module_from_s3(url, name=DEFAULT_MODULE_NAME, cache=True):  # type: (str, str, bool) -> module
    """Download, prepare and install a compressed tar file from S3 as a module.
    SageMaker Python SDK saves the user provided scripts as compressed tar files in S3
    https://github.com/aws/sagemaker-python-sdk.
    This function downloads this compressed file, transforms it as a module, and installs it.
    Args:
        name (str): name of the script or module.
        url (str): the s3 url of the file.
        cache (bool): default True. It will not download and install the module again if it is already installed.
    Returns:
        (module): the imported module
    """
    download_and_install(url, name, cache)

    try:
        module = importlib.import_module(name)
        six.moves.reload_module(module)

        return module
    except Exception as e:
        six.reraise(_errors.ImportModuleError, _errors.ImportModuleError(e), sys.exc_info()[2])