20260608160402
This commit is contained in:
1 parent
1fd956966c
commit
930f03954a
339 files changed
+2175
-140695
No files matched your search
@@ -1,45 +1,269 @@
|
||||
# Conda(Python环境与包管理)
|
||||
# Conda(Python 环境与包管理)
|
||||
|
||||
## 介绍
|
||||
Anaconda 是一个免费、易于安装的包管理器、环境管理器和 Python 发行版,包含 1,500 多个开源包,并提供 免费社区支持。Anaconda 与平台无关,因此无论您在 Windows、macOS 还是 Linux 上都可以使用它。
|
||||
Conda 用来管理 Python 版本、虚拟环境和依赖包。做 AI、数据处理、OCR、深度学习项目时,建议每个项目单独创建一个环境,避免依赖互相污染。
|
||||
|
||||
## 安装
|
||||
下载地址:https://www.anaconda.com/download/success?reg=skipped
|
||||
|
||||
下载 Miniconda 即可
|
||||
推荐安装 Miniconda,体积更小,后续按需安装依赖即可。
|
||||
|
||||
- Miniconda:https://docs.conda.io/projects/miniconda/en/latest/
|
||||
- Anaconda:https://www.anaconda.com/download/success?reg=skipped
|
||||
|
||||
### Windows
|
||||
|
||||
下载 exe 文件按照步骤安装即可。
|
||||
下载 `.exe` 安装包后按步骤安装。安装完成后,如果命令行无法识别 `conda`,将下面目录加入系统环境变量:
|
||||
|
||||
#### 设置环境变量
|
||||
|
||||
将 `D:\devtool\anaconda3\condabin` 添加到环境变量中即可
|
||||
|
||||
|
||||
## 常用命令
|
||||
|
||||
**创建环境**
|
||||
```bash
|
||||
conda create -n myenv python=3.9
|
||||
```text
|
||||
D:\devtool\anaconda3\condabin
|
||||
```
|
||||
|
||||
**激活环境**
|
||||
检查是否安装成功:
|
||||
|
||||
```bash
|
||||
conda --version
|
||||
```
|
||||
|
||||
初始化 Shell:
|
||||
|
||||
```bash
|
||||
conda init
|
||||
```
|
||||
|
||||
:::tip
|
||||
执行 `conda init` 后,关闭并重新打开终端再使用 `conda activate`。
|
||||
:::
|
||||
|
||||
## 环境管理
|
||||
|
||||
### 创建环境
|
||||
|
||||
```bash
|
||||
# 创建指定 Python 版本的环境
|
||||
conda create -n myenv python=3.10
|
||||
|
||||
# 创建环境并同时安装常用包
|
||||
conda create -n myenv python=3.10 numpy pandas
|
||||
```
|
||||
|
||||
### 激活与退出
|
||||
|
||||
```bash
|
||||
# 激活环境
|
||||
conda activate myenv
|
||||
```
|
||||
|
||||
**退出环境**
|
||||
```bash
|
||||
# 退出当前环境
|
||||
conda deactivate
|
||||
```
|
||||
|
||||
**删除环境**
|
||||
### 查看环境
|
||||
|
||||
```bash
|
||||
# 查看所有环境
|
||||
conda env list
|
||||
|
||||
# 或者
|
||||
conda info --envs
|
||||
```
|
||||
|
||||
### 删除环境
|
||||
|
||||
```bash
|
||||
conda remove -n myenv --all
|
||||
```
|
||||
|
||||
**查看所有环境**
|
||||
:::warning
|
||||
删除环境会移除该环境里的 Python 和所有依赖包,执行前确认环境名是否正确。
|
||||
:::
|
||||
|
||||
## 包管理
|
||||
|
||||
```bash
|
||||
conda env list
|
||||
```
|
||||
# 安装包
|
||||
conda install numpy
|
||||
|
||||
# 安装指定版本
|
||||
conda install python=3.10
|
||||
conda install numpy=1.26
|
||||
|
||||
# 从指定 channel 安装
|
||||
conda install -c conda-forge opencv
|
||||
|
||||
# 更新包
|
||||
conda update numpy
|
||||
|
||||
# 删除包
|
||||
conda remove numpy
|
||||
|
||||
# 查看当前环境已安装的包
|
||||
conda list
|
||||
```
|
||||
|
||||
如果 Conda 找不到包,可以在当前环境中使用 `pip`:
|
||||
|
||||
```bash
|
||||
pip install 包名
|
||||
```
|
||||
|
||||
:::tip
|
||||
同一个环境里可以混用 `conda` 和 `pip`,但建议优先用 `conda` 安装底层依赖较重的包,例如 `numpy`、`opencv`、`pytorch`。
|
||||
:::
|
||||
|
||||
## 镜像源
|
||||
|
||||
网络较慢时可以配置清华镜像源。
|
||||
|
||||
### Conda 源
|
||||
|
||||
```bash
|
||||
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/cloud/conda-forge
|
||||
conda config --set show_channel_urls yes
|
||||
```
|
||||
|
||||
查看当前配置:
|
||||
|
||||
```bash
|
||||
conda config --show channels
|
||||
```
|
||||
|
||||
恢复默认源:
|
||||
|
||||
```bash
|
||||
conda config --remove-key channels
|
||||
```
|
||||
|
||||
### pip 源
|
||||
|
||||
Conda 环境里经常也会使用 `pip install`。`pip` 使用的是 PyPI 源,和上面的 Conda channel 不是同一套配置。
|
||||
|
||||
参考:清华大学开源软件镜像站 PyPI 帮助页:https://mirrors.tuna.tsinghua.edu.cn/help/pypi/
|
||||
|
||||
临时使用清华 PyPI 源安装某个包:
|
||||
|
||||
```bash
|
||||
pip install -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple some-package
|
||||
```
|
||||
|
||||
:::tip
|
||||
地址末尾的 `simple` 不能省略;`pip` 要求使用 `https`。
|
||||
:::
|
||||
|
||||
设置为默认 PyPI 源:
|
||||
|
||||
```bash
|
||||
python -m pip install --upgrade pip
|
||||
pip config set global.index-url https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple
|
||||
```
|
||||
|
||||
如果默认源访问很慢,可以临时使用清华源升级 `pip`:
|
||||
|
||||
```bash
|
||||
python -m pip install -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple --upgrade pip
|
||||
```
|
||||
|
||||
查看当前 `pip` 配置:
|
||||
|
||||
```bash
|
||||
pip config list
|
||||
```
|
||||
|
||||
取消全局 PyPI 源配置:
|
||||
|
||||
```bash
|
||||
pip config unset global.index-url
|
||||
```
|
||||
|
||||
配置额外源:
|
||||
|
||||
```bash
|
||||
pip config set global.extra-index-url "https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple"
|
||||
```
|
||||
|
||||
## 环境导出与迁移
|
||||
|
||||
### 导出环境
|
||||
|
||||
```bash
|
||||
conda env export > environment.yml
|
||||
```
|
||||
|
||||
### 根据文件创建环境
|
||||
|
||||
```bash
|
||||
conda env create -f environment.yml
|
||||
```
|
||||
|
||||
### 更新已有环境
|
||||
|
||||
```bash
|
||||
conda env update -f environment.yml --prune
|
||||
```
|
||||
|
||||
### 导出精简依赖
|
||||
|
||||
只导出手动安装的依赖,适合项目记录:
|
||||
|
||||
```bash
|
||||
conda env export --from-history > environment.yml
|
||||
```
|
||||
|
||||
## 清理缓存
|
||||
|
||||
```bash
|
||||
# 清理安装包缓存
|
||||
conda clean -p
|
||||
|
||||
# 清理 tar 包缓存
|
||||
conda clean -t
|
||||
|
||||
# 清理所有可清理缓存
|
||||
conda clean -a
|
||||
```
|
||||
|
||||
## 常见问题
|
||||
|
||||
### conda activate 不生效
|
||||
|
||||
先初始化 Shell,再重启终端:
|
||||
|
||||
```bash
|
||||
conda init
|
||||
```
|
||||
|
||||
### 进入终端自动激活 base
|
||||
|
||||
如果不想每次打开终端都进入 `base` 环境:
|
||||
|
||||
```bash
|
||||
conda config --set auto_activate_base false
|
||||
```
|
||||
|
||||
恢复自动激活:
|
||||
|
||||
```bash
|
||||
conda config --set auto_activate_base true
|
||||
```
|
||||
|
||||
### 查看 Python 与 pip 来自哪个环境
|
||||
|
||||
```bash
|
||||
where python
|
||||
where pip
|
||||
```
|
||||
|
||||
Linux / macOS:
|
||||
|
||||
```bash
|
||||
which python
|
||||
which pip
|
||||
```
|
||||
|
||||
### 依赖冲突
|
||||
|
||||
优先新建干净环境,不要在旧环境里反复修:
|
||||
|
||||
```bash
|
||||
conda create -n newenv python=3.10
|
||||
conda activate newenv
|
||||
```
|
||||
Reference in new issue
Block a user