Skip to content
Latchkey

Python "AttributeError: module X has no attribute Y" from Version Skew

The module imported fine, but the attribute the code reads is gone or moved. Almost always a dependency drifted to a version where that API was renamed, relocated, or removed.

What this error means

Code that worked before now raises AttributeError: module X has no attribute Y in CI, even though import X succeeds. Pinning the dependency back to the old version makes the attribute reappear - confirming version skew.

Python traceback
AttributeError: module 'collections' has no attribute 'Mapping'
# or
AttributeError: module 'numpy' has no attribute 'float'

Common causes

An upgraded dependency removed or moved the API

A newer release dropped a deprecated attribute (e.g. numpy.float, collections.Mappingcollections.abc.Mapping). Unpinned deps drifted into the breaking version.

A name shadowed by a local module or file

A local file named like the import (e.g. numpy.py) shadows the real package, so the "module" has none of the real attributes.

How to fix it

Pin a compatible version and migrate the code

Pin to a release that still has the attribute, then update the code to the new API and lift the pin.

Python / requirements.txt
# stopgap pin
numpy<1.24
# durable fix: use the new API
import collections.abc as cabc   # was collections.Mapping

Rule out a shadowing local module

  1. Run python -c "import X; print(X.__file__)" to confirm it resolves to the real package.
  2. If __file__ points at your repo, rename the shadowing file.
  3. Avoid naming local modules after third-party packages.

How to prevent it

  • Pin or range-constrain dependencies so a transitive upgrade can’t remove an API.
  • Read deprecation warnings and migrate before the attribute is removed.
  • Never name local files after packages you import.

Related guides

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