SteveOstrowski
Databricks Employee
Databricks Employee

Hi @holunder42,

The behavior you are seeing is expected. The display() function is not a standard Python built-in. It is injected into the notebook's global namespace by the Databricks runtime when a notebook cell executes. When you move code into an imported Python module (.py file), that module has its own namespace and does not automatically inherit display(), spark, dbutils, or other notebook-scoped objects. That is why your module falls back to the standard __repr__ output.

RECOMMENDED APPROACH: PASS DISPLAY AS A PARAMETER

The cleanest pattern is to pass the display function (or any notebook-scoped object) into your module as an argument:

In your module file (e.g., my_utils.py):

def show_data(df, display_fn=None):
  if display_fn is not None:
      display_fn(df)
  else:
      # Fallback for standard Python environments
      print(df.toPandas().to_string())

In your notebook:

from my_utils import show_data
df = spark.table("my_catalog.my_schema.my_table")
show_data(df, display_fn=display)

This keeps your module portable. When running inside a Databricks notebook, you pass the built-in display. When running outside (unit tests, local development), the fallback kicks in.

ALTERNATIVE: LOOK UP DISPLAY AT RUNTIME

You can also detect whether you are running inside a Databricks notebook and grab display from the IPython environment:

def get_display():
  try:
      from IPython.display import display as ipython_display
      # Check if the Databricks-enhanced display is available
      shell = get_ipython()
      if hasattr(shell, 'user_ns') and 'display' in shell.user_ns:
          return shell.user_ns['display']
      return ipython_display
  except Exception:
      return print

def show_data(df):
  display_fn = get_display()
  display_fn(df)

In this approach, get_ipython().user_ns gives you access to the notebook's namespace, which includes the Databricks-enhanced display function that renders rich table output and charts.

ALTERNATIVE: USE IPYTHON.DISPLAY DIRECTLY

If you only need basic rendering (not the full Databricks rich table with chart options), the IPython.display module works from imported modules:

from IPython.display import display, HTML

def show_html(html_string):
  display(HTML(html_string))

This renders HTML output in the notebook cell. However, it does not give you the Databricks-specific table visualization with sorting, filtering, and chart creation. For that, you need the notebook-scoped display function.

SUMMARY

- display() is notebook-scoped, not available by default in imported modules
- Best practice: pass display as an argument to your module functions
- Runtime lookup via get_ipython().user_ns['display'] also works
- IPython.display provides basic rendering but not the full Databricks visualization

Docs reference for working with Python modules in notebooks:
https://docs.databricks.com/en/files/workspace-modules.html

Docs reference for IPython kernel support in Databricks:
https://docs.databricks.com/en/notebooks/ipython-kernel.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.