tensorflow踩坑分享:AttributeError: module 'tensorflow' has no attribute 'xxx'

在使用TensorFlow 1.14时遇到AttributeError,问题出现在环境中存在TensorFlow 2.0版本。解决方案包括:1) 检查并确保使用的是正确的conda环境及TensorFlow 1.x版本;2) 删除本地Python路径下的TensorFlow 2.0文件夹,重启PyCharm,让其指向conda虚拟环境的TensorFlow 1.14。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

tensorflow踩坑分享:AttributeError: module 'tensorflow' has no attribute 'xxx'

一、环境

windows10
python3.6
tensorflow-gpu1.14

二、前因

前段时间开始学习tensorflow,后因事搁置,前几天重新学习,一开始使用没什么问题,后需要加装别的包,搞到环境有点乱,且此时已经出现了如题 AttributeError: module ‘tensorflow’ has no attribute ‘xxx’ 的问题。所以重新新建了一个conda的虚拟环境,但问题仍然存在。

三、解决过程

(一)问题表现:

import tensorflow as tf 可以成功,但是不管是 tf.Variable 还是 tf.placeholder 均会报如题错误。

(二)问题排查:

此时 tf.__path__ 仍可以使用,利用 tf.__path__ 可以查询到tensorflow的路径。

经查询,路径并非导向conda虚拟环境(一般为:C:\Users\用户名\Anaconda3\envs\环境名 路径下的一堆东西),而是导向了本地环境的路径(在我这里是 C:\Users\用户名\AppData\Roaming\Python\Python36\tensorflow

该路径下的tensorflow是2.0版本,应该是我装环境是误操作装上,后续pycharm导入时

AttributeError: module 'tensorflow' has no attribute 'gfile'错误通常是由于TensorFlow版本不兼容引起的。在TensorFlow 2.x版本中,tf.gfile被移除了,所以在使用TensorFlow 2.x版本时,如果你的代码中有使用到tf.gfile,那么就会出现这个错误。 解决办法(一)是升级你的TensorFlow版本到最新的2.x版本。在最新的TensorFlow版本中,tf.gfile已被tf.io.gfile取代。所以你只需要将代码中的tf.gfile替换为tf.io.gfile即可。 例如,如果你的代码中有这样一行代码: tf.gfile.GFile(file_path, 'w') 你可以将其修改为: tf.io.gfile.GFile(file_path, 'w') 这样就可以解决AttributeError: module 'tensorflow' has no attribute 'gfile'错误。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [AttributeError: module 'tensorflow.compat.v1' has no attribute '](https://download.csdn.net/download/qq_38766019/86272235)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [程序报错:AttributeError: module 'tensorflow' has no attribute 'xxx' 解决办法](https://blog.csdn.net/qq_41320433/article/details/104198059)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值