Tuesday
Hi all,
I'm looking for a best practice on how to share several Python packages with notebooks in a Databricks bundle on serverless compute.
Setup
bundle-root/
โโโ databricks.yml
โโโ pyproject.toml
โโโ uv.lock
โโโ resources/
โโโ notebooks/
โ โโโ main.ipynb
โโโ src/
โโโ my_project/
โ โโโ foo/__init__.py
โโโ common/__init__.py
โโโ ingestion/__init__.pyLocally everything works: `uv sync` installs the project in editable mode (hatchling, `packages = ["src/my_project", "src/common", "src/ingestion"]`) and `from my_project.foo.utils import bar` works in the notebook.
After `databricks bundle deploy`, on serverless I get `ModuleNotFoundError: No module named 'my_project'`, because the bundle root / `src` is not on `sys.path`.
Options I see
1. **Plain source + sys.path:** Add `src` to `sys.path` at the top of every notebook. Works, but feels hacky, depends on the notebook location, and doesn't handle third-party dependencies.
2. **One pyproject.toml โ one wheel with all packages:**
a) Use the Beta `project_environment` (https://docs.databricks.com/aws/en/compute/serverless/dependencies#configure-a-project-environment-w...), which needs the "Project-Scoped Shared Compute" preview, environment version 5+, an up-to-date `uv.lock` and CLI 1.21.0+.
b) Build the wheel as a bundle artifact and `%pip install` it in the notebook (or via `environments` for wheel tasks).
Questions
- What is your recommended / production-proven approach for this?
- For option 2b: how do you reference the deployed wheel from a notebook task without hardcoding the workspace path?
- For option 2a: i build one wheel cotaining with all packages: is that fine?
- How do you keep interactive development in the workspace fast (no rebuild/redeploy after every code change)?
Thanks a lot!
Wednesday
src/ plus a third-party dependency) on serverless, on both AWS and Azure, with environment versions 4 and 5.pyproject.toml: one wheel can contain all three packages:[project]
name = "my_project"
version = "0.1.0"
dependencies = ["tabulate"] # your third-party deps go here
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
[tool.hatch.build.targets.wheel]
packages = ["src/my_project", "src/common", "src/ingestion"]databricks.yml:artifacts:
default:
type: whl
build: uv build --wheel
path: .tasks:
- task_key: main
notebook_task:
notebook_path: ../notebooks/main.ipynb
environment_key: default
environments:
- environment_key: default
spec:
environment_version: "5"
dependencies:
- ../dist/*.whlโbundle deploy builds the wheel, uploads it and resolves the path, so you never hardcode a workspace location. In the notebook, my_project.foo, common and ingestion all import as top-level packages, and the third-party deps get installed from pyproject.toml.databricks.yml. You need it because dist/ is usually gitignored and sync skips it otherwise:sync:
include:
- dist/*.whlbundle_root: ${workspace.file_path} to the task as a parameter, then run:import glob
whl = sorted(glob.glob(f"{dbutils.widgets.get('bundle_root')}/dist/*.whl"))[-1]
%pip install {whl}project_environment: it's still Beta and needs a workspace preview. The wheel approach works without it, and moving over later is a small config change.sys.path: it does find your own src code, and the official default-python bundle template uses it in its sample notebook. But it doesn't install third-party dependencies, which is where it failed in my test.import sys, os
sys.path.insert(0, os.path.abspath("../src"))databricks bundle sync --watch locally while you edit, and importlib.reload(module) in the notebook to pick up changes. No rebuild needed. When you want to test the real job setup, bundle deploy to your dev target takes a few seconds.%pip install -e on the bundle folder. The deps install, but serverless doesn't put the editable path on sys.path, so your own packages still fail to import.uv sync env is also an option, but I didn't verify it end to end with this layout.Wednesday
src/ plus a third-party dependency) on serverless, on both AWS and Azure, with environment versions 4 and 5.pyproject.toml: one wheel can contain all three packages:[project]
name = "my_project"
version = "0.1.0"
dependencies = ["tabulate"] # your third-party deps go here
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
[tool.hatch.build.targets.wheel]
packages = ["src/my_project", "src/common", "src/ingestion"]databricks.yml:artifacts:
default:
type: whl
build: uv build --wheel
path: .tasks:
- task_key: main
notebook_task:
notebook_path: ../notebooks/main.ipynb
environment_key: default
environments:
- environment_key: default
spec:
environment_version: "5"
dependencies:
- ../dist/*.whlโbundle deploy builds the wheel, uploads it and resolves the path, so you never hardcode a workspace location. In the notebook, my_project.foo, common and ingestion all import as top-level packages, and the third-party deps get installed from pyproject.toml.databricks.yml. You need it because dist/ is usually gitignored and sync skips it otherwise:sync:
include:
- dist/*.whlbundle_root: ${workspace.file_path} to the task as a parameter, then run:import glob
whl = sorted(glob.glob(f"{dbutils.widgets.get('bundle_root')}/dist/*.whl"))[-1]
%pip install {whl}project_environment: it's still Beta and needs a workspace preview. The wheel approach works without it, and moving over later is a small config change.sys.path: it does find your own src code, and the official default-python bundle template uses it in its sample notebook. But it doesn't install third-party dependencies, which is where it failed in my test.import sys, os
sys.path.insert(0, os.path.abspath("../src"))databricks bundle sync --watch locally while you edit, and importlib.reload(module) in the notebook to pick up changes. No rebuild needed. When you want to test the real job setup, bundle deploy to your dev target takes a few seconds.%pip install -e on the bundle folder. The deps install, but serverless doesn't put the editable path on sys.path, so your own packages still fail to import.uv sync env is also an option, but I didn't verify it end to end with this layout.Wednesday - last edited Wednesday
thanks for the clarification.
I conclude (after beta is over): use pyproject.toml + project environment: Then artifacts section shouldn't be necessary any more (?)
I assume, that there are no caching issues (as environments are usually cached)
Wednesday
Hello @a_user12, I took a look at both internal and external documentation and here is what I found.
@Avi3 has already given you a solid, tested answer. I agree with it and only have a few things to add from the docs.
Why the import breaks after deploy: locally, uv sync installs your project into the venv, so my_project, common and ingestion are all importable. On Databricks, a notebook only gets its own working directory added to sys.path automatically (on DBR 11.3 LTS and above). src/ isn't on the path, and nothing has installed the packages yet, so you get ModuleNotFoundError: No module named 'my_project'.
Answers to your questions:
1. Production approach. Go with what @Avi3 described. Build one wheel with a uv build --wheel artifact, then attach it to the notebook task through a job environment (environments โ spec.dependencies: - ../dist/*.whl, with environment_key set on the task). The bundle resolves and uploads the path, so you never hardcode a workspace location. Your third-party dependencies come in through pyproject.toml. Your current layout already follows the recommended pattern: logic in src/ modules, with notebooks as thin orchestration on top.
2. Notebook tasks and job environments. The serverless docs say notebook tasks default to the notebook's own environment, and you can override that with a job-level environment. environment_key is only required for Python script, wheel and dbt tasks, but it's allowed on notebook tasks. So the %pip install fallback should only be needed if your workspace rejects that configuration.
3. One wheel with several top-level packages. That's fine. Your [tool.hatch.build.targets.wheel] packages = ["src/my_project", "src/common", "src/ingestion"] is the standard Hatchling way to do it. Split into separate wheels only if the packages need independent versions or release cycles.
4. project_environment. This points the task straight at pyproject.toml and uv.lock. It's still Beta and needs an admin to enable the preview, plus environment version 5+ and CLI 1.21.0+. One detail: the project itself only gets installed when uv.lock is current. If it isn't, only the dependencies are installed and you'd hit the same import error. Until it's GA, the wheel approach is the safer default.
5. Fast interactive development. Keep the job environment so third-party dependencies are present. Then point the notebook at the synced src folder and run databricks bundle sync --watch locally. There are two gotchas:
- Workspace paths appended to sys.path have the lowest library precedence, so the installed wheel would win. Use sys.path.insert(0, ...) instead of append so your live source takes priority.
- Instead of calling importlib.reload by hand, you can turn on autoreload (%load_ext autoreload then %autoreload 2), which re-imports changed modules before each cell runs. It doesn't push code to Spark executors, so UDF code still needs a fresh session.
Keep the sys.path override in dev only. Production runs should rely on the installed wheel.
References
Serverless environment, job task environments: https://docs.databricks.com/aws/en/compute/serverless/dependencies#configure-the-environment-by-task...
Project environment with pyproject.toml (Beta): https://docs.databricks.com/aws/en/compute/serverless/dependencies#configure-a-project-environment-w...
Bundle library dependencies (uv build artifact): https://docs.databricks.com/aws/en/dev-tools/bundles/library-dependencies
Bundle task settings (environment_key): https://docs.databricks.com/aws/en/dev-tools/bundles/job-task-types
Python library precedence: https://docs.databricks.com/aws/en/libraries#python-library-precedence
Python modules, sys.path and autoreload: https://docs.databricks.com/aws/en/files/workspace-modules
Developer best practices (src layout, thin notebooks): https://docs.databricks.com/aws/en/developers/best-practices/
If you found this answer helpful please check off "Accept as Solutions" so that others can have confidence in the answer.
Cheers, Lou.
Wednesday
Thanks for clarifying.
Regarding 5: I think the main benefit of the serverless folder is, that everything (vs-code, web-ui and jobs) are configured via pyproject.toml. So I think, when using serverless folders no on-top configuration (sys.path modification) is necessary right?