资料主要来源于 TensorFlow 官网 。
安装方式选择
官方提供了一下几种安装方式:
Virtualenv (虚拟环境)
"native" pip(原生环境使用 pip 工具安装)
Docker (使用 docker 容器)
installing from sources, which is documented in a separate guide. (使用源码)
官方推荐使用第一种方式,即 Virtualenv
,是一个虚拟 Python 环境,这种方式的好处就是避免 pip 直接安装对原本 Python 环境造成干扰。
关于 virtualenv 的优点,可以参考:Python开发必备神器之一:virtualenv
安装步骤
接下来我们就开始使用 Virtualenv
的方式来安装 TensorFlow :
使用终端安装 pip 和 virtualenv :
$ sudo easy_install pip $ sudo pip install --upgrade virtualenv
OS X 系统一般默认安装了 Python ,可以在终端中输入
python --version
查询当前版本:$ python --version Python 2.7.10
刚开始安装 pip 时我开着 Shadowsocks ,结果
sudo easy install pip
卡在Reading https://pypi.python.org/simple/pip/
就没动静了,将其关掉再安装就正常。安装 virtualenv 时提示:
Using cached virtualenv-15.2.0-py2.py3-none-any.whl matplotlib 1.3.1 requires nose, which is not installed. matplotlib 1.3.1 requires tornado, which is not installed. Installing collected packages: virtualenv
加上 sudo 之后才正常,看到如下日志表示安装成功:
$ sudo pip install virtualenv Password: Collecting virtualenv Downloading virtualenv-15.2.0-py2.py3-none-any.whl (2.6MB) 100% |████████████████████████████████| 2.6MB 45kB/s matplotlib 1.3.1 requires nose, which is not installed. matplotlib 1.3.1 requires tornado, which is not installed. Installing collected packages: virtualenv Successfully installed virtualenv-15.2.0 $ virtualenv --version 15.2.0
能查询到版本信息才表示安装成功。
创建
Virtualenv
环境:$ virtualenv --system-site-packages 虚拟环境根目录(目录名不可带空格)
这里创建的虚拟环境仅用于 TensforFlow 的开发,可以直接用其来命名,例如:
$ virtualenv --system-site-packages ~/Desktop/mechineLearning/TensorFlow/Virtualenv/ New python executable in /Users/linshuhe/Desktop/mechineLearning/TensorFlow/Virtualenv/bin/python Installing setuptools, pip, wheel...done.
执行成功会在指定的目录下生成如下文件:lib 、bin 、include 和 pip-selfcheck.json 。即复制了一份本地的 Python 环境到一个独立的目录下,从而实现环境隔离的目的。
目录或父目录带空格的话,在虚拟环境中访问 pip 会报错:bad interpreter: No such file or directory。
删除虚拟环境,确保是在虚拟环境非激活状态下,直接将要删除的虚拟环境所在的目录删除即可:
$ rm -r 虚拟环境根目录
激活上一步创建的虚拟环境:
$ cd 虚拟环境根目录 $ source ./bin/active
激活成功的话,便会进入到虚拟环境的命令状态,指令目录会发生变化,会出现当前虚拟环境的名称(与创建虚拟环境包的目录名一致):
linshuhes-MacBook-Pro:Virtualenv linshuhe$ source ./bin/activate (Virtualenv) linshuhes-MacBook-Pro:Virtualenv linshuhe$
确保虚拟环境中的 pip 版本高于 8.1,低于此版本的可以使用指令
easy_install -U pip
来更新。退出当前虚拟环境的指令:deactivate
安装 TensorFlow :
pip install --upgrade tensorflow
这个过程会安装许多工具插件,需要等待一定时间。知道看到如下输出表示安装成功:
Successfully installed absl-py-0.1.13 astor-0.6.2 backports.weakref-1.0.post1 bleach-1.5.0 enum34-1.1.6 funcsigs-1.0.2 futures-3.2.0 gast-0.2.0 grpcio-1.11.0 html5lib-0.9999999 markdown-2.6.11 mock-2.0.0 numpy-1.14.2 pbr-4.0.2 protobuf-3.5.2.post1 six-1.11.0 tensorboard-1.7.0 tensorflow-1.7.0 termcolor-1.1.0 werkzeug-0.14.1
这里显示所有安装的工具插件。
卸载也很简单:
pip uninstall tensorflow
此外,这里安装的 tensorflow 是一个工具库,不能在命令行中直接通过
tensorflow --version
查询其版本信息。但可以通过 Python 语句来查询:$ python Python 2.7.10 (default, Jul 15 2017, 17:16:57) [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import tensorflow as tf >>> tf.__version__ # 查询版本号 '1.7.0' >>> tf.__path__ # 查询安装路径 ['/Users/linshuhe/Desktop/mechineLearning/TensorFlow/Virtualenv/lib/python2.7/site-packages/tensorflow'] >>>
如此,表示 tensorflow 安装完成。
测试 Demo
就以上面查询版本号的例子,直接编写一个 Python 脚本名称为 TestTFVersion.py
内容如下:
import tensorflow as tf print "current tf version: ",tf.__version__ print "tf install path: ",tf.__path__
然后在 Tensorflow 安装的虚拟环境激活的状态下执行测试脚本:
$ python TestTFVersion.py current tf version: 1.7.0 tf install path: ['/Users/linshuhe/Desktop/mechineLearning/TensorFlow/Virtualenv/lib/python2.7/site-packages/tensorflow']
其他
常见的安装问题解决可以查询:Common installation problems