from pathlib import Path


def path_to_namespace(path: Path, root: Path) -> str:
    """
    Given a path (in form R/A/B/C) and a root directory R, create a namespace string A.B.C.
    The path must be located under the root directory.

    :param path: Path to convert to namespace
    :param root: Path prefix to remove from namespace.
    :return: A Python namespace string
    """
    return ".".join([Path(x).stem for x in path.relative_to(root).parts])