How to install Weights and Biases
Quick answer
Install Weights and Biases by running
pip install wandb in your Python environment. Then, initialize it in your code with import wandb and wandb.init() to start tracking experiments.PREREQUISITES
Python 3.8+pip package managerInternet connection for package download
Setup
Install the Weights and Biases Python SDK using pip. This package enables experiment tracking, logging, and visualization for machine learning projects.
Set your API key via environment variable WANDB_API_KEY or login interactively after installation.
pip install wandb Step by step
Here is a minimal example to install and initialize Weights and Biases in a Python script to track a simple metric.
import wandb
# Initialize a new run
wandb.init(project="my-project")
# Log a metric
for epoch in range(3):
wandb.log({"epoch": epoch, "accuracy": 0.8 + epoch * 0.05})
print("Run completed and logged to W&B.") output
Run completed and logged to W&B.
Common variations
- Use
wandb.login()to authenticate interactively if you don't want to set the API key as an environment variable. - Integrate with popular ML frameworks like PyTorch or TensorFlow by adding
wandb.watch(model)to log gradients and parameters. - Run asynchronously or in distributed training setups by configuring
wandb.init()with appropriate parameters.
Troubleshooting
- If you see
wandb: ERROR API key not found, set your API key withexport WANDB_API_KEY=your_api_keyon Linux/macOS orsetx WANDB_API_KEY your_api_keyon Windows. - For network issues, ensure your firewall allows outbound HTTPS traffic to
api.wandb.ai. - If
pip install wandbfails, upgradepipwithpip install --upgrade pipand retry.
Key Takeaways
- Use
pip install wandbto install the Weights and Biases SDK quickly. - Set your API key via the
WANDB_API_KEYenvironment variable for seamless authentication. - Initialize tracking in your Python code with
wandb.init()and log metrics withwandb.log(). - Use
wandb.login()for interactive authentication if preferred over environment variables. - Troubleshoot common errors by verifying API key setup and network connectivity.