windows本地python3.9安装tensorflow 2.6并测试成功

1. 环境:windows 系统; python3.9.7版本

2. 安装 tensorflow

2.1 直接 pip install tensorflow 对应的版本为2.9.1,加载时报错

在导入的时候会报ImportError: cannot import name '_pywrap_record_io' from 'tensorflow.python.lib.io'


image.png

看很多说是python版本问题,但又不想把python 调到3.6以下

2.2 卸载后,安装tensorflow 2.6版本
如果不指定镜像,直接pip 会报超时
pip install tensorflow==2.6.0
image.png

指定镜像安装即可

pip install tensorflow==2.6.0 -i https://pypi.tuna.tsinghua.edu.cn/simple

3. 安装测试

3.1 直接导入tensorflow,在用tf.session时报错AttributeError: module 'tensorflow' has no attribute 'Session'
 import tensorflow as tf
 import os
 hello = tf.constant('hello cui, hello tensorflow')
 os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
 sess = tf.Session()
image.png
3.2 因为tensorflow1.X 与2.X版本不兼容问题会报如上错误,要加入tf.compat.v1.disable_eager_execution() 或更简单的直接导入
 import tensorflow.compat.v1 as tf
 import os
 hello = tf.constant('hello cui, hello tensorflow')
 os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
 sess = tf.Session()
print(sess.run(hello))
image.png

参考:
https://blog.csdn.net/S_Wiper/article/details/125789851
https://blog.csdn.net/UCAS2019/article/details/120010881

你可能感兴趣的:(windows本地python3.9安装tensorflow 2.6并测试成功)