使用 conda 管理 pytorch 的 python 开发环境

文章目录

    更新推荐:为了避免律师函警告,从 miniconda 切换到 miniforge

    目录

    • [X] 查看已有环境
    • [X] 新建一个开发环境
    • [X] 切换开发环境
    • [X] 开发环境配置文件导出
    • [X] 到一个新开发机,由配置文件重建一个开发环境
    • [X] 使用国内的镜像源加速下载

    查看已有环境

    conda 安装好后,可以通过指令 conda info -e 或者 conda env list 查看已有环境情况。
    env list 这个命令相对容易记忆。

    (base) PS D:\work> conda info -e
    # conda environments:
    #
    base                  *  D:\app\miniconda
    
    (base) PS D:\work> conda env list
    # conda environments:
    #
    base                  *  D:\app\miniconda
    

    新建一个开发环境

    例如,基于 base 环境,新建一个开发环境,名为 wheat:

    conda create -n wheat --clone base
    

    如果不想基于 base,省略掉 clone 参数即可

    conda create -n wheat
    

    也可以指定不同的 python 版本,这就非常实用了,方便管理一台开放机多个 python 版本的情况:

    conda create -n wheat python=3.10
    

    此时查看已有环境,就会发现多了一个环境:

    (base) PS D:\work> conda env list
    # conda environments:
    #
    base                  *  D:\app\miniconda
    wheat                    D:\app\miniconda\envs\wheat
    

    切换开发环境

    例如,从当前环境 base 切换到 wheat:

    1. 先 deactivate
    2. 再 activate 指定环境

    按 Tab, conda 会自动补全命令,不用担心记不住命令。

    (base) PS D:\work> conda deactivate
    
    PS D:\work> conda activate wheat
    
    (wheat) PS D:\work> conda info
    
         active environment : wheat
        active env location : D:\app\miniconda\envs\wheat
                shell level : 1
           user config file : C:\Users\Lenovo\.condarc
     populated config files :
              conda version : 4.14.0
        conda-build version : not installed
             python version : 3.9.12.final.0
    

    开发环境配置文件导出

    例如,我想讲当前配置好的开发环境的依赖信息,导出到一个配置文件。方便在其他开发机上部署:

    (wheat) PS D:\work> conda env export --file wheat.yml
    

    查看内容:

    (wheat) PS D:\work> cat .\wheat.yml
    name: wheat
    channels:
      - pytorch
      - defaults
    dependencies:
      - blas=1.0=mkl
      - brotli=1.0.9=h2bbff1b_7
      - brotli-bin=1.0.9=h2bbff1b_7
      - brotlipy=0.7.0=py39h2bbff1b_1003
      ...
      - pysocks=1.7.1=py39haa95532_0
      - python=3.9.12=h6244533_0
      - python-dateutil=2.8.2=pyhd3eb1b0_0
      - pytorch=1.12.1=py3.9_cpu_0
      - pytorch-mutex=1.0=cpu
      ...
    

    里面记录了具体的 python 版本,及相关依赖包的版本。

    到一个新开发机,由配置文件重建一个开发环境

    conda env create -f wheat.yml
    

    使用国内的镜像源加速下载

    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 / miniconda / anaconda

    • Anaconda: 水蟒,蟒蛇
    • conda
    • miniconda: Miniconda is a free minimal installer for conda. It is a small, bootstrap version of Anaconda that includes only conda, Python, the packages they depend on, and a small number of other useful packages, including pip, zlib and a few others.

    加到 Windows Terminal 中管理

    %windir%\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy ByPass -NoExit -Command "& 'D:\app\miniconda\shell\condabin\conda-hook.ps1' ; conda activate 'D:\app\miniconda' "
    

    关于作者 🌱

    我是来自山东烟台的一名开发者,有感兴趣的话题,或者软件开发需求,欢迎加微信 zhongwei 聊聊,或者关注我的个人公众号“大象工具”, 查看更多联系方式