一文全面掌握conda
- 本篇涉及知识点
- conda是什么,3个一
- 官网:最棒资源
- 两个版本:建议安装miniconda版
- 安装miniconda
- 基本命令
- 环境:创建/删除
- 查看既存环境:conda env list创建虚拟环境:conda create -n [环境名称] [安装库包列表]查看环境列表:conda env list引申知识启动新创建环境:oldgeek-study新创建环境中安装所需包:oldgeek-study退出新环境:输入exit删除环境:conda remove -n [环境名称] --all创建个低版本的环境:Python3.8克隆一个base环境,以备不实之需环境包导入和导出
- 镜像
- 终端执行下面命令添加国内的镜像:咱们清华大学开源的镜像站
- 骚操作
- 删除conda
- 速查宝典
- 查看帮助和版本环境相关日常命令通道相关
- 华山论剑:conda & pip
- 参考资料
闭嘴,安静的看文章
小码匠:今天学什么?
老码农:你每天放学回来开口第一句,过于雷同了,有没有点创意啊?你是小孩,
不要像我们大人被岁月慢慢磨掉了棱角,变得循规蹈矩,毫无创意。
小码匠:就一句话也被你奚落,那我闪了,不学了。
老码农:甩锅,你敢。
小码匠:你又不舍得揍我,吓唬我不管用的。
老码农:好啦,今天我写了篇文章,你自己静下心来,好好读读,有不懂的我们在探讨。
学习这个东西,还是要靠自己的,手把手教你,不是好事
。
再说,你自学能力这么强,也不需要我手把手教啊。
小码匠:老码农,你就会忽悠我,斗不过你,那我看文章了啊。
老码农:嗯,那你
慢慢读,精读
的哟。
conda是什么,3个一
- 一个能支持Python、R、Java、JavaScript、C等语言包、依赖和环境管理工具
- 一个能在Windows、MacOS、Linux上运行开源的软件包管理系统和环境管理系统
- 一个能在本地轻松创建、保存、切换环境
官网:最棒资源
多看官方文档,网络上知识碎片化太严重,官方文档最新、最严谨、最全,是我们学习知识的最佳场所
- 官网:https://www.anaconda.com/
两个版本:建议安装miniconda版
- anaconda:包含常用的包,个人版就有440MB,里面包罗万象,但很多东西你未必用到,所以才有mini版
- Mac版本:440MB(2021年11月3日的Mac版本)https://www.anaconda.com/products/individual
- miniconda:miniconda则是精简版,需要啥装啥,所以推荐使用miniconda
- Mac版本:52.3MB(2021年11月3日的Mac版本),大小差了9倍,虽然硬盘不值钱,但咱也没必要浪费啊。https://docs.conda.io/en/latest/miniconda.html
- 根据自己电脑的操作系统选择,下载相应的安装包。
安装miniconda
“
小码匠,教你一个秘籍,大部分安装程序你一路回车就能搞定
”
小码匠批注
“
老码农,这哪是秘籍,废话当宝贝? 你自己收着吧
”
- Step1: 按【继续】
- Step2: 继续按【继续】
- Step3: 继续按【继续】
- Step4: 继续按【同意】
- Step5: 继续按【安装】,几秒后就安装完毕
- 基本命令
- 确认版本:conda -v
(base) coder@192 pycharm % conda -V conda 4.10.3 (base) coder@ 123
- 查看命令参数: conda, 可以看到子命令有clean、help、list、install、search等子命令
(base) coder@192 ~ % conda usage: conda [-h] [-V] command ... conda is a tool for managing and deploying applications, environments and packages. Options: positional arguments: command clean Remove unused packages and caches. compare Compare packages between conda environments. config Modify configuration values in .condarc. This is modeled after the git config command. Writes to the user .condarc file (/Users/cynthia/.condarc) by default. create Create a new conda environment from a list of specified packages. help Displays a list of available conda commands and their help strings. info Display information about current conda install. init Initialize conda for shell interaction. [Experimental] install Installs a list of packages into a specified conda environment. list List linked packages in a conda environment. package Low-level conda package utility. (EXPERIMENTAL) remove Remove a list of packages from a specified conda environment. uninstall Alias for conda remove. run Run an executable in a conda environment. [Experimental] search Search for packages and display associated information. The input is a MatchSpec, a query language for conda packages. See examples below. update Updates conda packages to the latest compatible version. upgrade Alias for conda update. optional arguments: -h, --help Show this help message and exit. -V, --version Show the conda version number and exit. conda commands available from other packages: env 12345678910111213141516171819202122232425262728293031323334
- 查看子命令帮助信息: conda [子命令] -h
环境:创建/删除
conda最有特色的点,可以便利创建不同的开发环境,对开发环境进行管理
查看既存环境:conda env list
(base) coder@192 ~ % conda env list # conda environments: # /Applications/JupyterLab.app/Contents/Resources/jlab_server base * /Users/coder/opt/miniconda3 12345
创建虚拟环境:conda create -n [环境名称] [安装库包列表]
- 下面示例:环境名称:oldgeek-study,环境默认安装python3.10版
(base) coder@192 ~ % conda create -n oldgeek-study python=3.10 Collecting package metadata (current_repodata.json): done Solving environment: done ## Package Plan ## environment location: /Users/coder/opt/miniconda3/envs/oldgeek-study added / updated specs: - python=3.10 The following packages will be downloaded: package | build ---------------------------|----------------- certifi-2020.6.20 | pyhd3eb1b0_3 155 KB ncurses-6.3 | hca72f7f_0 856 KB pip-21.2.4 | py310hecd8cb5_0 1.8 MB python-3.10.0 | h88f2d9e_1 10.1 MB setuptools-58.0.4 | py310hecd8cb5_0 782 KB tzdata-2021e | hda174b7_0 112 KB ------------------------------------------------------------ Total: 13.8 MB The following NEW packages will be INSTALLED: bzip2 pkgs/main/osx-64::bzip2-1.0.8-h1de35cc_0 ca-certificates pkgs/main/osx-64::ca-certificates-2021.10.26-hecd8cb5_2 certifi pkgs/main/noarch::certifi-2020.6.20-pyhd3eb1b0_3 libcxx pkgs/main/osx-64::libcxx-12.0.0-h2f01273_0 libffi pkgs/main/osx-64::libffi-3.3-hb1e8313_2 ncurses pkgs/main/osx-64::ncurses-6.3-hca72f7f_0 openssl pkgs/main/osx-64::openssl-1.1.1l-h9ed2024_0 pip pkgs/main/osx-64::pip-21.2.4-py310hecd8cb5_0 python pkgs/main/osx-64::python-3.10.0-h88f2d9e_1 readline pkgs/main/osx-64::readline-8.1-h9ed2024_0 setuptools pkgs/main/osx-64::setuptools-58.0.4-py310hecd8cb5_0 sqlite pkgs/main/osx-64::sqlite-3.36.0-hce871da_0 tk pkgs/main/osx-64::tk-8.6.11-h7bc2e8c_0 tzdata pkgs/main/noarch::tzdata-2021e-hda174b7_0 wheel pkgs/main/noarch::wheel-0.37.0-pyhd3eb1b0_1 xz pkgs/main/osx-64::xz-5.2.5-h1de35cc_0 zlib pkgs/main/osx-64::zlib-1.2.11-h1de35cc_3 Proceed ([y]/n)? 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
- 输入:y,创建虚拟环境
查看环境列表:conda env list
(base) coder@192 ~ % conda env list # conda environments: # /Applications/JupyterLab.app/Contents/Resources/jlab_server base * /Users/coder/opt/miniconda3 oldgeek-study /Users/coder/opt/miniconda3/envs/oldgeek-study 123456
引申知识
- Step1: 创建虚拟环境时安装包:requests、numpy 环境名称:coder-study
conda create -n coder-study requests numpy 1
- Step2: 在Step1创建的环境上还想继续安装包:scripy
conda install -n coder-study scipy 1
启动新创建环境:oldgeek-study
(base) coder@192 ~ % conda activate oldgeek-study (oldgeek-study) coder@192 ~ % 12
新创建环境中安装所需包:oldgeek-study
- 安装网络HTTP包:rquests,悲剧安装失败
(oldgeek-study) coder@192 ~ % conda install requests Collecting package metadata (current_repodata.json): done Solving environment: failed with initial frozen solve. Retrying with flexible solve. Solving environment: failed with repodata from current_repodata.json, will retry with next repodata source. Collecting package metadata (repodata.json): done Solving environment: failed with initial frozen solve. Retrying with flexible solve. Solving environment: | Found conflicts! Looking for incompatible packages. This can take several minutes. Press CTRL-C to abort. failed UnsatisfiableError: The following specifications were found to be incompatible with each other: Output in format: Requested package -> Available versions 1234567891011121314
- 在conda创建的环境中也可以使用pip安装,执行命令:pip install requests
- pip 是一个Python包管理工具,主要是用于安装 PyPI 上的软件包。
(oldgeek-study) coder@192 ~ % pip install requests Collecting requests Using cached requests-2.26.0-py2.py3-none-any.whl (62 kB) Collecting certifi>=2017.4.17 Downloading certifi-2021.10.8-py2.py3-none-any.whl (149 kB) |████████████████████████████████| 149 kB 164 kB/s Collecting urllib3<1.27,>=1.21.1 Downloading urllib3-1.26.7-py2.py3-none-any.whl (138 kB) |████████████████████████████████| 138 kB 67 kB/s Collecting idna<4,>=2.5 Downloading idna-3.3-py3-none-any.whl (61 kB) |████████████████████████████████| 61 kB 16 kB/s Collecting charset-normalizer~=2.0.0 Downloading charset_normalizer-2.0.7-py3-none-any.whl (38 kB) Installing collected packages: urllib3, idna, charset-normalizer, certifi, requests Successfully installed certifi-2021.10.8 charset-normalizer-2.0.7 idna-3.3 requests-2.26.0 urllib3-1.26.7 12345678910111213141516
使用pip安装成功,那我们用conda list查看安装情况
(oldgeek-study) coder@192 ~ % conda list # packages in environment at /Users/coder/opt/miniconda3/envs/oldgeek-study: # # Name Version Build Channel bzip2 1.0.8 h1de35cc_0 ca-certificates 2021.10.26 hecd8cb5_2 certifi 2020.6.20 pyhd3eb1b0_3 libcxx 12.0.0 h2f01273_0 libffi 3.3 hb1e8313_2 ncurses 6.3 hca72f7f_0 openssl 1.1.1l h9ed2024_0 pip 21.2.4 py310hecd8cb5_0 python 3.10.0 h88f2d9e_1 readline 8.1 h9ed2024_0 setuptools 58.0.4 py310hecd8cb5_0 sqlite 3.36.0 hce871da_0 tk 8.6.11 h7bc2e8c_0 tzdata 2021e hda174b7_0 wheel 0.37.0 pyhd3eb1b0_1 xz 5.2.5 h1de35cc_0 zlib 1.2.11 h1de35cc_3 (oldgeek-study) cynthia@192 ~ % pip list Package Version ------------------ --------- certifi 2021.10.8 charset-normalizer 2.0.7 idna 3.3 pip 21.2.4 requests 2.26.0 setuptools 58.0.4 urllib3 1.26.7 (oldgeek-study) cynthia@192 ~ % 1234567891011121314151617181920212223242526272829303132
- 悲剧,没有查到,用conda安装的包,conda都能进行管理,因为我们使用pip安装的,所以查看不到,没关系, 我们用pip list可以继续查看,能看到吧。自己管理自己的东西,别人管理的与我何关
- 安装包: 科学计算包:NumPy
(oldgeek-study) coder@192 ~ % conda install numpy Collecting package metadata (current_repodata.json): done Solving environment: failed with initial frozen solve. Retrying with flexible solve. Solving environment: failed with repodata from current_repodata.json, will retry with next repodata source. Collecting package metadata (repodata.json): done Solving environment: failed with initial frozen solve. Retrying with flexible solve. Solving environment: | Found conflicts! Looking for incompatible packages. This can take several minutes. Press CTRL-C to abort. failed UnsatisfiableError: The following specifications were found to be incompatible with the existing python installation in your environment: Specifications: - numpy -> python[version='>=2.7,<2.8.0a0|>=3.6,<3.7.0a0|>=3.7,<3.8.0a0|>=3.8,<3.9.0a0|>=3.9,<3.10.0a0|>=3.5,<3.6.0a0'] Your python: python=3.10 If python is on the left-most side of the chain, that's the version you've asked for. When python appears to the right, that indicates that the thing on the left is somehow not available for the python version you are constrained to. Note that conda will not change your python version to a different minor version unless you explicitly specify that. 12345678910111213141516171819202122232425
悲剧:没有安装成功,我们太新潮了,python3.10刚出来,numpy尚未来的及对python3.10的支持,所以直接安装挂了。
建议:
- 学习了解新功能,可以安装新版本
- 生产环境:切换新版本一定要注意,要经过仔细测试,无问题后才可以在生产切换,切勿未经测试,直接切换,酿成重大生产事故。
退出新环境:输入exit
(oldgeek-study) coder@192 ~ % exit Saving session... ...copying shared history... ...saving history...truncating history files... ...completed. [进程已完成] 1234567
删除环境:conda remove -n [环境名称] --all
(oldgeek-study) coder@192 ~ % conda remove -n oldgeek-study --all CondaEnvironmentError: cannot remove current environment. deactivate and run conda remove again 123
创建个低版本的环境:Python3.8
- 科学计算包:numpy
- 数学包:scipy
- 符号包:sympy
- 可视化包:matplotlib seaborn
conda create -n oldgeek-study python=3.8 numpy scipy sympy matplotlib seaborn 1
克隆一个base环境,以备不实之需
“
后面oldgeek-study环境在安装新包时出了问题,可以直接用这个base环境,继续搞,省去了初始化安装必要包的时间
”
(base) coder@192 ~ % conda create -n coder-base --clone oldgeek-study WARNING: A directory already exists at the target location '/Users/coder/opt/miniconda3/envs/coder-base' but it is not a conda environment. Continue creating environment (y/[n])? y Source: /Users/coder/opt/miniconda3/envs/oldgeek-study Destination: /Users/coder/opt/miniconda3/envs/coder-base Packages: 131 Files: 0 Preparing transaction: done Verifying transaction: done Executing transaction: done # # To activate this environment, use # # $ conda activate coder-base # # To deactivate an active environment, use # # $ conda deactivate 1234567891011121314151617181920
环境包导入和导出
场景
老码农在自己的电脑环境:coder-base安装了许多软件包,一直运行很稳定
小码匠自己瞎捣鼓,把自己环境弄挂了,着急想尽快重新构建个环境,那咋办呢。
- 导出base环境yml文件
(base) coder@192 ~ % conda env export --file coder-base.yml --name coder-base 1
- 查看yml文件内容,哦,文件内容原来是安装包的列表
(base) coder@192 ~ % cat coder-base.yml name: coder-base channels: - conda-forge - defaults dependencies: - anyio=3.3.4=py38h50d1736_0 - appnope=0.1.2=py38h50d1736_2 - argon2-cffi=21.1.0=py38h96a0964_1 - async_generator=1.10=py_0 - attrs=21.2.0=pyhd8ed1ab_0 - babel=2.9.1=pyh44b312d_0 - backcall=0.2.0=pyh9f0ad1d_0 - backports=1.0=py_2 - backports.functools_lru_cache=1.6.4=pyhd8ed1ab_0 123456789101112131415
- 利用刚导出的文件列表,直接创建
conda env create -f coder-base.yml 1
镜像
场景:我们有时安装软件包时,经常会很慢,有时还直接挂了,那有没有提速的办法呢。
是什么原因呢?
安装完conda,默认我们会去官方镜像站点拉取包,官方的服务器都在国外,网络一旦不稳定,自然我们拉取软件包会很慢。
conda config --add channels bioconda conda config --add channels conda-forge 12
终端执行下面命令
- 初次执行,会在当前用户的目录下面生成一个.condarc,文件位置在当前用户的目录下
conda config 1
- Mac端,执行ls ~/.co*,是不是看到了有个.condarc文件,这个时conda的配置文件
(base) coder@192 ~ % ls ~/.co* /Users/coder/.codota-id /Users/coder/.condarc 12
- 查看文件内容
(base) coder@192 ~ % cat ~/.condarc show_channel_urls: true 12
- 查看channel,空空哒,我们没有添加任何配置,自然时空空哒
(base) cynthia@192 ~ % conda config --get channels 1
- 忘了很重要的一点:conda info,执行该命令,能查看conda现在的配置信息,信息很丰富
(base) coder@192 ~ % conda info active environment : base active env location : /Users/coder/opt/miniconda3 shell level : 1 user config file : /Users/coder/.condarc populated config files : /Users/coder/.condarc conda version : 4.10.3 conda-build version : not installed python version : 3.9.5.final.0 virtual packages : __osx=10.16=0 __unix=0=0 __archspec=1=x86_64 base environment : /Users/coder/opt/miniconda3 (writable) conda av data dir : /Users/coder/opt/miniconda3/etc/conda conda av metadata url : None channel URLs : https://repo.anaconda.com/pkgs/main/osx-64 https://repo.anaconda.com/pkgs/main/noarch https://repo.anaconda.com/pkgs/r/osx-64 https://repo.anaconda.com/pkgs/r/noarch package cache : /Users/coder/opt/miniconda3/pkgs /Users/coder/.conda/pkgs envs directories : /Users/coder/opt/miniconda3/envs /Users/coder/.conda/envs platform : osx-64 user-agent : conda/4.10.3 requests/2.25.1 CPython/3.9.5 Darwin/20.6.0 OSX/10.16 UID:GID : 501:20 netrc file : None offline mode : False 1234567891011121314151617181920212223242526272829
添加国内的镜像:咱们清华大学开源的镜像站
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/ conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/ conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/ conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/ 1234
- 显示安装的频道
(base) coder@192 ~ % conda config --get channels --add channels 'defaults'# lowest priority --add channels 'https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/' --add channels 'https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/' --add channels 'https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/' --add channels 'https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/'# highest priority 123456
骚操作
- 我想切换到小码匠的学习环境,通常我们这样做
conda activate coder-study 1
- 换个姿势,我能不能直接输入coder,就可以直接进入呢?当然可以了
(base) coder@192 ~ % vim ~/.bash_profile # >>> conda initialize >>> # !! Contents within this block are managed by 'conda init' !! __conda_setup="$('/Users/coder/opt/miniconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)" if [ $? -eq 0 ]; then eval "$__conda_setup" else if [ -f "/Users/coder/opt/miniconda3/etc/profile.d/conda.sh" ]; then . "/Users/coder/opt/miniconda3/etc/profile.d/conda.sh" else export PATH="/Users/coder/opt/miniconda3/bin:$PATH" fi fi unset __conda_setup # <<< conda initialize <<< export PATH=/System/Volumes/Data/fgb/04.tool/apache-maven-3.8.1/bin:$PATH export PATH="$PATH":/usr/local/mysql/bin export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.aliyun.com/homebrew/homebrew-bottles export PATH=${PATH}:/Library/Frameworks/Python.framework/Versions/3.10/bin/python3 alias python="/Library/Frameworks/Python.framework/Versions/3.10/bin/python3" alias coder="conda activate coder-study" (base) coder@192 ~ % source ~/.bash_profile (base) coder@192 ~ % coder (coder-study) cynthia@192 ~ % 1234567891011121314151617181920212223242526
删除conda
Mac
- 查看安装路径:conda env list,我们可以看到默认是安装在/Users/cynthia/opt/miniconda3
(base) coder@192 opt % conda env list # conda environments: # /Applications/JupyterLab.app/Contents/Resources/jlab_server base * /Users/coder/opt/miniconda3 12345
- 移除conda,使用rm命令
rm -rf /Users/coder/opt/miniconda3 1
Windows:
- 去控制面板,点击“添加或删除程序”,选择miniconda并点击删除程序。就不截图了。
速查宝典
- 官方
查看帮助和版本
conda | 查看命令列表 |
conda [子命令] -h 栗子:conda list -h | 查看子命令帮助信息 |
conda -V | 查看版本号 |
conda info | 查看配置信息 |
conda |
环境相关
conda create -n [env_name] | 创建环境 |
conda create -n [env_name] python=3.8 | 创建环境并安装Python指定版本 |
conda create -n [env_name] python=3.8 dumpy | 创建环境并安装Python指定版本并安装其他包 |
conda remove -n [env_name] --all | 移除环境 |
conda activate [env_name] | 进入环境 |
环境内直接输入:exit | 退出环境 |
conda create --name new_env_name --clone base_env_name | 克隆环境 |
conda env export --file [文件名字.yml] --name [env_name] 栗子:conda env export --file coder-base.yml --name coder-base | 导出环境的包列表 |
conda env create -f [文件名字.yml] conda env create -f coder-base.yml | 基于导出的yml文件创建新环境 |
日常命令
conda install [package_name] | 安装包 |
caonda list | 查看已安装列表 |
conda search [package_name] | 搜索待安装包信息 |
conda update [package_name] | 更新包 |
conda remove [package_name] | 删除包 |
conda clean -t | 直接清除被缓存包 |
conda clean -y -a | 直接清除索引缓存、未使用缓存包 |
通道相关
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/ conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/ conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/ conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/ | 更换源:栗子是清华大学的通道 |
conda config --show channels | 查看已安装通道 |
conda config --remove-key channels | 恢复默认通道 |
conda update [package_name] | 更改通道 |
conda config --remove channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/ | 删除某个通道 |
华山论剑:conda & pip
Command reference
- https://docs.conda.io/projects/conda/en/latest/commands.html
安装包 |
|
| X |
更新包 |
|
| X |
更新管理器 |
| Linux/macOS:
Win:
| X |
卸载包 |
|
| X |
创建环境 |
| X |
|
进入环境 |
* | X |
|
退出环境 |
| X |
|
搜寻可用包 |
|
| X |
从指定通道安装包 |
|
| X |
显示已安装包 |
|
| X |
获取已安装包 |
|
| X |
查看环境列表 |
| X | Install virtualenv wrapper, then
|
安装包 |
|
| X |
安装Python指定版本 |
| X | X |
卸载Python |
* | X |
参考资料
- 官网
- https://docs.conda.io/en/latest/
- 用户指南
- https://docs.conda.io/projects/conda/en/latest/user-guide/index.html
- 安装
- -https://docs.conda.io/projects/conda/en/latest/user-guide/install/index.html
- 命令速查
- https://docs.conda.io/projects/conda/en/latest/commands.html
- 精简版下载和安装
- https://docs.conda.io/en/latest/miniconda.html
对话
小码匠:累死宝宝了,老码农,你为啥整理这么多啊,看着有点累啊。
老码农:这份文档绝大部分问题你都可以解决,快速查找。
小码匠:那也不用一次弄这么细吧。
老码农:小码匠,很严肃的话题,
学习要注重效率
,梳理资料,搞N次,每次都丢三落四的,会严重影响未来的效率。一次搞全,后面再补充新知识补充就好了。要养成好的学习习惯,才能事倍功半的。
梳理资料
- 要有目录结构,便于查看的同学快速定位到自己想看的知识
- 尽量系统,梳理一遍也是梳理,二遍也是梳理,为啥不一次搞好呢
- 资料要确认,不要随意拷贝,错误的知识耽误自己,更耽误别人
小码匠:你的要求好高啊,我尽量争取吧,不成为你的拖油瓶。
老码农:你不会的,因为你要成为码匠的。