conda和virtualenv一样,都是用于python项目做多版本环境创建与切换的,也就是在同一个环境中,可以存在几个不同python版本或者不同requirement的虚拟环境,下面简单介绍一下miniconda的安装以及使用
OS
- Linux
- Mac OS X
- Windows(此处不做讲解)
Setup
- Source Download:https://conda.io/miniconda.html
以Mac为例,其实和linux安装步骤一模一样
- 安装
# 获取安装文件
cd ~/
wget https://repo.continuum.io/miniconda/Miniconda2-latest-MacOSX-x86_64.sh
# 安装miniconda,根据提示完成安装
cd ~/
bash Miniconda2-latest-MacOSX-x86_64.sh
# 需新建Terminal Session或手动加载环境变量来使得conda生效
# 手动加载
source ~/.bash_profie
- 验证安装成功
mymac:~ mymac$ conda -h
usage: conda [-h] [-V] command ...
conda is a tool for managing and deploying applications, environments and packages.
Options:
.....
- 升级conda以及pip version
conda upgrade conda
pip install --upgrade pip
- 查看当前env list,由于新建conda,所以env默认只有base
mymac:~ mymac$ conda-env list
# conda environments:
#
base * /Users/mymac/miniconda2
创建虚拟环境
- 使用
conda create
创建虚拟环境,比如创建一个名字为djangocms,python version为2.7的虚拟环境:
# 注意:这里最好指定python的版本,例如python=2.7
# 不然系统会直接使用global python version
# 并且把你所安装的依赖包全部放在global env下面,不利于你对python虚拟环境的隔离
conda create -n djangocms python=2.7 -y
#创建完毕后注意看提示
#
# To activate this environment, use:
# > source activate djangocms
#
# To deactivate an active environment, use:
# > source deactivate
#
# 查看env list
onda-env list
# conda environments:
#
base * /Users/xy920/miniconda2
djangocms /Users/mymac/miniconda2/envs/flaskcms
- 使用
source activate/deactivate
来激活和退出虚拟环境
# activate
mymac:~ mymac$ source activate djangocms
(djangocms) mymac:~ mymac$ pip freeze
certifi==2018.1.18
(djangocms) mymac:~ mymac$
# 激活虚拟环境之后你就可以在这里边安装依赖包做进一步开发了
# 使用`pip`安装依赖包,比如`djangocms-installer`
(djangocms) mymac:~ mymac$ pip install djangocms-installer
(djangocms) mymac:~ mymac$ pip freeze
certifi==2018.1.18
dj-database-url==0.5.0
djangocms-installer==1.0.0
pytz==2018.3
six==1.11.0
tzlocal==1.5.1
# 退出djangocms虚拟环境
(djangocms) mymac:~ mymac$ source deactivate
mymac:~ mymac$
卸载miniconda
#去掉~/.bash_profile环境变量配置
# added by Miniconda2 installer
export PATH="/Users/xy920/miniconda2/bin:$PATH"
#删除miniconda
rm -rf ~/miniconda2
# 去掉相关隐藏文件
rm -rf ~/.condarc ~/.conda ~/.continuum