Sunday, November 30, 2025

How to access a DataBricks workspace from MLFlow ?

pip install --upgrade "mlflow[databricks]>=3.1"


Step 2: Create an MLflow Experiment

Open your Databricks workspace

Go to Experiments in the left sidebar under Machine Learning

At the top of the Experiments page, click on New Experiment


Step 3: Configure Authentication

Choose one of the following authentication methods:


Option A: Environment Variables


In your MLflow Experiment, click Generate API Key

Copy and run the generated code in your terminal:

bash


export DATABRICKS_TOKEN=<databricks-personal-access-token>

export DATABRICKS_HOST=https://<workspace-name>.cloud.databricks.com

export MLFLOW_TRACKING_URI=databricks

export MLFLOW_EXPERIMENT_ID=<experiment-id>



Option B: .env File


In your MLflow Experiment, click Generate API Key

Copy the generated code to a .env file in your project root:

bash


DATABRICKS_TOKEN=<databricks-personal-access-token>

DATABRICKS_HOST=https://<workspace-name>.cloud.databricks.com

MLFLOW_TRACKING_URI=databricks

MLFLOW_EXPERIMENT_ID=<experiment-id>


Install the python-dotenv package:

bash


pip install python-dotenv

Load environment variables in your code:

python


# At the beginning of your Python script

from dotenv import load_dotenv


# Load environment variables from .env file

load_dotenv()



Step 4: Verify Your Connection

Create a test file and run this code to verify your connection:


python


import mlflow


# Test logging to verify connection

print(f"MLflow Tracking URI: {mlflow.get_tracking_uri()}")

with mlflow.start_run():

    print("✓ Successfully connected to MLflow!")


No comments:

Post a Comment