How to solve AttributeError: module 'numpy' has no attribute 'bool'?

I'm using a conda environment with Python version 3.9.7, pip 22.3.1, numpy 1.24.0, gluoncv 0.10.5.post0, mxnet 1.7.0.post2

from gluoncv import data, utils gives the error:

C:\Users\std\anaconda3\envs\myenv\lib\site-packages\mxnet\numpy\utils.py:37: FutureWarning: In the future `np.bool` will be defined as the corresponding NumPy scalar. (This may have returned Python scalars in past versions bool = onp.bool --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) Cell In[1], line 3 1 #import cv2 2 #import os ----> 3 from gluoncv import data, utils #does not work File ~\anaconda3\envs\myenv\lib\site-packages\gluoncv\__init__.py:16 14 _found_mxnet = _found_pytorch = False 15 try: ---> 16 _require_mxnet_version('1.4.0', '2.0.0') 17 from . import data 18 from . import model_zoo File ~\anaconda3\envs\myenv\lib\site-packages\gluoncv\check.py:6, in _require_mxnet_version(mx_version, max_mx_version) 4 def _require_mxnet_version(mx_version, max_mx_version='2.0.0'): 5 try: ----> 6 import mxnet as mx 7 from distutils.version import LooseVersion 8 if LooseVersion(mx.__version__) < LooseVersion(mx_version) or \ 9 LooseVersion(mx.__version__) >= LooseVersion(max_mx_version): File ~\anaconda3\envs\myenv\lib\site-packages\mxnet\__init__.py:33 30 # version info 31 __version__ = base.__version__ ---> 33 from . import contrib 34 from . import ndarray 35 from . import ndarray as nd File ~\anaconda3\envs\myenv\lib\site-packages\mxnet\contrib\__init__.py:30 27 from . import autograd 28 from . import tensorboard ---> 30 from . import text 31 from . import onnx 32 from . import io File ~\anaconda3\envs\myenv\lib\site-packages\mxnet\contrib\text\__init__.py:23 21 from . import utils 22 from . import vocab ---> 23 from . import embedding File ~\anaconda3\envs\myenv\lib\site-packages\mxnet\contrib\text\embedding.py:36 34 from ... import base 35 from ...util import is_np_array ---> 36 from ... import numpy as _mx_np 37 from ... import numpy_extension as _mx_npx 40 def register(embedding_cls): File ~\anaconda3\envs\myenv\lib\site-packages\mxnet\numpy\__init__.py:23 21 from . import random 22 from . import linalg ---> 23 from .multiarray import * # pylint: disable=wildcard-import 24 from . import _op 25 from . import _register File ~\anaconda3\envs\myenv\lib\site-packages\mxnet\numpy\multiarray.py:47 45 from ..ndarray.numpy import _internal as _npi 46 from ..ndarray.ndarray import _storage_type, from_numpy ---> 47 from .utils import _get_np_op 48 from .fallback import * # pylint: disable=wildcard-import,unused-wildcard-import 49 from . import fallback File ~\anaconda3\envs\myenv\lib\site-packages\mxnet\numpy\utils.py:37 35 int64 = onp.int64 36 bool_ = onp.bool_ ---> 37 bool = onp.bool 39 pi = onp.pi 40 inf = onp.inf File ~\anaconda3\envs\myenv\lib\site-packages\numpy\__init__.py:284, in __getattr__(attr) 281 from .testing import Tester 282 return Tester --> 284 raise AttributeError("module {!r} has no attribute " 285 "{!r}".format(__name__, attr)) AttributeError: module 'numpy' has no attribute 'bool' 
1

8 Answers

I got the same error...

Finally, the combination that works for me is:

pip3 install mxnet-mkl==1.6.0 numpy==1.23.1 
1

Adding to this post since @Yinon_90 's answer helped me in a different context

Setup

  • from your terminal:
conda create -n conda-env python=3.8 -y conda activate conda-env (conda-env) python -m pip install -e . 

numpy version 1.24.1 is installed

  • requirements.txt contents:
coremltools diffusers[torch] torch transformers scipy 

Solution --> downgrade numpy

python -m pip uninstall numpy python -m pip install numpy==1.23.1 
2

As we can see in NumPy 1.20.0 Release Notes

Using the aliases of builtin types like np.int is deprecated.

(Not just np.int but also np.bool, ...)

Then, in version NumPy 1.24.0, the deprecated np.bool was entirely removed. This means you are using a NumPy version that removed the deprecated ways AND the library you are using wasn't updated to match that version (uses something like np.bool instead of just bool).

You can use an older version of numpy (before the removal) while that isn't fixed. @sirViv notes that the latest is 1.23.5.

1

numpy.bool is deprecated. It is recommended to downgrade to version 1.23.1. As legacy there is numpy.bool_ (with underscore). A trick that helped me was to do the following:

import numpy as np np.bool = np.bool_ 

It's pretty dirty, but it worked for me.

This is everywhere in mxnet. It's here in a v2.0.0 release candidate (so, bleeding edge), same as it is in version 1.7.0 that you're using.

What's less clear to me is when this ceased being a thing in numpy. It's not listed in the current scalar types. But, let's jump back to 2019, with version 1.18 of numpy, prior to the release of the version of mxnet that you're using - here. It's not even a type there!

What's most confusing here is that it's in utils.py and you would expect such a module to be blowing up all over the place with this issue as that'll be a core module... but it isn't. I'm not sure what I'm missing here but it might be worth raising on their github.

4

For numpy-1.24.3

It says:

np.bool was a deprecated alias for the builtin bool.

To avoid this error in existing code, use bool by itself.

Doing this will not modify any behavior and is safe.

If you specifically wanted the numpy scalar type, use np.bool_ here.

Either use bool or np.bool_ both will work in place of np.bool.

My case was solved by downgrading Numpy (not with MXNet). I reinstalled Numpy with version 1.23.1. I think that the reason without MXNet is that I built the MXNet from the source (and install Python tool from the build).

My runtime environment:
   OS: Ubuntu 20.04 in Arm processor (Nvidia AGX Orin)
   Python 3.8
   MXNet 2.0.0

One Deep Solution is to go to the library files and replace any np.bool with bool in the .py file shown in the error message. in the error message, you will find something like _tree.py has np.bool

  1. Locate the file where the package with the problem is installed on your system.It is typically located in the site-packages directory of your Python environment. Use import site; site.getsitepackages() to find your sitepackages path.

  2. Open your package file named base.py or the name of .py file you found in the error message that caused the attribute error.

  3. Search for the line that contains np.bool and replace it with bool

  4. Save the .py file.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like