ImportError: cannot import name 'trapz' from 'scipy.integrate'
Third-person routing text. Fixes ImportError for trapz from scipy.integrate: scipy 1.14 renamed it to trapezoid. Import trapezoid (optionally aliased as trapz) or pin scipy below 1.14. Use when an agent hits this on code written for older scipy. Not for other scipy renames.
TL;DR: scipy 1.14 removed the old trapz name; it is now called trapezoid. Change your import to from scipy.integrate import trapezoid (alias it as trapz if you like) or pin scipy below 1.14.
The error
ImportError: cannot import name 'trapz' from 'scipy.integrate'Fix it
- Update the import (keeps working on old scipy too if you alias):
from scipy.integrate import trapezoid as trapzExpected: existing trapz(...) call sites keep working.
- Or use the new name directly:
from scipy.integrate import trapezoid- Verify:
python -c "from scipy.integrate import trapezoid; print(trapez([1,2,3]))"Expected: prints 4.0.
- Stopgap if you cannot edit the code:
pip install "scipy<1.14". Expected: old import works again.
When this applies
Use this when code written for older scipy does from scipy.integrate import trapz on scipy 1.14+.
When it does not apply
If the error names cumtrapz, its new name is cumulative_trapezoid. If scipy itself fails to import, reinstall scipy.
Compatibility
scipy 1.14+. trapezoid exists since scipy 1.6, so the aliased import works on scipy 1.6 through 2.x.
Root cause
scipy renamed trapz to trapezoid for a clearer API and kept trapz as a deprecated alias for years before removing it in 1.14. Code that never migrated breaks at import.
Edge cases
The sibling cumtrapz was renamed cumulative_trapezoid in the same cleanup; check for it in the same files. Libraries should use the try/except import pattern to support both eras.