AttributeError: module 'tensorflow' has no attribute 'Session'

根据网上教程安装tensorflow环境时,最后都会有个测试代码用来测试tensorflow是否安装成功

import tensorflow as tf
hello = tf.constant('hello,tf')
sess = tf.Session()
print(sess.run(hello))

有的朋友在运行这样代码时,会出现这样的错误

AttributeError: module 'tensorflow' has no attribute 'Session'

不用担心,你的tensorflow已经安装成功,原因是你安装的tensorflow是2.0以上版本

sess = tf.Session()

代码已经修改为

sess = tf.compat.v1.Session()

当然,如果你觉得不习惯的话,还有另一种方法,将你的tensorflow从2.0以上版本降级为以下版本即可

pip uninstall tensorflow

再重新安装

pip install tensorflow=1.14

安装完成可能会出现FutureWarning警告,不妨碍运行,详细解决方案可点击以下链接查看
解决Python import tensorflow出现FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated;

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容