Flask API克隆新仓库后运行报错:module 'numpy' has no attribute 'bool'
Flask API运行报错排查:Numpy无
bool属性问题 问题背景
原Flask API可正常运行,因原仓库中已删除的大体积数据集反复出现导致无法推送至GitHub,克隆到新仓库后运行触发报错,虽未直接使用Numpy,但出现Numpy相关属性错误。
报错详情
File "/Users/sa/api-flask2/flask_app.py", line 5, in <module> import pandas as pd File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/__init__.py", line 180, in <module> import pandas.testing File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/testing.py", line 5, in <module> from pandas._testing import ( File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/_testing.py", line 24, in <module> import pandas._libs.testing as _testing File "pandas/_libs/testing.pyx", line 10, in init pandas._libs.testing File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/numpy/__init__.py", line 284, in __getattr__ raise AttributeError("module {!r} has no attribute ".format(__name__)) AttributeError: module 'numpy' has no attribute 'bool' Process finished with exit code 1
解决方法
这个错误是Numpy与Pandas版本不兼容导致的:Numpy 1.24及以上版本移除了numpy.bool这个旧属性,而当前使用的Pandas版本依赖该属性,结合Python3.8的环境限制,可按以下方案解决:
方案一:降级Numpy到兼容版本
安装适配旧版Pandas的Numpy 1.23.x版本:pip install numpy==1.23.5方案二:升级Pandas到兼容高版本Numpy的版本
Python3.8支持的最高Pandas版本为2.0.x,安装该版本可兼容Numpy 1.24+:pip install pandas==2.0.3方案三:用虚拟环境隔离依赖(推荐)
避免全局包版本冲突,创建专属虚拟环境并安装适配的依赖:python3.8 -m venv venv source venv/bin/activate pip install flask pandas numpy==1.23.5
内容的提问来源于stack exchange,提问作者Sofia
相关产品推荐
相关产品推荐

