【Python】conda虚拟环境创建及使用

文章目录

  • 0.前言
  • 1.Miniconda安装
  • 2.conda本地基本操作
  • 3.创建conda虚拟环境
  • 4.激活conda虚拟环境
  • 5.退出conda虚拟环境
  • 6.删除conda虚拟环境
  • 7.克隆conda虚拟环境
  • 0.前言

    conda 是一个开源的包、环境管理器,可以用于在同一个机器上创建不同的虚拟环境,安装不同 Python 版本的软件包及其依赖,并能够在不同的虚拟环境之间切换。Conda常通过安装Anaconda/Miniconda来进行使用。一般使用Miniconda就够了。

    1.Miniconda安装

    下载Miniconda安装包

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

    安装Miniconda

    bash Miniconda3-latest-Linux-x86_64.sh
    ...
    Please answer 'yes' or 'no':'
    >>> yes
    ...
    Miniconda3 will now be installed into this location:
    /root/miniconda3
    
      - Press ENTER to confirm the location
      - Press CTRL-C to abort the installation
      - Or specify a different location below
    
    [/root/miniconda3] >>> 
    ...
    You can undo this by running `conda init --reverse $SHELL`? [yes|no]
    [no] >>> yes
    

    配置国内镜像源

    conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
    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/r
    conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
    conda config --set show_channel_urls yes
    

    到此,安装完成。

    2.conda本地基本操作

    获取版本号

    conda --version 或 conda -V
    

    检查更新当前conda

    conda update conda
    

    查看当前存在哪些虚拟环境

    conda env list 或 conda info -e 或 conda info --envs
    

    查看/安装/更新/删除包

    conda list:
    conda search package_name
    conda install package_name
    conda install package_name=1.5.0
    conda update package_name
    conda remove package_name
    

    3.创建conda虚拟环境

    创建名为your_env_name的环境

    conda create --name your_env_name
    

    创建制定python版本的环境

    conda create --name your_env_name python=3.12
    

    创建包含某些包的环境

    conda create --name your_env_name numpy scipy
    

    创建指定python版本下包含某些包的环境

    conda create --name your_env_name python=3.12 numpy scipy
    

    4.激活conda虚拟环境

    Linux

     conda activate your_env_name
    

    Windows

    activate your_env_name
    

    5.退出conda虚拟环境

    Linux

    conda deactivate
    

    Windows

    deactivate env_name
    

    6.删除conda虚拟环境

    conda remove -n your_env_name --all
    conda remove --name your_env_name --all
    

    7.克隆conda虚拟环境

    conda create --name new_env_name --clone old_env_name
    

    作者:鸟哥大大

    物联沃分享整理
    物联沃-IOTWORD物联网 » 【Python】conda虚拟环境创建及使用

    发表回复