ImportError: cannot import name 'bool' from 'numpy'
Fixes ImportError: cannot import name 'bool' from 'numpy' Use when the traceback shows `from numpy import bool` (or int, float, complex, object, unicode, str) and you run numpy 1.24 or newer Not for: the error is `ModuleNotFoundError: No module named 'numpy'` (numpy is not installed) or a binary/ABI mismatch error
How to fix ImportError: cannot import name 'bool' from 'numpy'
Stop importing the alias. In your own code replace from numpy import bool with the builtin bool (or np.bool_ if you need the numpy scalar type). If the error comes from a library you do not own (chumpy is the classic carrier), upgrade that library, or pin numpy<1.24 as a stopgap. numpy 1.24 removed the old aliases np.bool, np.int, np.float and friends for good.
The exact error
ImportError: cannot import name 'bool' from 'numpy'Fix it
- Find the bad import:
grep -rn "from numpy import" --include=*.py .and look for bool, int, float, complex, object, unicode, str.
Expected: you see the exact line raising the error
- In your own code, swap the alias for the builtin:
from numpy import boolgoes away, just usebool. Only keep numpy'snp.bool_if you truly need the numpy scalar.
Expected: import succeeds on numpy>=1.24
- If the traceback points inside a third-party package (chumpy is the classic), upgrade that package; if no fixed release exists, pin
numpy<1.24in your requirements.
Expected: pip shows numpy 1.23.x and the import works
When this applies
the traceback shows from numpy import bool (or int, float, complex, object, unicode, str) and you run numpy 1.24 or newer
When this does NOT apply
the error is ModuleNotFoundError: No module named 'numpy' (numpy is not installed) or a binary/ABI mismatch error
Versions
numpy >= 1.24, Python 3.8+
Why it happens
numpy deprecated these aliases in 1.20 and removed them in 1.24. Old code and old libraries still import them.
Edge cases
np.bool_ still exists and is fine to use. Pinning numpy<1.24 is a band-aid; the real fix is updating the importing code.
Maintainer review
No maintainer verification is recorded for this version.
This records the version a maintainer checked. It does not assert that the version is the latest upstream release.