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.
pip install codecarbonVerified import paths — ran on the pinned version, not inferred.
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.
Ensure you are using `codecarbon==3.2.5` or later. If you installed 3.2.4, uninstall it and reinstall the latest version: `pip uninstall codecarbon && pip install codecarbon`.
Review the official CodeCarbon documentation for changes to API endpoints, authentication, and configuration files (`.codecarbon.config`). Re-run `codecarbon login` if using the online dashboard.
For the most accurate tracking, consult the CodeCarbon documentation's methodology section to install the recommended power measurement tools for your specific hardware and OS.
If you need to run multiple trackers, initialize `EmissionsTracker` with `allow_multiple_runs=True`. If an error occurs due to a stale lock file, manually delete `/tmp/.codecarbon.lock` (or equivalent on your OS) or restart your environment.
For tracking remote GenAI API call emissions, consider using complementary tools like EcoLogits. CodeCarbon is intended for monitoring code running on your local machine or managed compute infrastructure.
If you intended to run multiple trackers, initialize with `EmissionsTracker(allow_multiple_runs=True)`. Otherwise, ensure no other `codecarbon` process is running or manually delete the lock file (e.g., `/tmp/.codecarbon.lock` on Linux).
Verify your internet connection. Check the `api_endpoint`, `api_key`, `organization_id`, `project_id`, and `experiment_id` in your global `~/.codecarbon.config` or local `.codecarbon.config` file. Re-run `codecarbon login` or `codecarbon config` CLI commands to re-authenticate and configure.
Ensure `codecarbon` is correctly installed: `pip install --upgrade codecarbon`. Verify the import statement: `from codecarbon import EmissionsTracker`. If running in an IDE, restart the Python kernel or environment.
Ensure `tracker.stop()` is called before trying to access `emissions.csv`. The file is saved in the directory where the script is run by default, or to a path specified by the `save_to_file` parameter in `EmissionsTracker`.