โ12-08-2025 11:27 PM - edited โ12-08-2025 11:34 PM
Hi
We have setup.py in my databricks workspace.
This script is executed in other transformation scripts using
%run /Workspace/Common/setup.pywhich consume lot of time.
This setup.py internally calls other utilities notebooks using %run
%run /Workspace/Common/01_Utilities.py
%run /Workspace/Common/02_Utilities.pyWe are trying to run setup.py. at cluster level. Currently shell files are allowed in init scripts.
Please help how we can execute this setup.py at cluster level and we can remove execution of this notebook in rest of notebooks.
โ12-09-2025 03:10 AM
@prashant151 - Unlike legacy (pre-UC) clusters, you cannot directly run a Databricks notebook (like setup.py) from a cluster init script, because init scripts only support shell commands โ not %run or notebook execution.
You will need to refactor your setup logic into a Python module and install it via the init script.
I would do below instead
โ12-11-2025 09:15 PM
You canโt โ%run a notebookโ from a cluster init scriptโinit scripts are shell-only and meant for environment setup (install libs, set env vars), not for executing notebooks or sharing Python state across sessions. +1 to what @Raman_Unifeye has told.
Convert your common code into a Python module or wheel and import it in notebooks instead of %run.
Option 1: Workspace modules (no build step)
Move Common/01_Utilities.py, 02_Utilities.py into Workspace Files as .py modules (add init.py). Then import them directly (DR 11.3+ auto-adds CWD to PYTHONPATH).
Example:
from Common.utilities import init_env, foo
init_env()
Option 2: Build a wheel and install per cluster
Package your code (setup.py or pyproject.toml), build a wheel, then install it as a cluster library or via %pip install /Workspace/.../yourpkg.whl (or from a UC volume).
Example:
%pip install /Workspace/Common/dist/yourpkg-0.1.0-py3-none-any.whl
import yourpkg
yourpkg.init_env()
โ12-09-2025 03:10 AM
@prashant151 - Unlike legacy (pre-UC) clusters, you cannot directly run a Databricks notebook (like setup.py) from a cluster init script, because init scripts only support shell commands โ not %run or notebook execution.
You will need to refactor your setup logic into a Python module and install it via the init script.
I would do below instead
โ12-11-2025 09:15 PM
You canโt โ%run a notebookโ from a cluster init scriptโinit scripts are shell-only and meant for environment setup (install libs, set env vars), not for executing notebooks or sharing Python state across sessions. +1 to what @Raman_Unifeye has told.
Convert your common code into a Python module or wheel and import it in notebooks instead of %run.
Option 1: Workspace modules (no build step)
Move Common/01_Utilities.py, 02_Utilities.py into Workspace Files as .py modules (add init.py). Then import them directly (DR 11.3+ auto-adds CWD to PYTHONPATH).
Example:
from Common.utilities import init_env, foo
init_env()
Option 2: Build a wheel and install per cluster
Package your code (setup.py or pyproject.toml), build a wheel, then install it as a cluster library or via %pip install /Workspace/.../yourpkg.whl (or from a UC volume).
Example:
%pip install /Workspace/Common/dist/yourpkg-0.1.0-py3-none-any.whl
import yourpkg
yourpkg.init_env()