# Conda教程
# 介绍什么是conda以及其优势
Conda是一个开源的包管理器和环境管理器,支持Windows,macOS和Linux系统。Conda可以快速安装、运行和更新包及其依赖项。Conda可以轻松创建、保存、加载和在您的本地计算机之间切换环境。虽然Conda最初是为Python程序创建的,但它可以打包和分发任何语言的软件。 作为包管理器,Conda可以帮助您查找和安装包。如果您需要一个需要不同版本的Python的包,您不需要切换到不同的环境管理器,因为Conda也是一个环境管理器。只需要几个命令,您就可以设置一个完全独立的环境来运行那个不同版本的Python,同时继续在普通环境中运行您通常的Python版本。 在其默认配置中,Conda可以安装和管理 Anaconda®构建,审核和维护的数千个包。 Conda可以与Travis CI和AppVeyor等持续集成系统结合使用,以提供对您的代码的定期自动测试。
# conda的安装
Conda包含在Anaconda和Miniconda的所有版本中。 Conda也包含在Anaconda Enterprise中,它为Python,R,Node.js,Java和其他应用程序栈提供现场企业包和环境管理。Conda也可以在conda-forge社区渠道上获得。您也可以在PyPI上获取Conda,但这种方法可能不太最新。 Conda的安装相当简单,对于个人开发者,推荐使用Anaconda或者Miniconda进行安装,官方文档安装指南如下:
# 安装Anaconda:
- 访问Anaconda网站 (opens new window),选择与您的操作系统对应的安装程序下载
- 双击安装程序并按照提示操作。安装完成后,您的命令行窗口或终端将直接进入Conda environvent。
# 安装Miniconda:
- 访问Miniconda网站 (opens new window),选择与您的操作系统对应的安装程序下载
- 双击安装程序并按照提示操作。安装完成后,需要手动初始化Conda环境。打开终端/命令行窗口,运行:
$ bash
$ conda init
# conda环境的创建、激活和删除
Conda环境可以隔离项目的依赖关系和Python版本。 您可以创建,激活和删除环境,还可以导出和导入环境以进行重复使用。
# 创建环境:
## 创建名为myenv的环境
$ conda create --name myenv
Retrieving notices: ...working... done
Collecting package metadata (current_repodata.json): done
Solving environment: done
## Package Plan ##
environment location: /Users/fbbyqsyea/miniconda3/envs/myenv
Proceed ([y]/n)? y
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use
#
# $ conda activate myenv
#
# To deactivate an active environment, use
#
# $ conda deactivate
## 创建python版本为3.7的myenv环境
$ conda create -n myenv python=3.7
# 激活环境:
## Linux/Mac
$ conda activate myenv
(myenv) $
## Windows
$ conda activate myenv
您的命令行提示符将显示(myenv),表示已激活myenv环境。 在激活的环境中,conda会安装任何包到该环境。 您可以使用conda install package_name安装软件包。
# 退出环境:
## Linux/Mac
(myenv) $ conda deactivate
(base) $
## Windows
$ conda deactivate
您的命令行提示符将恢复正常或者变为(base),表示已退出环境。
# 删除环境:
##删除myenv环境及其中的所有包
$ conda remove --name myenv --all
Conda环境非常有用,它可以确保在不同项目中使用不同版本的Python和包,而不会互相干扰。
# 在conda环境中安装和管理软件包
Conda可以安装、更新、删除和列出软件包。主要命令如下:
# 安装
(myenv) $ webdata conda install flask
Collecting package metadata (current_repodata.json): done
Solving environment: done
## Package Plan ##
environment location: /Users/fbbyqsyea/miniconda3/envs/myenv
added / updated specs:
- flask
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-2023.01.10-hecd8cb5_0
click pkgs/main/osx-64::click-8.0.4-py311hecd8cb5_0
flask pkgs/main/osx-64::flask-2.2.2-py311hecd8cb5_0
itsdangerous pkgs/main/noarch::itsdangerous-2.0.1-pyhd3eb1b0_0
jinja2 pkgs/main/osx-64::jinja2-3.1.2-py311hecd8cb5_0
libffi pkgs/main/osx-64::libffi-3.4.2-hecd8cb5_6
markupsafe pkgs/main/osx-64::markupsafe-2.1.1-py311h6c40b1e_0
ncurses pkgs/main/osx-64::ncurses-6.4-hcec6c5f_0
openssl pkgs/main/osx-64::openssl-1.1.1t-hca72f7f_0
pip pkgs/main/osx-64::pip-23.0.1-py311hecd8cb5_0
python pkgs/main/osx-64::python-3.11.3-h1fd4e5f_0
readline pkgs/main/osx-64::readline-8.2-hca72f7f_0
setuptools pkgs/main/osx-64::setuptools-66.0.0-py311hecd8cb5_0
sqlite pkgs/main/osx-64::sqlite-3.41.2-h6c40b1e_0
tk pkgs/main/osx-64::tk-8.6.12-h5d9f67b_0
tzdata pkgs/main/noarch::tzdata-2023c-h04d1e81_0
werkzeug pkgs/main/osx-64::werkzeug-2.2.3-py311hecd8cb5_0
wheel pkgs/main/osx-64::wheel-0.38.4-py311hecd8cb5_0
xz pkgs/main/osx-64::xz-5.4.2-h6c40b1e_0
zlib pkgs/main/osx-64::zlib-1.2.13-h4dc903c_0
Proceed ([y]/n)? y
Downloading and Extracting Packages
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
# 更新
(myenv) $ webdata conda update flask
Collecting package metadata (current_repodata.json): done
Solving environment: done
# All requested packages already installed.
# 列出软件包
(myenv) $ webdata conda list
# packages in environment at /Users/fbbyqsyea/miniconda3/envs/myenv:
#
# Name Version Build Channel
bzip2 1.0.8 h1de35cc_0
ca-certificates 2023.01.10 hecd8cb5_0
click 8.0.4 py311hecd8cb5_0
flask 2.2.2 py311hecd8cb5_0
itsdangerous 2.0.1 pyhd3eb1b0_0
jinja2 3.1.2 py311hecd8cb5_0
libffi 3.4.2 hecd8cb5_6
markupsafe 2.1.1 py311h6c40b1e_0
ncurses 6.4 hcec6c5f_0
openssl 1.1.1t hca72f7f_0
pip 23.0.1 py311hecd8cb5_0
python 3.11.3 h1fd4e5f_0
readline 8.2 hca72f7f_0
setuptools 66.0.0 py311hecd8cb5_0
sqlite 3.41.2 h6c40b1e_0
tk 8.6.12 h5d9f67b_0
tzdata 2023c h04d1e81_0
werkzeug 2.2.3 py311hecd8cb5_0
wheel 0.38.4 py311hecd8cb5_0
xz 5.4.2 h6c40b1e_0
zlib 1.2.13 h4dc903c_0
# 删除
(myenv) $ webdata conda remove flask
Collecting package metadata (repodata.json): done
Solving environment: done
## Package Plan ##
environment location: /Users/fbbyqsyea/miniconda3/envs/myenv
removed specs:
- flask
The following packages will be REMOVED:
click-8.0.4-py311hecd8cb5_0
flask-2.2.2-py311hecd8cb5_0
itsdangerous-2.0.1-pyhd3eb1b0_0
jinja2-3.1.2-py311hecd8cb5_0
markupsafe-2.1.1-py311h6c40b1e_0
werkzeug-2.2.3-py311hecd8cb5_0
Proceed ([y]/n)? y
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
# 其他命令
其他命令可以见conda --help
(myenv) $ webdata conda --help
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/fbbyqsyea/.condarc) by default. Use the --show-sources flag to display all identified configuration locations on your computer.
create Create a new conda environment from a list of specified packages.
info Display information about current conda install.
init Initialize conda for sh interaction.
install Installs a list of packages into a specified conda environment.
list List installed packages in a conda environment.
package Low-level conda package utility. (EXPERIMENTAL)
remove (uninstall)
Remove a list of packages from a specified conda environment. Use `--all` flag to remove all packages and the environment itself.
rename Renames an existing environment.
run Run an executable in a conda environment.
search Search for packages and display associated information.The input is a MatchSpec, a query language for conda packages. See examples below.
update (upgrade) Updates conda packages to the latest compatible version.
notices Retrieves latest channel notifications.
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 (legacy):
content-trust
env
...