dbx_patch.install_sitecustomize
source module dbx_patch.install_sitecustomize
Install sitecustomize.py to Auto-Apply Patches on Startup.
This module provides functionality to install a sitecustomize.py file that automatically applies all dbx-patch fixes when Python starts. This solves the timing issue where sys_path_init and WsfsImportHook are loaded before any notebook code runs.
Why sitecustomize.py? - Python automatically imports sitecustomize.py during interpreter initialization - It runs BEFORE sys_path_init.py and import hooks are installed - This is the ONLY way to patch the import system early enough
Usage
from dbx_patch.install_sitecustomize import install_sitecustomize install_sitecustomize()
Functions
-
get_site_packages_path — Get the first writable site-packages directory.
-
get_sitecustomize_content — Generate the sitecustomize.py content.
-
install_sitecustomize — Install sitecustomize.py to auto-apply patches on Python startup.
-
uninstall_sitecustomize — Remove the auto-apply sitecustomize.py.
-
check_sitecustomize_status — Check if sitecustomize.py is installed and active.
source get_site_packages_path() → Path | None
Get the first writable site-packages directory.
Returns
-
Path | None — Path to site-packages directory, or None if not found
source get_sitecustomize_content() → str
Generate the sitecustomize.py content.
Returns
-
str — Python code to be written to sitecustomize.py
source install_sitecustomize(force: bool = True, restart_python: bool = True) → bool
Install sitecustomize.py to auto-apply patches on Python startup.
This is the RECOMMENDED way to use dbx-patch because
- Patches are applied BEFORE sys_path_init runs
- Import hooks are patched BEFORE they're installed
- No need to manually call patch_dbx() in every notebook
- Works automatically for all Python processes on the cluster
Parameters
-
force : bool — If True, overwrite existing sitecustomize.py
-
restart_python : bool — If True, automatically restart Python using dbutils.library.restartPython()
Returns
-
bool — True if installation succeeded, False otherwise
Example
Run once per cluster (e.g., in init script or first notebook):
from dbx_patch.install_sitecustomize import install_sitecustomize install_sitecustomize()
Python will restart automatically if running in Databricks
After restart, editable installs will work automatically!
source uninstall_sitecustomize() → bool
Remove the auto-apply sitecustomize.py.
Returns
-
bool — True if uninstallation succeeded, False otherwise
source check_sitecustomize_status() → SitecustomizeStatus
Check if sitecustomize.py is installed and active.
Returns
-
SitecustomizeStatus — SitecustomizeStatus with installation information