Conda #
- conda 是一款软件管理软件,相当于 windows 里面的应用商店
- miniconda 和 anaconda 中都包含了 conda
- miniconda windows 64 位安装包大小为 51.4 Mb,只包含了 conda、python、和一些必备的软件工具
- anaconda windows 64 位安装包大小为 462 Mb,是 miniconda 的扩展,包含了数据科学和机器学习要用到的很多软件。
使用教程 #
# 清理缓存
conda clean -a
# The base environment is activated by default
conda config --set auto_activate_base True
# The base environment is not activated by default
conda config --set auto_activate_base False
# 查看已创建的conda环境
conda info --envs
# 激活环境
conda activate base
# 退出环境
conda deactivate
镜像源 #
阿里云 #
cat << EOF > ~/.condarc
channels:
- defaults
show_channel_urls: true
default_channels:
- http://mirrors.aliyun.com/anaconda/pkgs/main
- http://mirrors.aliyun.com/anaconda/pkgs/r
- http://mirrors.aliyun.com/anaconda/pkgs/msys2
custom_channels:
conda-forge: http://mirrors.aliyun.com/anaconda/cloud
msys2: http://mirrors.aliyun.com/anaconda/cloud
bioconda: http://mirrors.aliyun.com/anaconda/cloud
menpo: http://mirrors.aliyun.com/anaconda/cloud
pytorch: http://mirrors.aliyun.com/anaconda/cloud
simpleitk: http://mirrors.aliyun.com/anaconda/cloud
EOF
conda clean -i
清华 TUNA #
cat << EOF > ~/.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
EOF
conda clean -i
北京外国语 #
cat << EOF > ~/.condarc
channels:
- defaults
show_channel_urls: true
default_channels:
- https://mirrors.bfsu.edu.cn/anaconda/pkgs/main
- https://mirrors.bfsu.edu.cn/anaconda/pkgs/r
- https://mirrors.bfsu.edu.cn/anaconda/pkgs/msys2
custom_channels:
conda-forge: https://mirrors.bfsu.edu.cn/anaconda/cloud
msys2: https://mirrors.bfsu.edu.cn/anaconda/cloud
bioconda: https://mirrors.bfsu.edu.cn/anaconda/cloud
menpo: https://mirrors.bfsu.edu.cn/anaconda/cloud
pytorch: https://mirrors.bfsu.edu.cn/anaconda/cloud
pytorch-lts: https://mirrors.bfsu.edu.cn/anaconda/cloud
simpleitk: https://mirrors.bfsu.edu.cn/anaconda/cloud
EOF
conda clean -i
conda vs pip #
- pip 是用来安装 python 包的,安装的是 python wheel 或者源代码的包。从源码安装的时候需要有编译器的支持,pip 也不会去支持 python 语言之外的依赖项。
- conda 是用来安装 conda package,虽然大部分 conda 包是 python 的,但它支持了不少非 python 语言写的依赖项,比如 mkl cuda 这种 c c++写的包。conda 安装的都是编译好的二进制包,不需要你自己编译。这导致了 conda 装东西的体积一般比较大,尤其是 mkl 这种,动不动几百兆甚至一 G 多。
- 所以,pip 有时候系统环境没有某个编译器可能会失败,conda 不会。
- conda 比 pip 更加严格,conda 会检查当前环境下所有包之间的依赖关系,pip 可能对之前安装的包就不管了。这样做的话,conda 基本上安上了就能保证工作,pip 有时候可能装上了也不 work。
- conda 算依赖项的时间比 pip 多很多,而且重新安装的包也会更多(会选择更新旧包的版本)
conda install
vs pip install
#
-
conda install xxx:这种方式安装的库都会放在
anaconda3/pkgs
目录下,- 这样的好处就是,当在某个环境下已经下载好了某个库,再在另一个环境中还需要这个库时,就可以直接从 pkgs 目录下将该库复制至新环境而不用重复下载。
-
pip install xxx:分两种情况,
- 一种情况就是当前 conda 环境的 python 是 conda 安装的,和系统的不一样,那么 xxx 会被安装到
anaconda3/envs/current_env/lib/python3.x/site-packages
文件夹中, - 如果当前 conda 环境用的是系统的 python,那么 xxx 会通常会被安装到
~/.local/lib/python3.x/site-packages
文件夹中
- 一种情况就是当前 conda 环境的 python 是 conda 安装的,和系统的不一样,那么 xxx 会被安装到
-
conda 和 pip 安装同一个 xxx 库情况下,conda 环境下 python 代码中 import xxx 时,谁安装的 xxx 优先级较高会被 import,这个问题通过下面这条命令可以解决:
python -m site
# 输出
sys.path = [
'/root/anaconda3',
'/root/anaconda3/lib/python39.zip',
'/root/anaconda3/lib/python3.9',
'/root/anaconda3/lib/python3.9/lib-dynload',
'/root/anaconda3/lib/python3.9/site-packages',
]
USER_BASE: '/root/.local' (exists)
USER_SITE: '/root/.local/lib/python3.9/site-packages' (doesn't exist)
ENABLE_USER_SITE: True
# USER_BASE 和 USER_SITE 是用户自定义的启用Python脚本和依赖安装包的基础路径
叶王 © 2013-2024 版权所有。如果本文档对你有所帮助,可以请作者喝饮料。