Implementation:Mlflow Mlflow Set Tracking Uri and Set Experiment
| Knowledge Sources | |
|---|---|
| Domains | ML_Ops, Experiment_Tracking |
| Last Updated | 2026-02-13 20:00 GMT |
Overview
Concrete tool for configuring the MLflow tracking destination and selecting the active experiment, provided by the MLflow library.
Description
mlflow.set_tracking_uri and mlflow.set_experiment are companion functions in the MLflow fluent API that together establish the tracking environment for a session. set_tracking_uri sets the global tracking server URI, determining where all subsequent tracking data (runs, metrics, parameters, artifacts) will be persisted. set_experiment selects or creates the experiment under which new runs will be grouped. Both functions propagate their settings through environment variables (MLFLOW_TRACKING_URI and MLFLOW_EXPERIMENT_ID) so that child processes inherit the same configuration.
Usage
Call set_tracking_uri at the beginning of a script or notebook to point to a local directory, a remote tracking server, or a Databricks workspace. Then call set_experiment to select the experiment by name or ID. If the named experiment does not exist, it will be created automatically. These calls must precede any start_run invocation for the configuration to take effect.
Code Reference
Source Location
- Repository: mlflow
- File (set_tracking_uri):
mlflow/tracking/_tracking_service/utils.py - Lines (set_tracking_uri): L73-124
- File (set_experiment):
mlflow/tracking/fluent.py - Lines (set_experiment): L137-237
Signature
def set_tracking_uri(uri: str | Path) -> None: ...
def set_experiment(
experiment_name: str | None = None,
experiment_id: str | None = None,
) -> Experiment: ...
Import
import mlflow
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| uri | str or Path | Yes (for set_tracking_uri) | Tracking server URI. Accepts a local file path (optionally prefixed with file:/), an HTTP/HTTPS URI (e.g. https://my-server:5000), the string "databricks", or a pathlib.Path instance.
|
| experiment_name | str or None | One of name/id required | Case-sensitive name of the experiment to activate. If the experiment does not exist, it will be created. |
| experiment_id | str or None | One of name/id required | ID of an existing experiment to activate. Raises an exception if no experiment with this ID exists. |
Outputs
| Name | Type | Description |
|---|---|---|
| (set_tracking_uri) | None | Sets the global _tracking_uri variable and the MLFLOW_TRACKING_URI environment variable. Also resets the tracer provider.
|
| (set_experiment) | mlflow.entities.Experiment | Returns an Experiment entity representing the now-active experiment. Sets the global _active_experiment_id and the MLFLOW_EXPERIMENT_ID environment variable.
|
Usage Examples
Basic Usage
import mlflow
# Point tracking to a remote server
mlflow.set_tracking_uri("https://my-tracking-server:5000")
# Select (or create) an experiment by name
experiment = mlflow.set_experiment("My Classification Experiment")
print(f"Experiment ID: {experiment.experiment_id}")
print(f"Artifact Location: {experiment.artifact_location}")
print(f"Lifecycle Stage: {experiment.lifecycle_stage}")
# All subsequent runs will be tracked under this experiment
with mlflow.start_run():
mlflow.log_param("model_type", "random_forest")
Local File Store
import mlflow
# Use a local directory for tracking
mlflow.set_tracking_uri("file:///tmp/my_tracking")
# Select experiment by name; created automatically if absent
mlflow.set_experiment("Local Experiment")
Databricks Workspace
import mlflow
# Connect to a Databricks workspace
mlflow.set_tracking_uri("databricks")
# Experiment paths on Databricks must be absolute
mlflow.set_experiment("/Users/user@example.com/my-experiment")