
5.2 Tensorflow-cpu安装
在官方网址https://tensorflow.google.cn/install/pip中可以看到关于使用pip安装Tensorflow比较详细的说明。
5.2.1 Tensorflow-cpu在线安装
使用Windows安装Tensorflow的同学要注意,从TensorFlow 2.1.0版本开始,需要安装vc_redist.x64.exe,进入链接https://support.microsoft.com/en-us/help/2977003/the-latest-supported-visual-c-downloads,下载Visual Studio 2015,2017 and 2019下面的x64:vc_redist.x64.exe(或直接从https://aka.ms/vs/16/release/vc_redist.x64.exe链接下载),下载后双击其进行安装。
Tensorflow在Winodws/MacOS/Linux环境下的安装方式基本上都是一样的。首先介绍CPU版本的安装。安装Tensorflow之前,先要安装Python环境,Python环境的安装在本书第2章已经介绍过了,大家先要把Anaconda给安装好,如使用Windows系统,则需要安装Python 3.5/3.6/3.7版本的64位的Anaconda。如果大家跟着书中的步骤进行安装,则需要先把安装流程全部看完再动手,否则可能会操作错误。Python安装模块的方式都可以用pip install的命令进行安装。Tensorflow 2.0正式发布以后,现在Tesnorflow默认安装的版本就是Tensorflow 2的版本。安装Tensorflow可以用管理员方式打开命令提示符,运行如下命令:

但上面的命令通常的下载速度比较慢,推荐从国内源进行下载速度比较快。使用下面的命令,其下载速度会比较快:

其中,-i https://pypi.douban.com/simple是国内下载源,安装其他的Python模型也可以使用该下载源。
执行完以上命令之后,会自动从网上下载安装Tensorflow安装包,同时也会安装和更新一些其他的Python包。
顺利的话,运行完以上命令后,Tensorflow就安装好了。安装好Tensorflow之后,我们可以在命令行的最后看到类似如下的信息:

但一般来说都不会这么顺利,关于可能会出现的问题,以及如何解决问题,后面会总结。
假设安装没有问题,那么可以打开一个Python的运行环境,如Jupyter,然后运行命令:

如果没有产生错误,那么就代表安装成功了。如果看到警告,不要紧张,有警告是正常的,一般警告都可以忽略掉,如图5.2所示表示Tensorflow安装成功。

图5.2 Tensorflow安装成功
5.2.2 安装过程中可能遇到的问题
由于Tensorflow会不断地更新,对于每个Tensorflow版本,我们可能会遇到的问题不同,每个人的计算机环境也有所不同,所以我这里总结的问题不一定跟大家碰到的问题相同,也可能会有缺漏,如果问题不同或者有缺漏,大家可以给我反馈,我再进行补充。
问题1:在安装过程中出现“ERROR:tensorboard 2.0.2 has requirement grpcio>=1.24.3,but you'll have grpcio 1.14.1 which is incompatible.
ERROR:keras 2.2.2 has requirement keras-applications==1.0.4,but you'll have keras-applications 1.0.8 which is incompatible.”或者类似错误。
解决方法:这类错误可以忽略不处理。
问题2:在安装过程中出现“ERROR:Cannot uninstall'wrapt'.It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.”。
解决方法:用管理员方式打开命令提示符,运行以下命令:

然后再次运行以下命令:

如果出错的不是“wrapt”而是其他模块,类似的错误可以用类似的方法解决。
问题3:在安装过程中出现“distributed 1.21.8 requires msgpack, which is not installed.”或者类似错误。
解决方法:安装“msgpack“,打开命令提示符,运行以下命令:

问题4:某条命令在安装过程中出现“PermissionError:[WinError 5] 拒绝访问”。
解决方法:这个错误主要是权限问题,关闭所有与Python相关的软件,重新用管理员方式打开命令提示符,然后再次运行该命令。
问题5:在安装过程中,模块下载中断并出现“ReadTimeoutError:HTTPSConnectionPoll”。
解决方法:由于下载的资源在国外,所以网络不好可能会导致下载链接超时,可以尝试重新运行命令再次下载安装。也可以使用国内的下载源进行安装,一般速度会比较快,运行下面的命令使用国内的源进行安装。

问题6:在安装过程中,模块下载中断并出现“拒绝访问”。
解决方法:系统权限问题,可以用管理员方式打开命令提示符,然后重新安装,或者在安装命令后面加上“--user”。例如

问题7:Tensorflow安装成功后,在Python环境中运行“import tensorflow”后出现“ImportError:cannot import name‘dense_features’from‘tensorflow.python.feature_column’”。
解决方法:用管理员方式打开命令提示符,先运行以下命令:

然后再运行以下命令:

问题8:Tensorflow安装成功后,在Python环境中运行“import tensorflow”后出现“ImportError:DLL load failed with error code-1073741795和ImportError:No module named'_pywrap_tensorflow_internal'”。
解决方法:由于计算机的CPU导致的错误,解决方法一是安装老版本的Tensorflow,如Tensorflow 1.2.0版本,但是不推荐。推荐的解决方法是换一台新一点的计算机。
问题9:Tensorflow安装成功后,在Python环境中运行“import tensorflow”后出现“ERROR:root:Internal Python error in the inspect module.Below is the traceback from this internal error.”。
解决方法:安装vc_redist.x64.exe,具体查看5.2.1小节中的说明,然后再重新安装Tensorflow。
问题10:Tensorflow安装成功后,在Python环境中运行“import tensorflow”后出现“No module named‘tensorflow’”,说明Tensorflow还没有安装好。
解决方法:打开命令提示符,重新安装。

问题11:Tensorflow安装成功后,在Python环境中运行“import tensorflow”后出现“ImportError:DLL load failed:找不到指定的模型”。
解决方法:安装vc_redist.x64.exe,具体查看5.2.1小节中的说明,然后再重新安装Tensorflow。
5.2.3 Tensorflow-cpu卸载
如果已经安装好了Tensorflow,想要卸载,可以用管理员方式打开命令行,执行命令:

5.2.4 Tensorflow-cpu更新
如果已经安装过Tensorlfow,现在想把Tensorflow更新到最新版本,可以用管理员方式打开命令行,执行命令:

5.2.5 Tensorflow-cpu指定版本的安装
如果我们想安装Tensorflow指定版本,如老一点的版本,可以使用指定版本的安装方式。例如,我们想安装Tensorflow 1.13.2版本,则可以用管理员方式打开命令行,执行命令:
