Fix module ‘numpy’ has no attribute ‘bool’

The error AttributeError: module 'numpy' has no attribute 'bool' usually means that code in your project, or in one of your installed packages, still uses the old np.bool alias in a NumPy version where that alias is not available.

The safest fix is usually simple: replace np.bool with Python’s built-in bool, or use np.bool_ when you specifically need the NumPy scalar boolean type. If the line appears inside a third-party package, update that package first.

Why this error happens

For years, np.bool was an alias for Python’s built-in bool. NumPy deprecated aliases such as np.bool, np.int, and np.float in NumPy 1.20. The NumPy 1.20 release notes explain that replacing these aliases with the built-in types works the same for ordinary use.

In NumPy 1.24, that deprecation expired. The NumPy 1.24 release notes state that aliases including np.bool and np.int now raise errors. That is why old code can suddenly break after a NumPy upgrade.

There is one modern nuance: NumPy 2.0 introduced np.bool again as the canonical NumPy boolean dtype, with np.bool_ as an alias. The NumPy 2.0 release notes describe this change for Array API compatibility. For code that must run across NumPy 1.24, 1.26, and 2.x, bool and np.bool_ are still the safest choices.

Quick fix in your own code

If the stack trace points to your file, change this:

import numpy as np

mask = np.array([True, False, True], dtype=np.bool)

To this:

import numpy as np

mask = np.array([True, False, True], dtype=bool)

Using dtype=bool is correct for normal boolean arrays. NumPy will create an array with a boolean dtype.

Use np.bool_ when you need the NumPy scalar type

If you specifically want NumPy’s scalar boolean type, use np.bool_. NumPy’s scalar type documentation lists bool_ as the NumPy boolean scalar type.

import numpy as np

value = np.bool_(True)
print(value)
print(type(value))

Output:

True
<class 'numpy.bool'>

Depending on your NumPy version, the displayed class name may use the newer canonical name, but np.bool_ remains the reliable spelling for compatibility.

Fix dtype conversion with astype(bool)

If the old code uses astype(np.bool), replace it with astype(bool) or astype(np.bool_).

import numpy as np

numbers = np.array([0, 1, 2, 0])
mask = numbers.astype(bool)
print(mask)

Output:

[False  True  True False]

The official ndarray.astype() documentation covers dtype conversion for arrays.

If the error comes from another package

Read the full traceback and find the first line that belongs to an installed package instead of your project. If an old version of TensorFlow, MXNet, Theano, PyMC, scikit-learn, or another library is still calling np.bool, update that library.

python -m pip install --upgrade numpy
python -m pip install --upgrade package-name

For Conda environments, prefer Conda-compatible updates when the package was installed through Conda:

conda update numpy
conda update package-name

Do not downgrade NumPy as the first fix. Pinning NumPy to an older version can hide the error, but it may create dependency conflicts or security and compatibility problems. Use a temporary pin only when you must run an old unmaintained package and you understand the environment tradeoff.

Check your NumPy version

Use this command inside the same environment that runs your script:

import numpy as np

print(np.__version__)

If your terminal shows one NumPy version and your IDE shows another, you are probably using different Python environments. Create a clean virtual environment and install the packages there.

python -m venv .venv
source .venv/bin/activate
python -m pip install --upgrade pip numpy

On Windows PowerShell, activate the environment with:

.venvScriptsActivate.ps1

Common replacements

Old code Preferred replacement Use when
np.bool bool You need the normal Python boolean type or array dtype.
np.bool np.bool_ You specifically need the NumPy scalar boolean type.
astype(np.bool) astype(bool) You are converting an array to a boolean mask.
dtype=np.bool dtype=bool You are creating a boolean NumPy array.

Related AttributeError and NumPy guides

For general troubleshooting, see our guide to Python AttributeError. For nearby NumPy and Pandas fixes, read module ‘pandas’ has no attribute ‘dataframe’, NumPy array to Pandas DataFrame, NumPy allclose(), NumPy squeeze(), and NumPy cross().

Conclusion

To fix module 'numpy' has no attribute 'bool', replace old np.bool usage with bool for ordinary boolean arrays or np.bool_ for the NumPy scalar type. If the error comes from a dependency, update that dependency. Only pin NumPy to an old version as a temporary compatibility workaround for legacy projects.

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted