今天下載安裝了Anaconda,需要在Anaconda上安裝OpenCV和TensorFlow,但總會顯示:
Specifications:
- tensorflow-gpu -> python[version='3.5.*|3.6.*|3.7.*|>=3.7,<3.8.0a0|>=3.6,<3.7.0a0|>=3.5,<3.6.0a0|>=2.7,<2.8.0a0'] Your python: python=3.8
因為python=3.8是最新版本,相應的TensorFlow 還沒能及時更新,需要自己下載安裝TensorFlow2
折騰了好久,走了很多彎路,最后使用了以下方法安裝成功,希望能幫助到其他朋友:
一、檢查電腦上目前的python和pip版本
python3 --version
pip3 --version
**注意:必須使用最新版本的pip
,才能安裝 TensorFlow 2。升級pip:
pip install --upgrade pip
二、配置python3.8運行環境
conda create -n env_name python=3.8 conda activate env_name conda install pandas scikit-learn matplotlib notebook
pip3 install --user --upgrade 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以上版本
可以將代碼修改為:
import tensorflow as tf
tf.executing_eagerly()
with tf.compat.v1.Session() as sess:
hello = tf.constant('Hello, tf!')
print(sess.run(hello))
點擊運行:
安裝成功!