CodeCarbon

3.2.6 · active · verified Thu Apr 16

CodeCarbon is an open-source Python library that helps estimate and track the CO2 emissions produced by computing, particularly for machine learning models. It measures energy consumption of CPU, GPU, and RAM, and combines it with regional carbon intensity data to provide CO2 equivalent emissions. Currently at version 3.2.6, it maintains an active development and release cadence, offering both Python API and CLI for monitoring and reporting.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use the `EmissionsTracker` class to measure the carbon footprint of a Python code block. It initializes a tracker, starts it before the task, stops it after, and prints the total emissions. A detailed report is saved to `emissions.csv` by default.

import time
from codecarbon import EmissionsTracker

def train_model():
    # Simulate some computationally intensive task
    print("Starting model training...")
    time.sleep(10) # Your actual ML code here
    print("Model training finished.")

if __name__ == "__main__":
    tracker = EmissionsTracker()
    tracker.start()
    train_model()
    emissions = tracker.stop()
    print(f"Emissions: {emissions} kg CO₂")
    print("Check emissions.csv for detailed report.")

view raw JSON →