Skip to main content

How to Set the MLflow Tracking URI in Python

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

The tracking URI tells your code where MLflow records go. Get it wrong and nothing fails: MLflow quietly writes your runs somewhere else, and your UI stays empty. This guide shows the two ways to set it, which one wins, how to check it, and exactly what MLflow 3.18 does when you don't set it, with real output.

Syntax​

Syntax
import mlflow

mlflow.set_tracking_uri("<URI>")
print(mlflow.get_tracking_uri())

Or, for every MLflow program you start from a terminal, the environment variable:

Syntax
MLFLOW_TRACKING_URI=<URI>
PartWhat it means
mlflow.set_tracking_uri(uri)Sends everything this program logs to uri
mlflow.get_tracking_uri()Returns the tracking URI MLflow is using right now
MLFLOW_TRACKING_URIThe same setting, read from the environment. A set_tracking_uri() call in your code wins over it
<URI>Where the records go: a tracking server, such as http://127.0.0.1:8080, or a database file, such as sqlite:///mlflow.db

Prerequisites​

Step 1: See the Default​

Save this as show_tracking_uri.py:

show_tracking_uri.py
import mlflow

print("Tracking URI:", mlflow.get_tracking_uri())

Run it with nothing set:

Terminal
uv run python show_tracking_uri.py
Output
Tracking URI: sqlite:////Users/you/source/mlflow-demo/mlflow.db

With no tracking URI set, MLflow 3.18 uses a SQLite file called mlflow.db in the folder you run from. Not your server.

Step 2: Set It with an Environment Variable​

MLflow reads MLFLOW_TRACKING_URI by itself:

Terminal
MLFLOW_TRACKING_URI=http://127.0.0.1:8080 uv run python show_tracking_uri.py
Output
Tracking URI: http://127.0.0.1:8080

The variable works for every MLflow program in that terminal, including the mlflow command line.

Step 3: Set It in Code​

set_tracking_uri.py
import mlflow

mlflow.set_tracking_uri("http://127.0.0.1:8080")
print("Tracking URI:", mlflow.get_tracking_uri())
Terminal
uv run python set_tracking_uri.py
Output
Tracking URI: http://127.0.0.1:8080

Step 4: Know Which One Wins​

Set both, with different ports: the variable says 9999, the code says 8080.

Terminal
MLFLOW_TRACKING_URI=http://127.0.0.1:9999 uv run python set_tracking_uri.py
Output
Tracking URI: http://127.0.0.1:8080

A terminal showing four tracking URI checks: the default sqlite:////Users/you/source/mlflow-demo/mlflow.db with nothing set, http://127.0.0.1:8080 from the environment variable, http://127.0.0.1:8080 from set_tracking_uri, and set_tracking_uri winning over a variable set to port 9999.

The order, from strongest to weakest:

  1. mlflow.set_tracking_uri() in your code;
  2. the MLFLOW_TRACKING_URI environment variable;
  3. the default, sqlite:///mlflow.db in the current folder.

A good pattern for real projects is to read the address from settings, so each computer can set its own, and pass it to set_tracking_uri(). MLflow Tutorial for Machine Learning in Python — Part 1 does exactly that with pydantic-settings.

The Trap: Runs That Never Reach Your Server​

This script logs a run without setting a tracking URI:

log_without_uri.py
import mlflow

with mlflow.start_run() as run:
mlflow.log_param("color", "blue")

print("Logged run", run.info.run_id)
print("It went to:", mlflow.get_tracking_uri())
Terminal
uv run python log_without_uri.py
Output
2026/11/13 17:06:34 INFO mlflow.store.db.utils: Creating initial MLflow database tables...
2026/11/13 17:06:34 INFO mlflow.store.db.utils: Updating database tables
Logged run b3072508b3af4707a001b40d46cf6f12
It went to: sqlite:////Users/you/source/mlflow-demo/mlflow.db

No error, no warning. MLflow created a brand-new mlflow.db in the project folder and put the run there, so it never appears in your server's UI. The two Creating initial MLflow database tables lines are the only clue. If your runs seem to vanish, check mlflow.get_tracking_uri() first, and keep those files out of Git: How to Keep MLflow Files Out of Git with .gitignore.

Common Mistakes​

MistakeWhat happensDo this instead
Setting no tracking URIRuns go to a new mlflow.db in the current folderSet it in code or with MLFLOW_TRACKING_URI
Calling set_tracking_uri() inside a run that's already startedThe run was created at the old address, so the next call fails at the new one: RestException: RESOURCE_DOES_NOT_EXIST: Run with id=… not foundSet the URI first, before anything else MLflow does
A wrong port in the URIMLflow retries for about four minutes, then failsMatch the address the server prints: Fixed: "MlflowException: API request to … failed with exception … Max retries exceeded" Connection Refused in MLflow
Expecting the variable in one terminal to apply in anotherEach terminal has its own variablesSet it in the terminal that runs the code, or in a settings file

Troubleshooting​

SymptomLikely causeFix
Creating initial MLflow database tables when your code runsNo tracking URI, so MLflow made a local databaseSet the URI
The UI shows only Default after a runThe run went to another URIPrint mlflow.get_tracking_uri() in your code

FAQ​

What does a tracking URI look like?​

For a server, http://host:port, such as http://127.0.0.1:8080. For a local database without a server, sqlite:///mlflow.db. Other schemes include postgresql://… and databricks.

Why four slashes in sqlite:////Users/…?​

sqlite:/// is the scheme, and the path itself starts with /. Three plus one.

Should I use localhost or 127.0.0.1?​

127.0.0.1, the address the server prints. On a Mac, localhost can resolve to the IPv6 address first, which caused trouble on port 5000: Fixed: "Access to localhost was denied" HTTP ERROR 403 When You Open the MLflow UI on a Mac.

Conclusion​

With nothing set, MLflow 3.18 used sqlite:///mlflow.db in the current folder and silently logged a run there. MLFLOW_TRACKING_URI pointed every MLflow program in the terminal at the server, mlflow.set_tracking_uri() did it in code and won over the variable, and mlflow.get_tracking_uri() always tells you which address is in use.

Additional Resources​