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

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,尤其是和自己使用的保持一致
  • 启动或关闭环境
# 激活环境
source activate
# 退出环境
source deactivate

#激活环境
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

7、实现切换用户,自动切换python虚拟环境

安装完成后,  在.bashrc文件添加如下代码, 实现切换用户自动启用python虚拟环境

# >>> 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 <<<
发布了173 篇原创文章 · 获赞 326 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/William0318/article/details/104643336