conda常用指令
| 功能 | 命令 | 说明 |
|---|---|---|
| 环境管理 | conda create - n 环境名 python=版本号 | 创建新环境(可指定 Python 版本) |
| conda activate 环境名 | 激活环境(Windows 直接使用,macOS/Linux 需 source ) | |
| conda deactiva te | 退出当前环境 | |
| conda env list 或 conda info -e | 查看所有环境 | |
| conda remove - n 环境名 --all | 删除环境(谨慎操作) | |
| 包管理 | conda install 包名 python=3.8 nodejs=24.4 | 安装包(默认从官方源) |
| conda install -c 频道名 包名 | 从指定频道(如 conda-forge )安装 | |
| conda update 包 名 | 更新包 | |
| conda remove 包 名 | 卸载包 | |
| conda list | 查看当前环境已安装的包 | |
| 高级操作 | conda env expo rt > environment.yml | 导出环境配置(用于跨平台迁移) |
| conda env crea te -f environment.yml | 通过 YAML 文件创建环境 |
conda在树莓派上的使用
在树莓派上使用 Conda 需要注意架构适配(树莓派是 ARM 架构,x86 版本的 Conda 无法直接用),主流方案是使用 Miniforge(专为 ARM 等非主流架构适配的 Conda 发行版),而非官方 Anaconda/Miniconda(仅支持 x86)。以下是完整步骤:
一、前提准备
- 树莓派系统要求:推荐 Raspberry Pi OS (64-bit)(Bullseye/Buster 均可),32 位系统也支持但兼容性稍差。
- 先更新系统依赖:
sudo apt update && sudo apt upgrade -y
sudo apt install -y wget bzip2 libgl1-mesa-glx # 安装基础依赖
二、安装 Miniforge(ARM 版 Conda)
Miniforge 是 Conda 的轻量级分支,内置 conda-forge 源,完美支持 ARM 架构。
步骤 1:下载 Miniforge 安装包
根据树莓派系统位数选择(推荐 64 位):
# 64 位 Raspberry Pi OS (aarch64)
wget https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-Linux-aarch64.sh
# 32 位 Raspberry Pi OS (armv7l)
wget https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-Linux-armv7l.sh
步骤 2:执行安装脚本
# 64 位(32 位替换为对应的 .sh 文件名)
bash Miniforge3-Linux-aarch64.sh
安装过程中注意:
- 按回车阅读许可,输入
yes同意; - 选择安装路径(默认
~/miniforge3,推荐保留默认); - 最后会问是否 “初始化 Conda”(
Do you wish the installer to initialize Miniforge3 by running conda init?),输入yes(否则每次终端启动需要手动激活 Conda)。
步骤 3:生效 Conda 环境
关闭当前终端,重新打开,或执行:
source ~/.bashrc
# bash 终端
# 若用 zsh:source ~/.zshrc
此时终端前缀会出现 (base),说明 Conda 已激活。
三、基础 Conda 操作(和 x86 版一致)
1. 管理环境
# 创建新环境(指定 Python 版本,例如 3.9)
conda create -n myenv python=3.9
# 激活环境
conda activate myenv
# 退出环境
conda deactivate
# 删除环境
conda remove -n myenv --all
2. 安装 / 卸载包
# 激活环境后安装包
conda install numpy pandas # 安装常用包
conda install tensorflow -c conda-forge # 指定源安装
# 卸载包
conda remove numpy
# 更新包
conda update numpy
# 列出已安装包
conda list
3. 配置 Conda 源(加速国内下载)
默认 conda-forge 源在国内较慢,可替换为清华镜像(ARM 架构适配):
# 编辑 .condarc 文件
vim ~/.condarc
替换为以下内容(删除原有内容):
channels:
- defaults
show_channel_urls: true
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
msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
pytorch-lts: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
保存后执行 conda clean -i 清除缓存,后续安装包会走清华源。
四、注意事项
- ARM 包兼容性:部分 x86 专属包(如某些闭源库)无 ARM 版本,优先用
conda-forge源(conda install -c conda-forge 包名),若仍找不到可尝试pip安装(激活 Conda 环境后直接pip install 包名)。 - 性能优化:树莓派内存有限,创建环境时尽量指定 Python 小版本(如 3.9.18),避免安装冗余依赖;可关闭 Conda 自动激活 base 环境:
conda config --set auto_activate_base false
- 卸载 Miniforge:若需卸载,直接删除安装目录 + 清理配置:
rm -rf ~/miniforge3
rm -rf ~/.condarc ~/.conda ~/.continuum
# 编辑 ~/.bashrc 删掉 Conda 初始化的代码段
五、常见问题解决
- 终端无
(base)前缀:重新执行source ~/miniforge3/bin/activate,再conda init。 - 安装包时报错
UnsatisfiableError:降低包版本(如conda install numpy=1.23),或换conda-forge源。 - 树莓派 32 位系统安装失败:确认下载的是
armv7l版本,且系统依赖完整(sudo apt install libssl-dev)。