The Code/Workflows CodeCarbon

Workflow

Measure the CO₂ your code emits with CodeCarbon

CodeCarbon is one of the projects the Mozilla Foundation funds.

It’s an open-source Python package that measures the electricity your CPU, GPU, and RAM use while your code runs, then factors in the carbon intensity of your local power grid. It works on a laptop, a workstation, or a cloud VM, so you can compare two models, test different regions, or benchmark a job before and after optimizing it.

Set up CodeCarbon 3 steps · Python

1Install the package

pip install codecarbon

2Wrap the code you want to measure with the tracker

from codecarbon import EmissionsTracker

tracker = EmissionsTracker()
tracker.start()

# Your code here

emissions = tracker.stop()
print(f"Emissions: {emissions} kg CO₂")

3Run your script as usual

When it finishes, it prints the emissions in kilograms of CO₂.