安装 conda 包#

conda 包是一个压缩的 tarball (.tar.bz2) 或 .conda 文件,其中包含在 conda 环境中安装特定软件所需的一切内容。 这包括

  • 软件/库本身以及任何必需的依赖项。

  • 关于包的元数据(版本、作者等等)。

  • 安装脚本和配置信息。

  • 适用于不同操作系统和架构的预编译二进制文件。

您可以使用 conda 包管理器从 Anaconda 的公共存储库 访问和安装超过 8,000 个开源数据科学和机器学习 conda 包,以及在 Anaconda.orgconda-forge 上发布的数千个更多社区开发的包。

您甚至可以使用 conda-build 工具构建自己的 自定义 conda 包,然后将它们上传到 Anaconda.orgconda-forge 上的公共频道,与他人分享。

注意

可以将 conda 与 pip 结合使用来构建 conda 环境。但是,在 conda 环境中使用 pip 包存在一些限制。有关更多信息,请参阅 安装 pip 包

此页面包含与 conda 包交互的常用 conda 命令。有关管理包的更多详细信息,请参阅 官方 conda 文档

打开 Anaconda Prompt(macOS/Linux 上的终端)并按照这些说明操作。如果您喜欢使用图形用户界面,也可以使用 Anaconda Navigator 只需点击几下即可安装 conda 包。

搜索 conda 包#

使用 conda 搜索包

# Replace <PACKAGE> with the name of the package you want to search for
conda search <PACKAGE>

默认情况下,conda 在 Anaconda 的默认通道中搜索包。

在特定通道中搜索包

# Replace <PACKAGE> with the name of the package you want to search for
# Replace <CHANNEL> with the URL or name of the channel you want to search, e.g., conda-forge
conda search <CHANNEL>::<PACKAGE>

如果您未在命令中指定完整 URL,则 conda 会调用通道别名来完成 URL。默认情况下,这是 https://conda.anaconda.org/<CHANNEL>。您可以使用 channel_alias 将通道别名更改为其他存储库。 有关 通道别名配置详细信息,请参阅官方 conda 文档。

官方 conda 文档还包含有关指定通道的更多信息。

安装 conda 包#

使用 conda install 命令将包安装到环境中。 如果命令中未指定环境,则 conda 会将包安装到工作环境中。运行 conda install --help 以查看帮助信息和可用选项列表。

注意

对于您的项目,Anaconda 强烈建议创建一个单独的 conda 环境来工作,而不是将您的项目包安装在基础环境中。 这可以保护您的基础环境免受因复杂的依赖冲突而损坏,并允许您在其他机器上轻松管理和重现您的环境。

要安装 conda 包,请运行以下命令

# Replace <PACKAGE> with the name of the package you want to install
conda install <PACKAGE>

要安装多个包,请列出以空格分隔的包

# Replace <PACKAGE> with the name of the package you want to install
conda install <PACKAGE> <PACKAGE> <PACKAGE>

要从特定通道安装

# Replace <PACKAGE> with the name of the package you want to install
# Replace <CHANNEL> with the URL or name of the channel you want to install from
conda install <CHANNEL>::<PACKAGE>

有关指定通道和设置通道别名的更多信息,请参阅上面的 搜索特定通道

要在工作环境以外的环境中安装包

# Replace <PACKAGE> with the name of the package you want to install
# Replace <ENVIRONMENT> with the name of the environment where you want to install the package
conda install <PACKAGE> --name <ENVIRONMENT>

指定包版本#

默认情况下,从命令行安装包时,conda 会从存储库通道检索最新的可用版本。 要定义包版本,conda 使用 MatchSpec 作为其查询语言。

这是一个安装 NumPy 2.2.2 版本的示例命令

conda install numpy=2.2.2

您还可以使用通配符和 MatchSpec 与 conda 匹配包的范围。 有关更多信息,请参阅关于 匹配规范的官方 conda 文档。

管理 Python 环境#

当您尝试在环境中安装包时,conda 会检查您的当前环境(或安装命令指定的环境)中安装了哪个版本的 Python,并且仅安装与该 Python 版本兼容的包。

如果您尝试安装的包没有与环境的 Python 版本兼容的版本,您可以创建一个使用不同 Python 版本的新环境。 Conda 将 Python 视为与任何其他包相同,因此易于管理和更新多个安装。

创建新环境并安装不同版本的 Python

# Replace <ENVIRONMENT> with the name of the new environment
# Replace <VERSION> with the specific version of Python you want to install
conda create --name <ENVIRONMENT> python=<VERSION>

有关管理 Python 的更多信息,请参阅官方 conda 文档。

在非联网(air-gapped)计算机上安装包#

要直接从本地计算机安装 conda 包

# Replace <PACKAGE_PATH/PACKAGE> with the relative path to the package.
conda install <PACKAGE_PATH/PACKAGE>.conda

Conda 将包安装到 anaconda/pkgs 目录中。 Conda 支持 .conda.tar.bz2 文件类型。

如果 conda 找不到该文件,请尝试使用绝对路径名而不是相对路径名。

注意

直接从本地文件安装包不会解决其依赖项。 如果您安装的包无法正常工作,则可能缺少需要手动解决的依赖项。