Share code between Databricks notebooks

This article describes how to use files to modularize your code, either in the Databricks workspace or in a Databricks Repo.

Databricks also supports multi-task jobs, which allow you to combine notebooks into workflows with complex dependencies.

Modularize your code using files

With Databricks Runtime 8.4 and above, you can create and manage source code files in a Databricks Repo, which lets you sync your files with a Git repository.

For more information on working with source code files, see Work with Python and R modules.

Preview

This feature is in Public Preview.

With Databricks Runtime 11.2 and above, you can create and manage source code files in the Databricks workspace, and then import these files into your notebooks as needed.

Create a file

To create a file:

  1. Navigate to a folder in the workspace.

  2. Right-click on the folder name and select Create > File.

  3. Enter a name for the file and click Create File or press Enter. The file opens in an editor window. Changes are saved automatically.

Open a file

Navigate to the file in your workspace and click on it. The file path displays when you hover over the name of the file.

Import a file into a notebook

You can import a file into a notebook using standard Python import commands:

Suppose you have the following file:

file that defines functions

You can import that file into a notebook and call the functions defined in the file:

import file into notebook

Run a file

You can run a file from the editor. This is useful for testing. To run a file, place your cursor in the code area and select Shift + Enter to run the cell, or highlight code in the cell and press Shift + Ctrl + Enter to run only the selected code.

Delete a file

See Folders and Workspace object operations for information about how to access the workspace menu and delete files or other items in the workspace.

Rename a file

To change the title of an open file, click the title and edit inline or click File > Rename.

Control access to a file

If your Databricks account has the Premium plan or above, you can use Workspace access control to control who has access to a file.