Skip to content
Latchkey

Python "ZeroDivisionError: division by zero" in CI

Dividing by zero raises ZeroDivisionError. In CI this typically appears when a denominator derived from a count, length, or total is zero because the input set was empty.

What this error means

A test or metric calculation fails with "ZeroDivisionError: division by zero" on an average, rate, or percentage computation.

python
    avg = total / len(values)
ZeroDivisionError: division by zero

Common causes

The denominator is an empty count

A len(...), sum, or count evaluated to zero because no rows matched, then code divided by it.

A configured rate or step was left at zero

A divisor read from config defaulted to 0 in the CI environment.

How to fix it

Guard the denominator

  1. Return a sensible default (0, None) when the denominator is zero instead of dividing.
  2. Validate config-supplied divisors are non-zero at load time.
  3. Add a test for the empty-input case so the guard is covered.
Python
avg = total / len(values) if values else 0.0

How to prevent it

  • Always handle the empty-collection case in aggregate math.
  • Validate any config divisor is non-zero before use.
  • Add explicit tests for zero-count edge cases.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →