---
title: Databricks Utilities with Databricks Connect for Python
description: Learn how to use Databricks Utilities with Databricks Connect for Python. Databricks Connect allows you to connect popular applications to Databricks clusters.
canonical_url: https://docs.databricks.com/aws/en/dev-tools/databricks-connect/python/databricks-utilities
---

# Databricks Utilities with Databricks Connect for Python

> [!NOTE]
>
> This article covers Databricks Connect for Databricks Runtime 13.3 LTS and above.

This article describes how to use [Databricks Utilities](https://docs.databricks.com/aws/en/dev-tools/databricks-utils.md) with Databricks Connect for Python. Databricks Connect enables you to connect popular IDEs, notebook servers, and custom applications to Databricks clusters. See [Databricks Connect](https://docs.databricks.com/aws/en/dev-tools/databricks-connect/index.md).

Before you begin to use Databricks Connect, you must [set up the Databricks Connect client](https://docs.databricks.com/aws/en/dev-tools/databricks-connect/python/install.md).

For the Scala version of this article, see [Databricks Utilities with Databricks Connect for Scala](https://docs.databricks.com/aws/en/dev-tools/databricks-connect/scala/databricks-utilities.md).

## Available Databricks Utilities

You use Databricks Connect to access Databricks Utilities as follows:

* Use the `WorkspaceClient` class's `dbutils` variable to access Databricks Utilities. The `WorkspaceClient` class belongs to the [Databricks SDK for Python](https://pypi.org/project/databricks-sdk) and is included in Databricks Connect.
* Use `dbutils.fs` to access the Databricks Utilities [fs](https://docs.databricks.com/aws/en/dev-tools/databricks-utils.md#file-system-utility-dbutilsfs) utility.
* Use `dbutils.secrets` to access the Databricks Utilities [secrets](https://docs.databricks.com/aws/en/dev-tools/databricks-utils.md#secrets-utility-dbutilssecrets) utility.
* Use `dbutils.widgets` to create and read local widgets, so that code written for the Databricks Utilities [widgets](https://docs.databricks.com/aws/en/dev-tools/databricks-utils.md#dbutils-widgets) utility runs locally. These widgets exist only in your local session and can't retrieve remote notebook or job task parameters. Reading a widget that you haven't created locally raises a `KeyError`, or returns the default value if you use `getArgument`.
* Use `dbutils.jobs.taskValues` to locally debug code written for the Databricks Utilities [jobs](https://docs.databricks.com/aws/en/dev-tools/databricks-utils.md#dbutils-jobs) utility. Task values aren't exchanged with remote job runs: `set` does nothing, and `get` returns the value of its `debugValue` argument, or raises an error if you don't pass `debugValue`. To set or get task values for downstream tasks, run your code as a job task.

No Databricks Utilities functionality other than the preceding utilities is available through `dbutils`.

> [!TIP]
>
> You can also use the included Databricks SDK for Python to access any available Databricks REST API, not just the preceding Databricks Utilities APIs. See [databricks-sdk](https://pypi.org/project/databricks-sdk) on PyPI.

## Intialize the WorkspaceClient

To initialize `WorkspaceClient`, you must provide enough information to authenticate an Databricks SDK with the workspace. For example, you can:

* Hard-code the workspace URL and your access token directly within your code, and then initialize `WorkspaceClient` as follows. Although this option is supported, Databricks **does not recommend** this option, as it can expose sensitive information, such as access tokens, if your code is checked into version control or otherwise shared:

  ```python
  from databricks.sdk import WorkspaceClient

  w = WorkspaceClient(host  = f"https://{retrieve_workspace_instance_name()}",
                      token = retrieve_token())
  ```

* Create or specify a [configuration profile](https://docs.databricks.com/aws/en/dev-tools/auth/config-profiles.md) that contains the fields `host` and `token`, and then initialize the `WorkspaceClient` as follows:

  ```python
  from databricks.sdk import WorkspaceClient

  w = WorkspaceClient(profile = "<profile-name>")
  ```

* Set the environment variables `DATABRICKS_HOST` and `DATABRICKS_TOKEN` in the same way you set them for Databricks Connect, and then initialize `WorkspaceClient` as follows:

  ```python
  from databricks.sdk import WorkspaceClient

  w = WorkspaceClient()
  ```

The Databricks SDK for Python does not recognize the `SPARK_REMOTE` environment variable for Databricks Connect.

For additional Databricks authentication options for the Databricks SDK for Python, as well as how to initialize `AccountClient` within the Databricks SDKs to access available Databricks REST APIs at the account level instead of at the workspace level, see [databricks-sdk](https://pypi.org/project/databricks-sdk) on PyPI.

## Example: Create a file in a volume

The following example shows how to use the Databricks SDK for Python to automate Databricks Utilities. This example creates a file named `zzz_hello.txt` in a Unity Catalog volume's path within the workspace, reads the data from the file, and then deletes the file. This example assumes that the environment variables `DATABRICKS_HOST` and `DATABRICKS_TOKEN` have already been set:

```python
from databricks.sdk import WorkspaceClient

w = WorkspaceClient()

file_path = "/Volumes/main/default/my-volume/zzz_hello.txt"
file_data = "Hello, Databricks!"
fs = w.dbutils.fs

fs.put(
  file      = file_path,
  contents  = file_data,
  overwrite = True
)

print(fs.head(file_path))

fs.rm(file_path)
```

See also [Interaction with dbutils](https://databricks-sdk-py.readthedocs.io/en/latest/dbutils.html) in the Databricks SDK for Python documentation.
