Linux系統CentOS虛擬機利用conda搭建python虛擬環境

Conda是一個開源的軟件包管理系統和環境管理系統,用於安裝多個版本的軟件包及其依賴關係,並在它們之間輕鬆切換。Conda主要用於Python程序,適用於Linux,OS X和Windows,也可以打包和分發其他軟件。是目前最流行的 Python 環境管理工具 。Conda是一個開源的軟件包管理系統和環境管理系統,用於安裝多個版本的軟件包及其依賴關係,並在它們之間輕鬆切換。Conda主要用於Python程序,適用於Linux,OS X和Windows,也可以打包和分發其他軟件。是目前最流行的 Python 環境管理工具 。

1、安裝

官網:https://docs.conda.io/en/latest/miniconda.html

wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
sh Miniconda3-latest-Linux-x86_64.sh

安裝完成後會在.bashrc文件添加如下代碼

# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/home/user/anaconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
    eval "$__conda_setup"
else
    if [ -f "/home/user/anaconda3/etc/profile.d/conda.sh" ]; then
# . "/home/user/anaconda3/etc/profile.d/conda.sh"  # commented out by conda initialize
    else
# export PATH="/home/user/anaconda3/bin:$PATH"  # commented out by conda initialize
    fi
fi
unset __conda_setup
# <<< conda initialize <<<

 

2、設置別名

alias conda='/path/to/software/conda/conda3/bin/conda'
alias actative='source /path/to/software/conda/conda3/bin/activate'
alias deactative='source /path/to/software/conda/conda3/bin/deactivate'

3、驗證是否安裝成功

# conda -h
usage: conda [-h] [-V] command ...

conda is a tool for managing and deploying applications, environments and packages.

Options:

positional arguments:
  command
    clean        Remove unused packages and caches.
    config       Modify configuration values in .condarc. This is modeled
                 after the git config command. Writes to the user .condarc
                 file (/home/dev/.condarc) by default.
    create       Create a new conda environment from a list of specified
                 packages.
    help         Displays a list of available conda commands and their help
                 strings.
    info         Display information about current conda install.
    init         Initialize conda for shell interaction. [Experimental]
    install      Installs a list of packages into a specified conda
                 environment.
    list         List linked packages in a conda environment.
    package      Low-level conda package utility. (EXPERIMENTAL)
    remove       Remove a list of packages from a specified conda environment.
    uninstall    Alias for conda remove.
    run          Run an executable in a conda environment. [Experimental]
    search       Search for packages and display associated information. The
                 input is a MatchSpec, a query language for conda packages.
                 See examples below.
    update       Updates conda packages to the latest compatible version.
    upgrade      Alias for conda update.

optional arguments:
  -h, --help     Show this help message and exit.
  -V, --version  Show the conda version number and exit.

conda commands available from other packages:
  env

 4、添加頻道

  • 頻道
#官方channel:
conda config --add channels bioconda
conda config --add channels conda-forge

#清華鏡像:
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/
  • 查看頻道
#顯示安裝的頻道
 conda config --set show_channel_urls yes 
#查看已經添加的頻道
conda config --get channels
vim ~/.condarc

5、環境管理

  • 查看環境
# 查看已安裝的python環境
conda info -e  #conda info --envs
conda env list
#當前環境會通過一個星號 (*) 標識
  • 創建環境
#基於python3.6版本創建一個名字爲test的python獨立環境
conda create --name test python=3.6 

#指定python2版本
conda create -n test2 python=2

#指定環境路徑
conda create --prefix=/path/to/py36 python=3.6 #注-p/--prefix和-n/--name參數不能同時用

#如果不指定python,安裝會默認爲conda自帶的python版本,即如果安裝的是conda2,就是python2,如果是conda3,就是python3.
#最好是每個環境指定python,尤其是和自己使用的保持一致
  • 啓動或關閉環境
#激活環境
conda activate(後接環境名,不加默認爲base)
conda activate test
#退出環境
conda deactivate test
#PS:若未加入環境變量,需進入conda的bin目錄下執行
  • 刪除環境
conda env remove -n test
conda remove -n test --all
  • 重命名環境
即先克隆,再刪除
conda create -n python2 --clone py2
conda remove -n py2 --all
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章