如何解决导入mxnet时出现的‘module 'numpy' has no attribute 'bool'’错误?
解决Google Colab中导入mxnet时的
module 'numpy' has no attribute 'bool'错误 问题场景
我在Google Colab中尝试用Python 3.8.5运行import mxnet as mx时,出现了module 'numpy' has no attribute 'bool'的错误。这段代码在2023年12月和2024年1月都能正常运行,现在突然无法使用了。我的操作步骤和代码如下:
挂载Google Drive
from google.colab import drive drive.mount('/content/drive')
安装Python 3.8.5
!wget https://www.python.org/ftp/python/3.8.5/Python-3.8.5.tgz !tar xvfz Python-3.8.5.tgz !Python-3.8.5/configure !make !sudo make install
安装依赖包
!pip install torch==1.6.0+cpu torchvision==0.7.0+cpu -f https://download.pytorch.org/whl/torch_stable.html !pip install --upgrade gluoncv !pip install --upgrade mxnet
导入模块的代码
import mxnet as mx from mxnet import image from mxnet.gluon.data.vision import transforms import gluoncv from gluoncv.utils.viz import get_color_pallete import matplotlib.image as mpimg from gluoncv.data.ade20k.segmentation import ADE20KSegmentation from matplotlib import pyplot as plt from gluoncv.data.transforms.presets.segmentation import test_transform from google.colab import files import pandas as pd import numpy as np import os
报错信息
/usr/local/lib/python3.10/dist-packages/mxnet/numpy/utils.py:37: FutureWarning: In the future `np.bool` will be defined as the corresponding NumPy scalar. bool = onp.bool --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-6-d07a1bcfa66c> in <cell line: 1>() ----> 1 import mxnet as mx 2 from mxnet import image 3 from mxnet.gluon.data.vision import transforms 4 import gluoncv 5 7 frames /usr/local/lib/python3.10/dist-packages/numpy/__init__.py in __getattr__(attr) 317 318 if attr in __former_attrs__: --> 319 raise AttributeError(__former_attrs__[attr]) 320 321 if attr == 'testing': AttributeError: module 'numpy' has no attribute 'bool'. `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. The aliases was originally deprecated in NumPy 1.20; for more details and guidance see the original release note at: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
解决方法
方法1:锁定兼容的numpy版本
问题核心是新版numpy彻底移除了np.bool这个废弃别名,而当前安装的mxnet版本仍在使用该写法。安装numpy 1.23.x及以下版本(保留np.bool兼容支持)即可解决:
!pip install numpy==1.23.5
执行后重新导入mxnet即可。
方法2:修改mxnet源码兼容新版numpy
如果不想降低numpy版本,可直接修改mxnet的兼容代码:找到报错中提示的/usr/local/lib/python3.10/dist-packages/mxnet/numpy/utils.py文件,将第37行的bool = onp.bool修改为:
bool = onp.bool_ # 或直接使用内置类型:bool = bool
注意:Colab重启后该修改会失效,仅适合临时测试。
方法3:确保Python 3.8环境生效
手动安装Python 3.8.5后,Colab默认仍使用系统Python 3.10,导致依赖包安装到了错误环境。用以下命令强制在Python 3.8环境中安装依赖:
!python3.8 -m pip install torch==1.6.0+cpu torchvision==0.7.0+cpu -f https://download.pytorch.org/whl/torch_stable.html !python3.8 -m pip install --upgrade gluoncv !python3.8 -m pip install --upgrade mxnet numpy==1.23.5
运行代码前切换Python环境:
import sys sys.executable = '/usr/local/bin/python3.8'
内容的提问来源于stack exchange,提问作者ROSA LEE
相关产品推荐
相关产品推荐

