Neptune Client
Neptune is an MLOps platform for experiment tracking and model management, focusing on machine learning metadata. The `neptune-client` Python library provides the interface to log, store, display, and compare MLOps artifacts and metadata directly from your code. It is currently at version 1.14.0.post2, with active development including an upcoming 2.x branch, and releases frequent updates.
Common errors
-
NeptuneMissingApiTokenException: 'NEPTUNE_API_TOKEN' environment variable not found. Please set it or pass `api_token` to `neptune.init_run()`.
cause The Neptune API token is not configured.fixSet the `NEPTUNE_API_TOKEN` environment variable with your personal API token (found in Neptune UI, Account Settings -> API Tokens), or pass `api_token='YOUR_API_TOKEN'` directly to `neptune.init_run()`. -
NeptuneMissingProjectException: 'NEPTUNE_PROJECT' environment variable not found. Please set it or pass `project` to `neptune.init_run()`.
cause The target Neptune project is not specified.fixSet the `NEPTUNE_PROJECT` environment variable to your project's identifier (e.g., 'your_workspace/your_project'), or pass `project='your_workspace/your_project'` directly to `neptune.init_run()`. -
AttributeError: module 'neptune' has no attribute 'init'
cause Attempting to use the old `neptune.init()` function which has been replaced.fixUpdate your code to use `neptune.init_run()` instead of `neptune.init()`.
Warnings
- breaking Python 3.7 is no longer supported. Ensure your environment uses Python 3.8 or newer.
- deprecated The `model` and `model_version` endpoints within the Neptune API are deprecated. Users should migrate to the new `Model` and `ModelVersion` objects.
- gotcha The `neptune.init()` function is deprecated. Using it will lead to warnings and potentially errors in future versions.
- gotcha When uploading HTML objects with `run['path'].upload()` or using `File.as_html()`, the default behavior is to embed the entire Plotly.js library (approx. 3MB) within the HTML. For Neptune SaaS users, this can significantly increase object size.
- gotcha You must set the `NEPTUNE_API_TOKEN` and `NEPTUNE_PROJECT` environment variables or pass them directly to `neptune.init_run()` to connect to your Neptune project. Forgetting to do so is a common cause of connection errors.
Install
-
pip install neptune
Imports
- init_run
import neptune run = neptune.init(...)
import neptune run = neptune.init_run(...)
- Run
from neptune import Run
Quickstart
import neptune
import os
import random
# Initialize a new Neptune run
# Replace 'YOUR_WORKSPACE/YOUR_PROJECT_NAME' with your actual project name, e.g., 'common/quickstart-python'
# Set NEPTUNE_API_TOKEN and NEPTUNE_PROJECT as environment variables for production use.
run = neptune.init_run(
project=os.environ.get("NEPTUNE_PROJECT", "common/quickstart-python"),
api_token=os.environ.get("NEPTUNE_API_TOKEN", "YOUR_NEPTUNE_API_TOKEN"), # Replace with your token if not using env var
name="Minimal Example",
tags=["quickstart", "example"]
)
# Log some parameters
params = {"learning_rate": 0.001, "optimizer": "Adam"}
run["parameters"] = params
# Log some metrics
for i in range(10):
run["train/loss"].append(random.uniform(0.1, 0.9))
run["train/accuracy"].append(random.uniform(0.5, 0.99))
# Log a model checkpoint placeholder (e.g., a path or a link)
run["model_checkpoints/best_model"].track_files("s3://my-bucket/models/best_model.pth")
# Stop the run
run.stop()
print(f"Neptune run URL: {run.get_url()}")