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

8

60

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'
Besprent answered 22/12, 2022 at 20:53 Comment(1)
yeah - it looks like the version of mxnet you have installed is incompatible with the version of numpy you're using. I'd make sure you're installing everything using the same conda channel. Since you're using anaconda, you cannot install anything into your base env using the conda-forge channel, so make sure you're careful about which channels go with which env. Frankly, I'd recommend deleting anaconda and installing miniforge or mambaforge from the same page.Rn
P
49

I got the same error...

Finally, the combination that works for me is:

pip3 install mxnet-mkl==1.6.0 numpy==1.23.1
Parsley answered 26/12, 2022 at 6:29 Comment(1)
mxnet-mkl is not required for those experiencing the same error, you just need to downgrade pipCucumber
S
34

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.

Sapphira answered 11/5, 2023 at 4:33 Comment(1)
It's simple and working. And it doesn't need to downgrade the numpy version. Thank you.Quiberon
T
26

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
Tegucigalpa answered 28/12, 2022 at 9:35 Comment(2)
I ran into a similar error trying to use the python ai-benchmark tool: "AttributeError: module 'numpy' has no attribute 'warnings'. Did you mean: 'hanning'?". Your fix downgrading to numpy v1.23.1 fixed it - thanks!Stradivarius
just doing the 'downgrade numpy' part worked for me - thanks!Marolda
U
21

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.

Unintelligent answered 1/1, 2023 at 16:56 Comment(1)
Jut to add up, the newest with the legacy np.bool seems to be NumPy 1.23.5Pretence
S
6

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.

Southey answered 22/12, 2022 at 21:24 Comment(4)
I just hopped on Google Colab which has 1.21.6 installed. There, using np.bool gives DeprecationWarning: np.bool is a deprecated alias for the builtin bool. To silence this warning, 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. Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecationsEventide
@Eventide I'm amazed this could survive so many release cycles as it's a show-stopper. Almost certainly something to raise on their githubSouthey
@Eventide do you mind if I promote your comment to be part of the answer?Southey
Not at all, go ahead.Eventide
F
3

For numpy-1.24.3

https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations

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.

Forlini answered 24/5, 2023 at 9:28 Comment(0)
B
1

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

Benford answered 28/2, 2023 at 20:1 Comment(0)
W
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.

Weakkneed answered 3/7, 2023 at 16:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.