未使用numpy.float却触发该属性不存在错误的技术求助
问题:numpy.float报错但未直接使用该属性的解决办法
问题描述
我运行一个使用numpy.float64的光谱计算脚本时,弹出错误提示“module numpy has no attribute float”,但我的脚本并未使用numpy.float。该错误会阻断calculate_tm30函数之后的代码执行。我已尝试将numpy降级至支持numpy.float的1.19.5版本,但问题仍未解决,求可行方案。
代码片段
def __init__(self, verbosity=True, csv=True, name='Spectrometer'): """Initialize the object. Keyword Arguments: ----------------- verbosity - option to hide/show different level of information in console csv - specify if the object should create a csv file. name - device name. """ self.logging = logging self.verbosity = verbosity self.name = name # opens the spectrometer for this program try: # creates a connection with the first available spectrometer self.spec = Spectrometer.from_serial_number("USB2+F04442") self.spec.open() print('SPECTROMETER CONNECTED') logging.info("SPECTROMETER CONNECTED") except Exception as e: self.spec = None print(e) print('SPECTRUM FAILED TO CONNECT') logging.info('SPECTRUM FAILED TO CONNECT') def get_spectrum(self, log=True): """Get light spectrum from the spectrometer. This will override any existing intensity and wavelength data. This does not modify/affect any csv's or graphs created. """ spectrum = [self.spec.wavelengths(), self.spec.intensities()] if self.verbosity: print('Captured Spectrum\n') if log: logging.info("Captured Spectrum") return spectrum def calculate_tm30(self, spectrum): """Calculate TM-30 of a spectrum. Keyword Arguments: ----------------- spectrum - Spectrum to use as the SPD (Spectral Power Distribution) """ tm30 = lx.cri.spd_to_tm30_fast(np.array(spectrum, dtype=np.float64)) return tm30.get('Rf') if __name__ == "__main__": test = ATJSpec(True) try: spectrum = test.get_spectrum() print(f"{spectrum[0]} ||| {spectrum[1]}") print("Test result: ") print(test.calculate_tm30(spectrum)) print("Test ran") except Exception as e: print(e) finally: time.sleep(1000) test.close()
运行输出
C:\Users\rrams\Desktop\ATJ\roboshi\ATJSpec.py:22: UserWarning: seabreeze.use has to be called before importing seabreeze.spectrometers seabreeze.use('cseabreeze') SPECTROMETER CONNECTED Captured Spectrum [1.7698e+02 1.7736e+02 1.7775e+02 ... 8.8320e+02 8.8350e+02 8.8380e+02] ||| [1.6384e+01 2.7326e+04 5.7343e+02 ... 6.2492e+02 5.6173e+02 5.6173e+02] 测试结果: module 'numpy' has no attribute 'float'. `np.float`是Python内置`float`的已弃用别名。为避免现有代码出现此错误,请直接使用`float`,这不会改变任何行为且安全。如果您确实需要numpy标量类型,请在此处使用`np.float64`。 该别名最初在NumPy 1.20版本中被弃用;更多详情和指南请查看原始发布说明: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
解决方案
- 问题根源:错误并非来自你的代码,而是你调用的
lx.cri.spd_to_tm30_fast函数内部使用了已被numpy弃用的np.float属性。降级numpy到1.19.5无效,大概率是因为环境中存在多个numpy版本,或者依赖库未同步适配旧版本numpy。 - 方案1:升级依赖库
假设lx是luxpy库,直接升级到最新版本:
新版本的库应该已经修复了numpy弃用属性的问题,把pip install --upgrade luxpynp.float替换成了float或np.float64。 - 方案2:添加临时兼容别名
如果无法升级库,可在脚本最开头添加以下代码,临时给numpy补上float别名:import numpy as np if not hasattr(np, 'float'): np.float = float - 方案3:清理Python环境
检查环境中是否存在多个numpy版本,导致降级操作无效:- 执行
pip list | findstr numpy(Windows)或pip list | grep numpy(Linux/Mac)查看已安装版本。 - 卸载所有numpy版本:
pip uninstall -y numpy。 - 重新安装1.19.5版本:
pip install numpy==1.19.5。
- 执行
内容的提问来源于stack exchange,提问作者th3virtuos0
相关产品推荐
相关产品推荐

