Conda&Pip
下载安装
windows
Latest Miniconda installer links | Miniconda
安装完成后执行conda --version
, 如果没有输出, 添加环境变量
1 | D:\...\Miniconda3 |
linux
Quick command line install | Miniconda
1 | mkdir -p ~/miniconda3 |
1 | echo $0 # check bash or zsh |
1 | ~/miniconda3/bin/conda init bash # bash |
重启终端出现 (base), 即完成安装
更新
1 | conda update conda |
镜像源
conda
1 | conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/ |
查看当前镜像源
1 | conda config --show-sources |
临时使用镜像源
1 | conda install --channel https://pypi.tuna.tsinghua.edu.cn/simple [package-name] |
pip
1 | pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple |
查看当前镜像源
1 | pip config list |
临时使用镜像源
1 | pip install --index-url https://pypi.tuna.tsinghua.edu.cn/simple [package-name] |
Conda/Pip 的基本使用
Commands | pip documentation
Working with conda (CLI) | conda docs
环境管理命令
-
查看已创建的环境以及路径
1
conda info --envs
-
创建新环境
1
conda create --name <环境名>
Note: conda 默认会将新环境存储在
C:/Users/.../.conda/envs
(windows) 目录下 -
创建环境指定python版本号
1
conda create --name <环境名> python=<版本号>
Note: "="等号两边不能有空格, 例如
python = 3.9
会报错, 需要改成python=3.9
-
从
requirements.txt
创建环境1
conda create --name <env> --file requirements.txt
-
激活环境
1
conda activate <环境名>
在 conda 的旧版本中, 使用
source activate
命令来激活环境 -
退出当前环境
1
conda deactivate
-
删除环境(不能删除当前环境)
1
conda remove --name <环境名> --all
-
进入 Python IDE
使用
Python
进入 Python IDE, 使用exit()
退出 Python IDE -
修改环境名 (clone + remove)
1
2conda create --name <new_name> --clone <old_name>
conda remove --name <old_name> --all
包管理命令
-
查看当前环境已安装的包
1
2conda list
pip list -
查看包的版本号
1
2conda list <包名>
pip show <包名> -
安装包
1
2conda install <包名>
pip install <包名> -
安装指定版本的包
1
2
3conda install [包名]=[版本号] # 指定版本
pip install [包名]==[版本号] # 指定版本
pip install [包名]>=[版本号] # 指定最小版本注意在指定版本安装时, conda使用1个等号
=
, pip使用2个等号==
来指定版本 -
同时安装多个包
1
2conda install <包名1> <包名2> <包名3>
pip install <包名1> <包名2> <包名3> -
卸载包
1
2conda remove <包名>
pip uninstall <包名> -
更新包
1
2conda update <包名>
pip install --upgrade <包名> -
更新环境中的所有包
1
conda update --all
-
搜索模块
1
conda search <包名>
其它命令
-
清除缓存
1
2conda clean --all
Remove index cache, lock files, unused cache packages, tarballs, and logfiles.
导入导出
Pip 导入导出
1 | pip install --requirement requirements.txt |
1 | pip list --format=freeze > requirements.txt |
Conda 导入导出
1 | conda install --file requirements.txt |
1 | conda list --export > requirements.txt |
Conda和Pip
import xxx
的优先级
1 | (newenv) C:\Users\xiaophai>python -m site |
使用 conda list
命令可以列出当前环境下的所有已安装的包. 使用 conda 安装的包在 Channel 一栏显示 defaults; 使用 pip 安装的包在 Channel 一栏显示 pypi
1 | (newenv) C:\Users\xiaophai>conda list |