一. 使用 anaconda 搭建 python 虚拟环境
1. 首先建议从 anaconda 来使用 python ,anaconda3 对应的是 python3, anaconda2 对应的是 python2 。
2. 现在开发机器上已安装了 anaconda3,现在需要使用 python2 ,而又不想安装 anaconda2,毕竟 anaconda 占用的空间挺大的。这个时候,就可以使用 anaconda 来创建 python2 的虚拟环境 。
3. 创建 python 虚拟环境基于沙盒原理 。下面是创建步骤
3.1 创建基于 python2.7 名字为 Py27 的虚拟环境,顺便安装 pip , numpy 这些软件包
JMY:your_dir your_name $ conda create -n Py27 python=2.7 pip numpy
Solving environment: done
## Package Plan ##
environment location: /Users/your_name/anaconda3/envs/Py27
added / updated specs:
- numpy
- pip
- python=2.7
The following packages will be downloaded:
package | build
---------------------------|-----------------
ca-certificates-2017.08.26 | ha1e5d58_0 264 KB
libcxx-4.0.1 | h579ed51_0 957 KB
libgfortran-3.0.1 | h93005f0_2 495 KB
libcxxabi-4.0.1 | hebd6815_0 149 KB
libedit-3.1 | hb4e282d_0 153 KB
------------------------------------------------------------
Total: 2.0 MB
The following NEW packages will be INSTALLED:
ca-certificates: 2017.08.26-ha1e5d58_0
certifi: 2018.1.18-py27_0
intel-openmp: 2018.0.0-h8158457_8
libcxx: 4.0.1-h579ed51_0
libcxxabi: 4.0.1-hebd6815_0
libedit: 3.1-hb4e282d_0
libffi: 3.2.1-h475c297_4
libgfortran: 3.0.1-h93005f0_2
mkl: 2018.0.1-hfbd8650_4
ncurses: 6.0-hd04f020_2
numpy: 1.14.2-py27ha9ae307_0
openssl: 1.0.2n-hdbc3d79_0
pip: 9.0.1-py27_5
python: 2.7.14-h138c1fe_30
readline: 7.0-hc1231fa_4
setuptools: 38.5.1-py27_0
sqlite: 3.22.0-h3efe00b_0
tk: 8.6.7-h35a86e2_3
wheel: 0.30.0-py27h677a027_1
zlib: 1.2.11-hf3cbc9b_2
Proceed ([y]/n)?
3.2 这里显示了创建指定的虚拟空间需要安装的软件包,选择 y 以继续 :
Proceed ([y]/n)? y
Downloading and Extracting Packages
ca-certificates 2017.08.26: ############################################################################################################################### | 100%
libcxx 4.0.1: ############################################################################################################################################# | 100%
libgfortran 3.0.1: ######################################################################################################################################## | 100%
libcxxabi 4.0.1: ########################################################################################################################################## | 100%
libedit 3.1: ############################################################################################################################################## | 100%
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use:
# > source activate Py27
#
# To deactivate an active environment, use:
# > source deactivate
#
3.3 这里提示创建虚拟 python 环境完成 。
查看创建的虚拟 python 环境 :
JMY:your_dir your_name $ conda env list
# conda environments:
#
base * /Users/your_name/anaconda3
Py27 /Users/your_name/anaconda3/envs/Py27
由上显示可知,Py27 的位置在 anaconda3 的 envs 中,即 ...../anaconda3/envs
使用并释放我们先前创建 python 虚拟环境 Py27 :
JMY:your_dir your_name $ source activate Py27
(Py27) JMY:your_dir your_name$ python
Python 2.7.14 |Anaconda, Inc.| (default, Mar 12 2018, 07:39:26)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print 1+2
3
>>>
(Py27) JMY:your_dir your_name $ source deactivate
JMY:your_dir your_name $