简介
python开发中存在2个版本: python2和python3,这2个版本有细微的不同,在OS上安装2个版本并很好地管理各自的依赖包,并不是一个很好的事情。
再者,在团队开发中,每个开发者的环境不同,对开发的各种影响也很折磨团队。
在OS中管理python环境及依赖包,聪明的开发者已经提供了virtualenv和anaconda。
Anaconda使用conda来管理开发使用的依赖包,安装后还提供图形化的UI。
Anaconda在网站上介绍自己是python Data Science Platform,包含了超过1000的数据科学的包,可以方便地管理使用。
安装
从Anaconda网站下载安装包进行安装,非常简单。
安装后,会在系统配置path中添加anaconda的执行文件所在目录,然后就可以方便地使用conda命令了。
$ diff ~/.profile-anaconda2.bak ~/.profile
43,45d42
<
< # added by Anaconda2 5.0.0 installer
< export PATH="/Users/david/anaconda2/bin:$PATH"
命令使用
先创建一个Python2的环境,可以看到,Anaconda建立了一个独立的python2的环境,包括python,pip等
$ conda create --name py2 python=2.7
Fetching package metadata ...........
Solving package specifications: .
Package plan for installation in environment /Users/david/anaconda2/envs/py2:
The following NEW packages will be INSTALLED:
ca-certificates: 2017.08.26-ha1e5d58_0
certifi: 2017.7.27.1-py27h482ffc0_0
libcxx: 4.0.1-h579ed51_0
libcxxabi: 4.0.1-hebd6815_0
libedit: 3.1-hb4e282d_0
libffi: 3.2.1-hd939716_3
ncurses: 6.0-ha932d30_1
openssl: 1.0.2l-h57f3a61_2
pip: 9.0.1-py27h61def0c_3
python: 2.7.14-he768d2d_19
readline: 7.0-h81b24a6_3
setuptools: 36.5.0-py27h2a45cec_0
sqlite: 3.20.1-h900c3b0_1
tk: 8.6.7-hcdce994_1
wheel: 0.29.0-py27h84bd1c0_1
zlib: 1.2.11-h60db283_1
Proceed ([y]/n)? y
ca-certificate 100% |##################################################################################| Time: 0:00:00 396.39 kB/s
libcxxabi-4.0. 100% |##################################################################################| Time: 0:00:00 3.06 MB/s
tk-8.6.7-hcdce 100% |##################################################################################| Time: 0:00:01 2.20 MB/s
zlib-1.2.11-h6 100% |##################################################################################| Time: 0:00:00 528.35 kB/s
libcxx-4.0.1-h 100% |##################################################################################| Time: 0:00:00 2.62 MB/s
openssl-1.0.2l 100% |##################################################################################| Time: 0:00:03 1.15 MB/s
libffi-3.2.1-h 100% |##################################################################################| Time: 0:00:00 697.85 kB/s
ncurses-6.0-ha 100% |##################################################################################| Time: 0:00:03 243.77 kB/s
libedit-3.1-hb 100% |##################################################################################| Time: 0:00:00 157.53 kB/s
readline-7.0-h 100% |##################################################################################| Time: 0:00:03 117.73 kB/s
sqlite-3.20.1- 100% |##################################################################################| Time: 0:00:17 108.32 kB/s
python-2.7.14- 100% |##################################################################################| Time: 0:02:10 79.49 kB/s
certifi-2017.7 100% |##################################################################################| Time: 0:00:02 79.36 kB/s
setuptools-36. 100% |##################################################################################| Time: 0:00:09 55.57 kB/s
wheel-0.29.0-p 100% |##################################################################################| Time: 0:00:00 91.61 kB/s
pip-9.0.1-py27 100% |##################################################################################| Time: 0:00:20 85.71 kB/s
#
# To activate this environment, use:
# > source activate py2
#
# To deactivate an active environment, use:
# > source deactivate
#
执行python命令,可以看到python2版本的信息。
激活一个环境
使用命令source activate py2
释放激活的环境
使用命令 source deactivate
再创建一个Python3的环境,Anaconda会建立了一个独立的python3的环境。
$ conda create --name py3 python=3
Fetching package metadata ...........
Solving package specifications: .
Package plan for installation in environment /Users/david/anaconda2/envs/py3:
The following NEW packages will be INSTALLED:
ca-certificates: 2017.08.26-ha1e5d58_0
certifi: 2017.7.27.1-py36hd973bb6_0
libcxx: 4.0.1-h579ed51_0
libcxxabi: 4.0.1-hebd6815_0
libedit: 3.1-hb4e282d_0
libffi: 3.2.1-hd939716_3
ncurses: 6.0-ha932d30_1
openssl: 1.0.2l-h57f3a61_2
pip: 9.0.1-py36hbd95645_3
python: 3.6.3-h6804ab2_0
readline: 7.0-h81b24a6_3
setuptools: 36.5.0-py36h2134326_0
sqlite: 3.20.1-h900c3b0_1
tk: 8.6.7-hcdce994_1
wheel: 0.29.0-py36h3597b6d_1
xz: 5.2.3-ha24016e_1
zlib: 1.2.11-h60db283_1
Proceed ([y]/n)?
xz-5.2.3-ha240 100% |##################################################################################| Time: 0:00:00 313.71 kB/s
python-3.6.3-h 100% |##################################################################################| Time: 0:03:20 68.40 kB/s
certifi-2017.7 100% |##################################################################################| Time: 0:00:02 96.14 kB/s
setuptools-36. 100% |##################################################################################| Time: 0:00:08 63.90 kB/s
wheel-0.29.0-p 100% |##################################################################################| Time: 0:00:01 54.94 kB/s
pip-9.0.1-py36 100% |##################################################################################| Time: 0:00:52 33.39 kB/s
#
# To activate this environment, use:
# > source activate py3
#
# To deactivate an active environment, use:
# > source deactivate
#
$ source activate py3
(py3) promote:conda_prj david$ python
Python 3.6.3 |Anaconda, Inc.| (default, Oct 6 2017, 12:04:38)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
(py3) promote:conda_prj david$ source deactivate
promote:conda_prj david$
promote:conda_prj david$ source activate py2
(py2) promote:conda_prj david$ python
Python 2.7.14 |Anaconda, Inc.| (default, Oct 5 2017, 02:28:52)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>>
(py2) promote:conda_prj david$
(py2) promote:conda_prj david$ source deactivate
promote:conda_prj david$
promote:conda_prj david$
验证python3的环境也成功。
Anaconda Navigation
如图:
tensorflow 环境
$ conda create -n tensorflow python=3
Fetching package metadata ...........
Solving package specifications: .
Package plan for installation in environment /Users/david/anaconda2/envs/tensorflow:
The following NEW packages will be INSTALLED:
ca-certificates: 2017.08.26-ha1e5d58_0
certifi: 2017.7.27.1-py36hd973bb6_0
libcxx: 4.0.1-h579ed51_0
libcxxabi: 4.0.1-hebd6815_0
libedit: 3.1-hb4e282d_0
libffi: 3.2.1-hd939716_3
ncurses: 6.0-ha932d30_1
openssl: 1.0.2l-h57f3a61_2
pip: 9.0.1-py36hbd95645_3
python: 3.6.3-h6804ab2_0
readline: 7.0-h81b24a6_3
setuptools: 36.5.0-py36h2134326_0
sqlite: 3.20.1-h900c3b0_1
tk: 8.6.7-hcdce994_1
wheel: 0.29.0-py36h3597b6d_1
xz: 5.2.3-ha24016e_1
zlib: 1.2.11-h60db283_1
Proceed ([y]/n)?
#
# To activate this environment, use:
# > source activate tensorflow
#
# To deactivate an active environment, use:
# > source deactivate
#
promote:conda_prj david$ source activate tensorflow
(tensorflow) promote:conda_prj david$ pip install --ignore-installed tensorflow --upgrade
Collecting tensorflow
Downloading tensorflow-1.3.0-cp36-cp36m-macosx_10_11_x86_64.whl (39.8MB)
100% |████████████████████████████████| 39.8MB 19kB/s
Collecting tensorflow-tensorboard<0.2.0,>=0.1.0 (from tensorflow)
Downloading tensorflow_tensorboard-0.1.8-py3-none-any.whl (1.6MB)
100% |████████████████████████████████| 1.6MB 36kB/s
Collecting six>=1.10.0 (from tensorflow)
Using cached six-1.11.0-py2.py3-none-any.whl
Collecting numpy>=1.11.0 (from tensorflow)
Downloading numpy-1.13.3-cp36-cp36m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (4.5MB)
100% |████████████████████████████████| 4.6MB 23kB/s
Collecting protobuf>=3.3.0 (from tensorflow)
Downloading protobuf-3.4.0-py2.py3-none-any.whl (375kB)
100% |████████████████████████████████| 378kB 18kB/s
Collecting wheel>=0.26 (from tensorflow)
Downloading wheel-0.30.0-py2.py3-none-any.whl (49kB)
100% |████████████████████████████████| 51kB 9.5kB/s
Collecting werkzeug>=0.11.10 (from tensorflow-tensorboard<0.2.0,>=0.1.0->tensorflow)
Using cached Werkzeug-0.12.2-py2.py3-none-any.whl
Collecting markdown>=2.6.8 (from tensorflow-tensorboard<0.2.0,>=0.1.0->tensorflow)
Downloading Markdown-2.6.9.tar.gz (271kB)
100% |████████████████████████████████| 276kB 26kB/s
Collecting html5lib==0.9999999 (from tensorflow-tensorboard<0.2.0,>=0.1.0->tensorflow)
Downloading html5lib-0.9999999.tar.gz (889kB)
100% |████████████████████████████████| 890kB 27kB/s
Collecting bleach==1.5.0 (from tensorflow-tensorboard<0.2.0,>=0.1.0->tensorflow)
Downloading bleach-1.5.0-py2.py3-none-any.whl
Collecting setuptools (from protobuf>=3.3.0->tensorflow)
Using cached setuptools-36.5.0-py2.py3-none-any.whl
Building wheels for collected packages: markdown, html5lib
Running setup.py bdist_wheel for markdown ... done
Stored in directory: /Users/david/Library/Caches/pip/wheels/bf/46/10/c93e17ae86ae3b3a919c7b39dad3b5ccf09aeb066419e5c1e5
Running setup.py bdist_wheel for html5lib ... done
Stored in directory: /Users/david/Library/Caches/pip/wheels/6f/85/6c/56b8e1292c6214c4eb73b9dda50f53e8e977bf65989373c962
Successfully built markdown html5lib
Installing collected packages: six, setuptools, protobuf, werkzeug, markdown, html5lib, numpy, bleach, wheel, tensorflow-tensorboard, tensorflow
Successfully installed bleach-1.5.0 html5lib-0.9999999 markdown-2.6.9 numpy-1.13.3 protobuf-3.4.0 setuptools-36.5.0.post20170921 six-1.11.0 tensorflow-1.3.0 tensorflow-tensorboard-0.1.8 werkzeug-0.12.2 wheel-0.30.0
(tensorflow) promote:conda_prj david$
测试一下安装的TensorFlow
$ python
Python 3.6.3 |Anaconda, Inc.| (default, Oct 6 2017, 12:04:38)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> print(tf.__version__)
1.3.0
OK, tensorflow的环境安装成功。