Sub topic 2
Attaching a notebook to compute
This chapter assumes you can create a notebook in the workspace and run basic Python or SQL cells. It also assumes you know the difference between a driver and workers in Spark. A notebook holds code, but it cannot run that code itself. It has to be attached to compute, and this chapter covers how attaching works, what it changes, and where it goes wrong.
What attaching means
A notebook is a document. Compute is the set of machines that executes it. When you attach a notebook, the workspace opens an execution session between the notebook and that compute. Every cell you run is sent to it. Results and printed output come back to the notebook.
A notebook is attached to one compute resource at a time. A compute resource can serve many notebooks at once. You choose the compute from the selector in the notebook toolbar. Until you pick one, cells that need execution cannot run.
Compute options for a notebook
| Compute type | Typical use | Notes |
|---|---|---|
| All-purpose compute (cluster) | Interactive development | You choose the Databricks Runtime version, node types, and access mode. It stays up until it is terminated or auto-terminates. |
| Serverless compute | Interactive work with no cluster setup | Databricks manages the infrastructure, and attaching is quick. It supports a narrower set of languages and settings than a cluster. Check current limits for your workspace. |
| SQL warehouse | SQL-only notebooks | Python cells will not run against it. |
| Job compute | Scheduled runs | Created for one job run and removed afterward. You do not attach to it by hand. |
Which options appear in the selector depends on your workspace configuration and your permissions.
Permissions decide what you can do
Two cluster permissions matter most for attaching:
- Can Attach To lets you attach notebooks and run commands. You cannot start or restart the cluster.
- Can Restart includes attach rights and also lets you start, stop, and restart the cluster.
If the cluster you need is not in the selector, you probably lack at least Can Attach To. Ask the cluster owner or a workspace admin. Do not work around it by creating a larger cluster of your own.
The attach lifecycle
Attaching has a few states, and the cluster has its own states underneath.
If you select a terminated cluster and you have Can Restart, you can start it from the selector. A cluster start takes minutes, and the notebook waits in the attaching state. If you have only Can Attach To, you must wait for someone else to start it.
What state lives on the compute
A notebook's variables, imported modules, and temporary views live in the memory of the attached compute, not in the notebook file. This has two consequences:
- Detaching, restarting, or losing the compute (for example through auto-termination) clears that state. The cell outputs you see may remain on screen, but the variables behind them are gone.
- Saved files, tables, and the notebook source are unaffected. Only in-memory state is lost.
Try it with two cells:
total = sum(range(10))
print(total)print(total + 1)Run both and you get 45 and 46. Detach and re-attach the notebook, then run only the second cell. It fails with NameError: name 'total' is not defined, because the variable lived in the session you ended.
Notebooks sharing one cluster
Several notebooks can attach to the same all-purpose cluster. Each notebook gets its own isolated session, so a variable named df in one notebook does not collide with df in another. They do share the cluster's resources. A heavy job in one notebook slows the others.
Libraries behave differently depending on scope:
- Libraries installed on the cluster are visible to every notebook attached to it.
%pip installinside a notebook installs a notebook-scoped library, visible only to that notebook's session.
%pip install requests==2.32.3Run %pip in the first cell of a notebook, before other code. It restarts the Python process, which clears any Python variables you defined earlier. Placing it first avoids losing work.
Checking what you are attached to
When a result looks wrong, first confirm which runtime you are on. This cell prints the Spark version of the attached compute:
print(spark.version)The Databricks Runtime version is chosen in the cluster configuration, not in the notebook. It controls the Spark version, the Python version, and the preinstalled libraries. A notebook that works on one runtime can fail on another because a library version differs. Record the runtime you tested on when you share code.
Attachment and Git
The compute a notebook is attached to is a property of your session in the workspace. It is not part of the notebook's source file. Because of this:
- Committing a notebook to a Git folder does not record which cluster it used.
- A teammate who opens the same notebook after pulling starts detached and picks their own compute.
- A job that runs the notebook from Git defines its own compute in the job configuration. It ignores whatever you were attached to while developing.
Anything the notebook needs from its environment, such as a runtime version or a library, must be stated in a place that travels with the code. Use a %pip cell, a requirements file, or the job definition.
Common errors
- The cluster is missing from the selector. You lack permission, or the cluster is not available to your workspace. Check with its owner.
- The notebook stays in "Attaching" for a long time. The cluster is starting, or it is stuck waiting for cloud capacity. Check the cluster's event log.
- A Python cell fails on a SQL warehouse. SQL warehouses run SQL only. Attach to a cluster or serverless compute instead.
- A library import works on your cluster but not on a teammate's. The library is installed at cluster scope on your cluster only. Move the install into the notebook with
%pipor into shared configuration. - Variables vanished mid-session. The cluster restarted, auto-terminated, or the notebook was detached. Rerun the cells.
Exercise
You develop a notebook on an all-purpose cluster, commit it to a Git folder, and a teammate pulls it. The teammate runs it and gets a ModuleNotFoundError for a library you had installed on the cluster. Why did it fail, and what are two ways to prevent it?
Show answer
The library was installed at cluster scope on your cluster, and the notebook attachment is not stored in Git, so the teammate's compute does not have it. Prevent it by adding a %pip install cell at the top of the notebook, which installs the library into the notebook's own session. Another way is to declare the library in the job or compute configuration that runs the notebook, so the environment is defined with the code and not with one person's cluster.