wandb.errors.UsageError
wandb.errors.UsageError: You must run `wandb login` before using wandb in non-interactive mode
Stack trace
wandb.errors.UsageError: You must run `wandb login` before using wandb in non-interactive mode
File "/usr/local/lib/python3.9/site-packages/wandb/sdk/wandb_init.py", line 123, in _init
raise UsageError("You must run `wandb login` before using wandb in non-interactive mode") Why it happens
wandb requires an API key to authenticate users before logging runs. In non-interactive environments like CI/CD or servers without a terminal, the automatic login prompt cannot run, causing this error if no API key is pre-configured.
Detection
Detect this error by catching wandb.errors.UsageError during wandb.init() calls and checking for the login prompt message in logs before failure.
Causes & fixes
No wandb API key configured in environment or config file in a non-interactive environment
Set the WANDB_API_KEY environment variable with your API key before running your script, e.g. export WANDB_API_KEY='your_api_key'
Running wandb.init() in a CI/CD pipeline or server without prior login
Use `wandb login --relogin` in a setup step or configure the API key environment variable in the pipeline before execution
Attempting to login interactively in a headless or non-interactive environment
Avoid interactive login prompts by pre-setting the API key or using wandb.login(key='your_api_key') programmatically
Code: broken vs fixed
import wandb
wandb.init(project="my-project") # triggers UsageError in non-interactive env without login import os
import wandb
os.environ["WANDB_API_KEY"] = "your_api_key_here" # Set API key to avoid login error
wandb.init(project="my-project") # now works without interactive login Workaround
Wrap wandb.init() in try/except wandb.errors.UsageError, then programmatically call wandb.login(key=os.environ.get('WANDB_API_KEY')) if available to authenticate before retrying.
Prevention
Always configure the WANDB_API_KEY environment variable or use wandb.login(key=...) in your deployment pipelines and non-interactive environments to ensure authentication without manual login.