下载安装

windows

Latest Miniconda installer links | Miniconda

miniconda清华源下载地址

安装完成后执行conda --version, 如果没有输出, 添加环境变量

1
2
3
D:\...\Miniconda3
D:\...\Miniconda3\Library\bin
D:\...\Miniconda3\Scripts

linux

Quick command line install | Miniconda

1
2
3
4
5
mkdir -p ~/miniconda3
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ~/miniconda3/miniconda.sh
# wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ~/miniconda3/miniconda.sh
bash ~/miniconda3/miniconda.sh -b -u -p ~/miniconda3
rm -rf ~/miniconda3/miniconda.sh
1
echo $0 # check bash or zsh
1
2
~/miniconda3/bin/conda init bash # bash
~/miniconda3/bin/conda init zsh # zsh

重启终端出现 (base), 即完成安装

更新

1
conda update conda

镜像源

conda

清华大学开源软件镜像站 Anaconda 镜像使用帮助

1
2
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --set show_channel_urls yes

查看当前镜像源

1
conda config --show-sources

临时使用镜像源

1
conda install --channel https://pypi.tuna.tsinghua.edu.cn/simple [package-name]

pip

清华大学开源软件镜像站 PyPI 镜像使用帮助

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
    2
    conda create --name <new_name> --clone <old_name>
    conda remove --name <old_name> --all

包管理命令

  • 查看当前环境已安装的包

    1
    2
    conda list
    pip list
  • 查看包的版本号

    1
    2
    conda list <包名>
    pip show <包名>
  • 安装包

    1
    2
    conda install <包名>
    pip install <包名>
  • 安装指定版本的包

    1
    2
    3
    conda install [包名]=[版本号] # 指定版本
    pip install [包名]==[版本号] # 指定版本
    pip install [包名]>=[版本号] # 指定最小版本

    注意在指定版本安装时, conda使用1个等号=, pip使用2个等号==来指定版本

  • 同时安装多个包

    1
    2
    conda install <包名1> <包名2> <包名3>
    pip install <包名1> <包名2> <包名3>
  • 卸载包

    1
    2
    conda remove <包名>
    pip uninstall <包名>
  • 更新包

    1
    2
    conda update <包名>
    pip install --upgrade <包名>
  • 更新环境中的所有包

    1
    conda update --all
  • 搜索模块

    1
    conda search <包名>

其它命令

  • 清除缓存

    conda clean

    1
    2
    conda clean --all
    # Remove index cache, lock files, unused cache packages, tarballs, and logfiles.

导入导出

Pip 导入导出

pip install --requirement

pip list --format

1
pip install --requirement requirements.txt
1
pip list --format=freeze > requirements.txt

Conda 导入导出

conda install --file

conda list --export

1
conda install --file requirements.txt
1
conda list --export > requirements.txt

Conda和Pip

import xxx 的优先级

1
2
3
4
5
6
7
8
9
10
11
(newenv) C:\Users\xiaophai>python -m site
sys.path = [
'C:\\Users\\xiaophai',
'C:\\Users\\xiaophai\\.conda\\envs\\newenv\\python36.zip',
'C:\\Users\\xiaophai\\.conda\\envs\\newenv\\DLLs',
'C:\\Users\\xiaophai\\.conda\\envs\\newenv\\lib',
'C:\\Users\\xiaophai\\.conda\\envs\\newenv',
'C:\\Users\\xiaophai\\.conda\\envs\\newenv\\lib\\site-packages',
]
USER_BASE: 'C:\\Users\\xiaophai\\AppData\\Roaming\\Python' (exists)
USER_SITE: 'C:\\Users\\xiaophai\\AppData\\Roaming\\Python\\Python36\\site-packages' (doesn't exist)

使用 conda list 命令可以列出当前环境下的所有已安装的包. 使用 conda 安装的包在 Channel 一栏显示 defaults; 使用 pip 安装的包在 Channel 一栏显示 pypi

1
2
3
4
5
6
7
8
9
10
11
(newenv) C:\Users\xiaophai>conda list
## packages in environment at C:\Users\xiaophai\.conda\envs\newenv:
#
## Name Version Build Channel
...
matplotlib 3.3.4 pypi_0 pypi
...
numpy 1.19.2 py36hadc3359_0 defaults
...
python 3.6.13 h3758d61_0 defaults
...