(+351) 21 24 10006  ·  info@bconcepts.pt
Carnaxide, Lisbon

How to reuse notebooks with %run in Databricks

João Barros 09 de July de 2026 4 min read

Copying and pasting the same functions into every Databricks notebook makes your code hard to maintain: a single fix has to be repeated in several places. To reuse code across notebooks in Databricks there is the %run magic command, which runs another notebook and makes its functions and variables available in the current notebook. The result is a single place to maintain your shared utilities, with no duplication.

Prerequisites

  • Access to a Databricks workspace.
  • An active cluster to run the notebooks.
  • Basic Python knowledge.

Step 1: Create the utilities notebook

Create a notebook called utils in the same folder where you will be working. This notebook exists only to hold the functions and variables you want to share — it can contain any Python code, from constants to helper functions. Type the following in a cell:

# Notebook: utils
PAIS_PADRAO = "Portugal"

def saudacao(nome):
    return f"Olá, {nome}!"

def contar_linhas(df):
    return df.count()

You do not need to run this notebook manually. The %run command takes care of that when you call it from another notebook.

Step 2: Put the notebooks in the same folder

The %run command accepts relative paths. If the utils notebook is in the same folder as your main notebook, reference it with ./utils. For different folders, use the full path, for example /Workspace/Users/your-email/utils. Check in the Workspace explorer that both notebooks are where you expect them.

Step 3: Call the notebook with %run

In your main notebook, create a cell with only the %run command. This command must be alone in the cell — you cannot mix it with other Python code in the same cell.

%run ./utils

When you run this cell, Databricks executes the utils notebook from start to finish and brings all of its definitions into the current context. It is as if you had written that code in the main notebook itself.

Step 4: Use the imported functions and variables

After the %run, the saudacao and contar_linhas functions and the PAIS_PADRAO variable are already available. Test them in a new cell:

print(saudacao("bConcepts"))
print("País padrão:", PAIS_PADRAO)

dados = [("Ana", 30), ("Rui", 25)]
df = spark.createDataFrame(dados, ["nome", "idade"])
print("Total de linhas:", contar_linhas(df))

Notice that you did not define any of these functions again: they all came from the utils notebook. That is the win — write once, use anywhere.

Step 5: Reuse it in another notebook

The real benefit shows up when you have several notebooks. Create a second notebook, for example relatorio, in the same folder and add %run ./utils there too, in its own cell. All the functions become available without copying a single line. If one day you fix the saudacao function in the utils notebook, every notebook that runs %run automatically uses the corrected version.

Check the result

If everything went well, the cell from Step 4 shows:

Olá, bConcepts!
País padrão: Portugal
Total de linhas: 2

If you get the error NameError: name 'saudacao' is not defined, it means the %run cell was not run first, or the path is wrong. Make sure you ran the %run cell first and that the name in ./utils matches exactly — case sensitivity counts.

Tip: %run shares the same context, so the variables stay accessible. If you need to run a notebook in isolation and receive a return value, use dbutils.notebook.run("utils", 60) instead.

Conclusion

With %run you get a single source of truth for shared functions and configuration, which reduces errors and eases maintenance. From here, organize your utilities by topic — for example, one notebook for reading data and another for validations — and consider versioning them with Databricks Repos and Git. Which functions do you repeat across your notebooks today that you could already move into a utils?