Ubuntu部署Anaconda环境及conda使用

1 Anaconda部署

1.0 下载Anaconda

https://www.anaconda.com/distribution/#linux

1.2 生成Anaconda认证

  • cmd
md5sum /path/Anaconda3-2018.12-Linux-x86_64.sh

其中pathAnaconda下载路径,Anaconda3-2018.12-Linux-x86_64.sh为下载的文件,后面的path也是这个意思.

  • Result
c9af603d89656bc89680889ef1f92623  Anaconda3-2018.12-Linux-x86_64.sh

1.3 安装Anaconda

  • cmd
bash /path/Anaconda3-2018.12-Linux-x86_64.sh
  • Result
Please, press ENTER to continue
>>> 

(1) 按Enter:继续安装

===================================
Anaconda End User License Agreement
===================================

Copyright 2015, Anaconda, Inc.

All rights reserved under the 3-clause BSD License:

(2) 按Ctrl+C:手动选择

Do you accept the license terms? [yes|no]
[no] >>> 

(3) 输入yes:接受BSD License

Do you accept the license terms? [yes|no]
[no] >>> yes
Anaconda3 will now be installed into this location:
/home/xdq/anaconda3

  - Press ENTER to confirm the location
  - Press CTRL-C to abort the installation
  - Or specify a different location below

[/home/xdq/anaconda3] >>> 

(4) 输入Enter:使用默认安装位置(/home/xdq/anaconda3)

PREFIX=/home/xdq/anaconda3
installing: python-3.7.1-h0371630_7 ...
Python 3.7.1
installing: blas-1.0-mkl ...
installing: ca-certificates-2018.03.07-0 ...
installing: conda-env-2.6.0-1 ...
installing: intel-openmp-2019.1-144 ...
installing: libgcc-ng-8.2.0-hdf63c60_1 ...
installing: libgfortran-ng-7.3.0-hdf63c60_0 ...
installing: libstdcxx-ng-8.2.0-hdf63c60_1 ...
.......
Do you wish the installer to initialize Anaconda3
in your /home/xdq/.bashrc ? [yes|no]
[no] >>> 

(5) 输入yes:初始化Anaconda3

Do you wish the installer to initialize Anaconda3
in your /home/xdq/.bashrc ? [yes|no]
[no] >>> yes
Thank you for installing Anaconda3!

(6) 输入no:不安装VSCode(已有其他编辑器)

Do you wish to proceed with the installation of Microsoft VSCode? [yes|no]
>>> no

(7) Anaconda目录

