Miniconda 使用

简介

conda 和 Anaconda、Miniconda 的关系

  • conda 是一个环境管理器。
  • Anaconda 和 Miniconda 都是一种 Python 和 R 发行版,其包括了 conda 工具。
  • Miniconda 是 Anaconda 的一个精简版。

conda 和 Python 的关系

  • conda 负责管理多个 Python 环境。

此外,你可能还听说过 Miniforge,这是一个由社区维护的发行版。Miniforge 和 Miniconda 在使用上的主要区别在于前者使用 conda-forge 作为默认频道。两者都提供了 conda 命令。

此外还有 Mamba,这是为了解决 conda 在解析依赖关系时速度过慢的问题而产生的项目。目前 Miniforge 已经包含了 Mamba。

参见:What is the difference between miniconda and miniforge? | Stack Overflow

conda 包搜索:anaconda.org

安装

如果只需要 condapyhton,我们安装 Miniconda 就可以了:

  • Ubuntu:

    wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O miniconda.sh
    bash miniconda.sh -bup ~/.local/opt/miniconda3
    rm miniconda.sh
    ~/.local/opt/miniconda3/bin/conda init "$(basename ${SHELL})"
    
  • macOS:

    brew install miniconda
    conda init "$(basename ${SHELL})"
    
  • Windows:

    curl https://repo.anaconda.com/miniconda/Miniconda3-latest-Windows-x86_64.exe -o miniconda.exe
    Start-Process -FilePath ".\miniconda.exe" -ArgumentList "/S" -Wait
    del miniconda.exe
    

参考:Installing Miniconda | Anaconda documentation

卸载

清理配置文件和缓存数据:

conda install anaconda-clean
anaconda-clean --yes

删除 Miniconda:

rm -rf "$(dirname $(conda info --base))"

使用

环境管理

安装 conda 时会自动创建一个 base 环境。为了避免包版本冲突,我们最好创建新的环境并在其中安装包。

创建新的 conda 环境:

conda create -n myenv python=3.8

许多科学计算库并不立即支持最新版本的 Python。因此,在安装 PyTorch 时,建议使用比 Python 版本早一到两个版本的 Python。例如,如果 Python 的最新版本是 3.13,建议使用 Python 3.11 或 3.12。

conda activate myenv             # 激活 conda 环境
conda deactivate                 # 停用当前激活的 conda 环境
conda info --envs                # 列出所有 conda 环境
conda remove -n myenv --all      # 删除一个 conda 环境

包管理

conda install numpy        # 安装包到当前激活的环境
conda install numpy=2.2.2  # 指定包版本
conda remove numpy         # 从当前激活的环境中卸载包
conda update numpy         # 更新当前激活环境中的包
conda update conda         # 更新 conda 自身
conda list                 # 列出当前激活环境中的所有包
conda search numpy         # 搜索包

导出环境

conda info                                          # 查看当前激活环境的详细信息
conda env export --from-history -f environment.yml  # 将当前环境导出到文件
conda env create -f environment.yml                 # 从文件创建环境

参见:Managing environments | conda documentation

清理缓存

conda clean -i     # 清理索引缓存
conda clean --all  # 清理所有缓存

换源

  1. 编辑 ~/.condarc

    channels:
      - defaults
    default_channels:
      - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
      - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
      - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
    custom_channels:
      conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
      pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
    
  2. 清除索引缓存:

    conda clean -i
    

杂项

常用环境变量

export CONDA_ENVS_PATH="/path/to/envs"  # 设置附加环境搜索路径

.condarc

# 指定 channels。channels 是 conda 下载包的来源
channels:
  - conda-forge
  - defaults

# 设定 channel 优先级
channel_priority: flexible

# 创建环境时默认安装的包
create_default_packages:
  - numpy
  - pandas

# 启用/禁用自动更新 conda
auto_update_conda: true

# 启用/禁用包的缓存
pkgs_dirs:
  - ~/.conda/pkgs

# 指定环境的默认位置
envs_dirs:
  - ~/.conda/envs

# 启用/禁用代理设置(如果需要)
# ssl_verify 可以设置为 false 来禁用 SSL 验证(不推荐)
ssl_verify: true

# 设定下载包的最大重试次数
remote_max_retries: 3

参考:Using the .condarc conda configuration file — conda 25.3.1 documentation

Troubleshooting

安装软件包时报错 CondaValueError

$ conda install conda-pack
Error while loading conda entry point: conda-libmamba-solver (module 'libmambapy' has no attribute 'QueryFormat')

CondaValueError: You have chosen a non-default solver backend (libmamba) but it was not recognized. Choose one of: classic

原因:在 base 环境中混用了来自 conda-forgedefaults 的软件包。

解决方法:因为我当时用的是云服务器,所以我直接更换镜像了。具体解决方法请参考 Stack Overflow。

参考:Solve conda-libmamba-solver (libarchive.so.19) error after updating conda to 23.11.0 without reinstalling conda? | Stack Overflow

无法通过 environment.yml 文件创建环境

我在 Mac 上使用下面的命令导出了 conda 环境:

conda env export -f environment.yml

得到的环境配置如下:

name: myenv
channels:
  - defaults
dependencies:
  - anyio=4.6.2.post1=pyhd8ed1ab_0
  ...
  - zstd=1.5.6=hb46c0d2_0
  - pip:
      - pyzipper==0.3.6
      ...
      - wsproto==1.2.0
prefix: /opt/homebrew/Caskroom/miniforge/base/envs/myenv

在 Ubuntu 上安装环境时得到如下错误:

Channels:
 - defaults
Platform: linux-64
Collecting package metadata (repodata.json): done
Solving environment: failed
Channels:
 - defaults
Platform: linux-64
Collecting package metadata (repodata.json): done
Solving environment: failed

LibMambaUnsatisfiableError: Encountered problems while solving:
  - nothing provides requested zstd ==1.5.6 hb46c0d2_0
  ...
  - nothing provides __osx needed by terminado-0.18.1-pyh31c8845_0

Could not solve for environment specs
The following packages are incompatible
├─ argon2-cffi-bindings ==21.2.0 py312h024a12e_5 does not exist (perhaps a typo or a missing channel);
├─ ...
├─ terminado ==0.18.1 pyh31c8845_0 is not installable because it requires
│  └─ __osx, which is missing on the system;
└─ zstd ==1.5.6 hb46c0d2_0 does not exist (perhaps a typo or a missing channel).

可以看到其中一些报错显示无法安装的原因是缺少 _osx,并且可以看到我们的 environment.yml 文件把 conda 包的版本号写的很死,个人猜测这些版本号指向了 Mac 特有的版本。因此,我们不能让 conda 指定详细的版本号。

解决方法如下,在 env export 命令中添加 --from-history 选项,让 conda 只导出我们手动安装的包:

conda env export --from-history -f environment.yml

导出结果如下:

name: myenv
channels:
  - defaults
dependencies:
  - python=3.12
  ...
  - rarfile
prefix: /opt/homebrew/Caskroom/miniforge/base/envs/myenv

可以看到这样导出的环境就不带版本号了(除非安装时手动指定)。此时即可成功在 Ubuntu 上创建环境。

posted @ 2024-06-26 01:35  Undefined443  阅读(448)  评论(0)    收藏  举报