SteveOstrowski
Databricks Employee
Databricks Employee

Hi @ChristianRRL,

Your follow-up solution is spot on. To summarize and add some context for others who land here:

THE ROOT CAUSE

The ModuleNotFoundError you initially saw happens because the Workspace filesystem (/Workspace/...) does not fully support certain write operations that Python and pytest rely on by default. Specifically:

1. pytest's assertion rewriting mechanism tries to write compiled .pyc files into __pycache__ directories, which fails on the Workspace filesystem.
2. The cache provider plugin tries to write a .pytest_cache folder, which also fails.

When either of these writes fails, pytest cannot properly collect and import your test modules, which surfaces as the misleading "ModuleNotFoundError: No module named 'test_my_functions'" error.

THE FIX

As you discovered, two settings resolve this. The official Databricks documentation for running pytest in notebooks actually recommends both of these:

import pytest
import sys

# Prevent Python from writing .pyc files to the read-only workspace filesystem
sys.dont_write_bytecode = True

# Run pytest with assertion rewriting disabled and cache provider off
retcode = pytest.main([".", "-v", "--assert=plain", "-p", "no:cacheprovider"])

assert retcode == 0, "The pytest invocation failed. See the log for details."

Here is what each setting does:

sys.dont_write_bytecode = True
Prevents Python from creating __pycache__/*.pyc files.

--assert=plain
Disables pytest's assertion rewriting, which also requires writing .pyc files.

-p no:cacheprovider
Disables the .pytest_cache directory that pytest uses to track test state between runs.

WHY SERVERLESS WORKS WITHOUT THESE FLAGS

On Serverless compute, the "Run tests" button uses a built-in test runner that is already configured to handle these filesystem constraints. That is why your tests passed on Serverless without any extra arguments.

ADDITIONAL TIPS FOR PYTEST IN DATABRICKS

1. sys.path for Workspace folders: As another commenter mentioned, if your source code is in a regular Workspace folder (not a Git folder/Repo), you may need to add its path to sys.path so pytest can find your modules:

import sys
sys.path.append("/Workspace/Users/your_email/your_project")

For code stored in Git folders (Repos), this is handled automatically on Runtime 14.3 and above.

2. Organize tests alongside source files: Databricks recommends storing functions and tests as Python files (.py) rather than inside notebooks, since test frameworks work best with standard Python files.

3. Documentation reference: The Databricks docs have a full walkthrough of running pytest in notebooks here:
https://docs.databricks.com/en/notebooks/testing.html

* This reply used an agent system I built to research and draft this response based on the wide set of documentation I have available and previous memory. I personally review the draft for any obvious issues and for monitoring system reliability and update it when I detect any drift, but there is still a small chance that something is inaccurate, especially if you are experimenting with brand new features.

If this answer resolves your question, could you mark it as "Accept as Solution"? That helps other users quickly find the correct fix.