|-- anaconda3
|   |-- LICENSE.txt
|   |-- bin
|   |-- compiler_compat
|   |-- conda-meta
|   |-- doc
|   |-- envs
|   |-- etc
|   |-- include
|   |-- lib
|   |-- libexec
|   |-- man
|   |-- mkspecs
|   |-- phrasebooks
|   |-- pkgs
|   |-- plugins
|   |-- qml
|   |-- resources
|   |-- sbin
|   |-- share
|   |-- ssl
|   |-- translations
|   |-- var
|   |-- vscode_inst.py.log
|   `-- x86_64-conda_cos6-linux-gnu

4 Anaconda使用

4.1 查看环境

conda info --envs
  • Result
# conda environments:
#
base                  *  /home/xdq/anaconda3

4.2 新建环境

4.2.1 默认python版本新建

conda create  --name env_name

其中env_name为环境名称.

conda create  --name py37cpu
  • Result 1
Solving environment: done
==> WARNING: A newer version of conda exists. <==
  current version: 4.5.12
  latest version: 4.6.8
Please update conda by running
    $ conda update -n base -c defaults conda
## Package Plan #
  environment location: /home/xdq/anaconda3/envs/py37cpu
Proceed ([y]/n)? 

输入:y

  • Result 2
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use
# 启动新建的py37cpu环境
#     $ conda activate py37cpu
#
# To deactivate an active environment, use
# 关闭py37cpu环境
#     $ conda deactivate

4.2.2 指定python版本新建

conda create -n env_name python=2.x
conda create -n env_name python=3.x

4.2.3 conda环境启动与关闭

启动环境

conda activate py37cpu
(py37cpu) xdq@xdq:~/anaconda3/env_pkg$

关闭环境

conda deactivate

进入base环境

(base) xdq@xdq:~/anaconda3/env_pkg$

查看此时Anaconda环境

conda info --envs
  • Result
# conda environments:
#
base                  *  /home/xdq/anaconda3
py37cpu                  /home/xdq/anaconda3/envs/py37cpu

一个是base环境(默认环境),一个是新建环境.

4.3 安装模块

4.3.1 查看安装模块

conda list
  • Result
# packages in environment at /home/xdq/anaconda3:
#
# Name                    Version                   Build  Channel
_ipyw_jlab_nb_ext_conf    0.1.0                    py37_0  
alabaster                 0.7.12                   py37_0  
anaconda                  2018.12                  py37_0  
anaconda-client           1.7.2                    py37_0  
anaconda-navigator        1.9.6                    py37_0  
anaconda-project          0.8.2                    py37_0
....

4.3.2 查看安装模块的仓库

conda list --explicit
  • Result
# This file may be used to create an environment using:
# $ conda create --name <env> --file <this file>
# platform: linux-64
@EXPLICIT
https://repo.anaconda.com/pkgs/main/linux-64/blas-1.0-mkl.tar.bz2
https://repo.anaconda.com/pkgs/main/linux-64/ca-certificates-2018.03.07-0.tar.bz2
https://repo.anaconda.com/pkgs/main/linux-64/conda-env-2.6.0-1.tar.bz2
....

4.3.3 将模块仓库文件导出到当前目录

conda list --explicit > spec-file.txt

若当前目录:/home/xdq/anaconda3
查看文件目录:

|-- anaconda3
...
|   |-- spec-file.txt

4.3.4 使用导出的仓库文件安装模块

conda install --name env_name --file spec-file.txt

4.3.5 安装模块

指定版本并安装单个模块

conda install -n python3.x scipy

指定版本并安装多个模块及指定模块版本

conda install -n python3.x scipy=0.15.0 babel

4.4 conda环境yml文件

4.4.1 导出

进入环境

conda activate py37cpu

导出yml文件

conda env export > enviorment.yml
  • Result
name: base
channels:
  - defaults
dependencies:
  - _ipyw_jlab_nb_ext_conf=0.1.0=py37_0
  - alabaster=0.7.12=py37_0
  - anaconda=2018.12=py37_0
  - anaconda-client=1.7.2=py37_0
  - anaconda-navigator=1.9.6=py37_0
  - anaconda-project=0.8.2=py37_0
  - asn1crypto=0.24.0=py37_0
  - astroid=2.1.0=py37_0
  - astropy=3.1=py37h7b6447c_0
  - atomicwrites=1.2.1=py37_0
  - attrs=18.2.0=py37h28b3542_0
.....

yml文件即当前环境的依赖(包).

4.4.2 使用yml安装模块

安装yml文件

conda env create -f enviroment.yml

4.5 clone已存在的环境

conda create --name 4caffe2 --clone py37cpu

py37cpu环境属性clone为新的环境4caffe2

Source:      /home/xdq/anaconda3/envs/py37cpu
Destination: /home/xdq/anaconda3/envs/4caffe2
Packages: 29
Files: 0
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use
#
#     $ conda activate 4caffe2
#
# To deactivate an active environment, use
#
#     $ conda deactivate

4.5 移除conda虚拟环境

conda remove --name py37cpu --all
Remove all packages in environment /home/xdq/anaconda3/envs/4caffe2:
## Package Plan ##
  environment location: /home/xdq/anaconda3/envs/4caffe2
Proceed ([y]/n)? 

输入:y

5 总结

(1)Anaconda是python的环境管理器,和虚拟环境(virtualenv)功能类似;

说明 Anaconda virtualenv
新建环境 conda create --name env_name python=3.x virtualenv -p /usr/bin/python3.x env_name
启动 conda activate env_name source path/bin/activate env_name
停止 conda deactivate deactivate

(2) Anaconda具备软件安装能力(这一点比virtualenv强),该功能类似于pip;

说明 Anaconda pip
安装模块 conda install -n pkg_name pip install pkg_name
卸载模块 conda remove -n env_name pkg_name pip uninstall pkg_name
查看安装模块 conda list pip list

(3) Anadconda功能强大,融合virtualenvpip.
(4) 切换Python环境:安装了Anaconda之后,终端输入python会直接进入Anacondapython环境:

Python 3.7.1 (default, Dec 14 2018, 19:28:38) 
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.

若需要进入其他版本及原先的python环境,可指定版本运行,如:python3.6,python2.7


[参考文献]
[1]https://docs.anaconda.com/anaconda/install/linux/
[2]https://conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html


猜你喜欢

转载自blog.csdn.net/Xin_101/article/details/88624726