一、用anaconda prompt安装TensorFlow
1、镜像的选择
因为我之前用的是清华的后缀是free的一个镜像
(https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/),但是这个镜像能下载的TensorFlow最新版本只有1.x的,如果是想降版本的话可以使用这个镜像。
后面我在网上找了几个别的我用过的能用的镜像,如下:
https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/ https://mirrors.aliyun.com/anaconda/pkgs/main/
2、镜像的添加
添加国内镜像的原因是为了提高anaconda资源的下载速度,因为如果下载速度慢的话不仅耗时间,有时候下着下着下载失败就停止了(这个我是深有体会)
Anaconda的安装教程就不在这里赘述了,安装好之后在文件夹里找到anaconda navigator点击打开
点击channels把原有的删除,再点击add之后输入网址回车之后点击update channels刷新
二、TensorFlow的安装
1、创建环境
打开anaconda prompt,建立一个名字为tensorflow_try、Python版本为3.9的环境(TensorFlow的版本对Python版本是有要求的,要跟对应的版本匹配。因为镜像里的TensorFlow是2.6.0的,对应的Python是3.8的)
如果没有添加镜像的话也可以在anaconda里边创建环境,不会存在版本冲突,但是库会安装的很慢
2、下载tensorflow
安装成功后可以在anaconda navigator里找到这个环境,然后就是下载tensorflow框架了,这里提供两种方式,一是直接在anaconda navigator里搜索下载
二是在anaconda prompt里输入指令下载
如果版本不匹配会出现如下情况
如果下载的tensorflow用不了,可尝试如下指令:
pip install --upgrade --ignore-installed tensorflow
(gpu版)pip install --upgrade --ignore-installed tensorflow-gpu
三、查看版本路径
首先打开Anaconda Prompt(tensorflow)命令窗口。一般安装完tensorflow后都有这个窗口。
然后再Anaconda Prompt(tensorflow)命令窗口中输入命令:python,
会出现python 的版本信息
然后输入:
import tensorflow as tf
tf.__version__
出来tensorflow的版本信息
输入:tf.path
出来tensorflow的安装路径(退出指令为‘exit()’)
四、Spyder、jupyter的安装
Spyder的话,直接在anaconda navigator主页安装就行,但是,需要注意的是,如果安装过程中弹出一个提示框需要创建一个新环境才能安装的话说明Python版本与Spyder版本不匹配,根据提示在终端重新创建一个新的且跟Spyder版本对应的环境(如果在报错页面提示创建新环境的话可能会导致在新环境里下载tensorflow)
jupyter的安装同上,需要注意的是如何改变jupyter读取文件的默认路径。
1、菜单中打开Anaconda Prompt
输入命令 jupyter notebook --generate-config
根据上面运行处的路径打开
C:\Users\XX.jupyter\jupyter_notebook_config.py文件
2、找到 #c.NotebookApp.notebook_dir = ‘’,去掉该行前面的“#”;然后将新的路径设置在单引号中,(如果是‘\’的话要切换成‘/’),保存。
3、在开始菜单找到“Jupyte Notebook”快捷键,鼠标右击 – 更多 – 打开文件位置,找到对应的“Jupyte Notebook”快捷图标,鼠标右击 – 属性 – 目标,去掉后面的 “%USERPROFILE%/”(很重要),然后点击“应用”“确定”
4、重新启动Jupyte Notebook
查看Python对应的tensorflow版本的话可查看该网址:
https://tensorflow.google.cn/install/source_windows#cpu
五、设置环境变量
六、指令汇总
创建新环境
conda create -n name python=3.x
卸载旧版本
pip uninstall tensorflow
安装新版本
pip install tensorflow
更新tensorflow版本
pip install --upgrade --ignore-installed tensorflow
(gpu版)pip install --upgrade --ignore-installed tensorflow-gpu
激活环境(例如tensorflow)
activate tensorflow
查看pip清单
pip list