Skip to content

dbx_patch.pth_processor

source module dbx_patch.pth_processor

PTH File Processor for Editable Installs.

This module processes .pth files in site-packages directories to ensure editable install paths are added to sys.path. This is critical because Databricks runtime bypasses standard Python site.py initialization.

Supports

  • Legacy setuptools .egg-link files
  • PEP 660 __editable_*.pth files
  • Standard .pth files with directory paths

Functions

source get_site_packages_dirs()list[str]

Get all site-packages and dist-packages directories from sys.path.

Returns

  • list[str] List of absolute paths to site-packages directories that exist.

source find_pth_files(site_packages_dir: str)list[str]

Find all .pth files in a site-packages directory.

Parameters

  • site_packages_dir : str Path to site-packages directory

Returns

  • list[str] List of absolute paths to .pth files

source process_pth_file(pth_file_path: str)list[str]

Process a single .pth file and extract directory paths.

PTH files can contain

  • Directory paths (one per line)
  • import statements (executed to install finders for PEP 660 editable installs)
  • Comments (lines starting with #)

For modern PEP 660 editable installs, the .pth file contains import statements that register import hooks. We execute these statements to properly install the editable package.

Parameters

  • pth_file_path : str Path to the .pth file

Returns

  • list[str] List of absolute directory paths found in the file

Find paths from .egg-link files (legacy setuptools editable installs).

Parameters

  • site_packages_dir : str Path to site-packages directory

Returns

  • list[str] List of absolute paths from .egg-link files

source detect_editable_installs_via_metadata()set[str]

Detect editable installs via importlib.metadata (PEP 660 modern approach).

Returns

  • set[str] Set of absolute paths to editable install directories

source add_paths_to_sys_path(paths: list[str], prepend: bool = False)int

Add paths to sys.path if not already present.

Parameters

  • paths : list[str] List of absolute directory paths

  • prepend : bool If True, add to beginning of sys.path; otherwise append

Returns

  • int Number of paths actually added

source process_all_pth_files(force: bool = False)PthProcessingResult

Process all .pth files in all site-packages directories.

This is the main entry point for fixing editable install imports.

Parameters

  • force : bool If True, re-add paths even if they're already in sys.path

Returns

source get_editable_install_paths()set[str]

Get all editable install paths without modifying sys.path.

Returns

  • set[str] Set of absolute paths to editable install directories