导入nibabel遇AttributeError:numpy无float属性,求解决方法
解决nibabel导入时的AttributeError: module 'numpy' has no attribute 'float'问题
问题
安装nibabel库后尝试导入时出现错误:AttributeError: module 'numpy' has no attribute 'float',更换不同版本的numpy后问题仍未解决。
报错堆栈
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-2-954ce44cd9c4> in <module> ----> 1 import nibabel as nb 2 import matplotlib.pyplot as plt 3 import numpy as np 4 import sys 3 frames /usr/local/lib/python3.9/dist-packages/numpy/__init__.py in __getattr__(attr) 303 304 if attr in __former_attrs__: --> 305 raise AttributeError(__former_attrs__[attr]) 306 307 # Importing Tester requires importing all of UnitTest which is not a AttributeError: module 'numpy' has no attribute 'float'. `np.float` was a deprecated alias for the builtin `float`. To avoid this error in existing code, use `float` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.float64` 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
可行解决办法
升级nibabel到最新版本:该错误源于旧版nibabel依赖numpy已废弃的
np.float别名,新版nibabel已修复此兼容性问题。执行命令:pip install --upgrade nibabel临时兼容补丁(仅应急使用):若暂时无法升级nibabel,可在导入前手动添加别名绕过错误,在代码开头加入:
import numpy as np np.float = float import nibabel as nb注意:此方法仅为临时方案,长期使用仍建议升级库以避免其他潜在问题
锁定兼容的numpy版本:如果需要保留特定版本的nibabel,可查看对应nibabel版本的官方说明,安装其兼容的numpy版本(例如较老的nibabel版本通常兼容numpy<1.24),执行命令:
pip install numpy<1.24
内容的提问来源于stack exchange,提问作者O-Negative
相关产品推荐
相关产品推荐

