Sub topic 14
Sharing code as files and Python modules
This chapter assumes you can write basic Python, know how Git folders hold notebooks, and have used %run or dbutils.notebook.run to call one notebook from another. Here you move reusable logic out of notebooks and into plain Python files that live in the same Git folder.
Why use files instead of notebooks
%run works, but it copies a notebook's variables into yours and is hard to test. A Python file (a module) gives you normal imports, unit tests, and cleaner code review diffs. Git folders store arbitrary files, not only notebooks, so a module can sit beside the notebooks that use it.
A typical layout
my-repo/
notebooks/
clean_orders.py
src/
helpers/
__init__.py
cleaning.pyThe file src/helpers/cleaning.py holds the shared function:
from pyspark.sql import DataFrame
from pyspark.sql import functions as F
def drop_empty_orders(df: DataFrame) -> DataFrame:
"""Keep only rows that have an order id and a positive amount."""
return df.filter(F.col("order_id").isNotNull() & (F.col("amount") > 0))Importing the module
In a Git folder, Databricks adds the notebook's current working directory to the Python path. It does not add the repository root. A notebook in notebooks/ therefore cannot see src/ until you add it.
import os
import sys
repo_root = os.path.abspath(os.path.join(os.getcwd(), ".."))
if repo_root not in sys.path:
sys.path.append(os.path.join(repo_root, "src"))
from helpers.cleaning import drop_empty_orders
orders = spark.createDataFrame(
[(1, 10.0), (None, 5.0), (3, -1.0)], ["order_id", "amount"]
)
display(drop_empty_orders(orders))Only the first row survives the filter. Checking repo_root not in sys.path before appending keeps repeated runs from adding duplicates.
Reading other files
Non-code files such as small CSV or JSON files can be read with normal Python file APIs, using a path relative to the notebook. Keep them small. Large data belongs in cloud storage or Unity Catalog volumes, not in the repository.
Common errors
- ModuleNotFoundError: the
srcfolder is not onsys.path, or the path is built from the wrong directory. Printos.getcwd()to check. - Stale code after editing a module: Python caches imported modules. Call
importlib.reload(helpers.cleaning)or detach and reattach the notebook to compute. - Missing
__init__.py: newer Python versions can import without it, but adding it makes the package explicit and avoids surprises with some tools. - Different branch, different code: the module you import is the version in the checked-out branch. Switching branches changes what the notebook runs.
Exercise
A notebook in notebooks/ runs from helpers.cleaning import drop_empty_orders and fails with ModuleNotFoundError. The file is at src/helpers/cleaning.py. What is the likely cause and the fix?
Show answer
The src folder is not on the Python path, because Git folders add only the notebook's working directory. Append the absolute path of src to sys.path before the import, for example sys.path.append(os.path.join(repo_root, "src")).