Skip to content

Defend against broken paths from non-editable installs #356

@jayqi

Description

@jayqi

The refreshed Python module boilerplate (#354) adds a {module_name}.config module that contains pathlib.Path instances to various project directories. These paths depend on the project module being installed as editable in order to resolve correctly.

Someone following the golden path while using a CCDS-generated project should end up with an editable installation. However, it's possible for someone to not do so. Some possible mitigations:

  • Detect if not an editable install, warn users
  • Set paths a different way intelligently if not an editable install

Some prototype code for achieving this that was ultimately removed from #354

# Check if the package is installed as editable
def _is_editable():
    # https://peps.python.org/pep-0660/#frontend-requirements
    try:
        dist = importlib.metadata.distribution("{{ cookiecutter.module_name }}")
        direct_url_data = dist.read_text("direct_url.json")
        if direct_url_data is None:
            return False
        return json.loads(direct_url_data).get("dir_info", {}).get("editable", False)
    except importlib.metadata.PackageNotFoundError:
        return False



IS_EDITABLE = _is_editable()

# Determine PROJ_ROOT path
if os.getenv("PROJ_ROOT"):
    logger.debug("Reading PROJ_ROOT from environment variable.")
    PROJ_ROOT = Path(os.getenv("PROJ_ROOT"))
elif IS_EDITABLE:
    logger.debug("Setting PROJ_ROOT relative to editable package.")
    PROJ_ROOT = Path(__file__).resolve().parents[1]
else:
    logger.debug("Using current working directory as PROJ_ROOT.")
    PROJ_ROOT = Path.cwd()

Some more discussion here: #354 (comment)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions