安装retinaface与TensorFlow遇AttributeError:模块无__version__属性求助
问题解决方案
一、清理冲突的TensorFlow环境
当前环境同时存在tensorflow和tensorflow-cpu包,会导致模块导入混乱,是AttributeError的核心诱因之一,按以下步骤清理:
- 卸载冲突包:
pip uninstall -y tensorflow tensorflow-cpu - 检查残留并清理:
若输出中还有tensorflow相关依赖(如tensorflow-estimator),执行pip list | grep tensorflowpip uninstall -y [包名]彻底移除。
二、安装TensorFlow 2.5的正确流程
TensorFlow 2.5仅支持Python 3.6-3.9,先执行python --version确认版本兼容,不兼容则先创建对应虚拟环境:
- 用conda创建适配环境(以Python 3.8为例):
conda create -n tf25_env python=3.8 conda activate tf25_env - 安装TensorFlow 2.5:
安装失败可换国内镜像加速:pip install tensorflow==2.5pip install tensorflow==2.5 -i https://pypi.tuna.tsinghua.edu.cn/simple - 验证安装:
正常输出import tensorflow as tf print(tf.__version__)2.5.0即为安装成功。
三、安装RetinaFace并适配TensorFlow 2.5
RetinaFace部分版本对TensorFlow 2.x兼容性有限,需选择适配分支:
- 克隆TensorFlow 2.x兼容的仓库(以主流适配版本为例):
git clone https://github.com/StanislasBertrand/RetinaFace-tf2.git - 安装依赖:
pip install -r requirements.txt - 运行前确认导入语句正确,避免环境残留导致的模块混淆。
四、tf.__version__报错的额外排查
若清理环境后仍报错,检查以下内容:
- 确认当前Python环境路径与安装TensorFlow的路径一致(执行
which python/where python查看)。 - 删除本地同名文件:若存在
tensorflow.py,会覆盖官方模块,需重命名或删除。 - 检查模块属性:执行
import tensorflow as tf; print(dir(tf)),若输出无__version__,则重新安装TensorFlow。
内容的提问来源于stack exchange,提问作者Sean Tichenor
相关产品推荐
相关产品推荐

