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.
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.Mapping → collections.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.
# stopgap pin
numpy<1.24
# durable fix: use the new API
import collections.abc as cabc # was collections.MappingRule out a shadowing local module
- Run
python -c "import X; print(X.__file__)"to confirm it resolves to the real package. - If
__file__points at your repo, rename the shadowing file. - 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.