安装 conda 软件包#
conda 包是一个压缩的 tarball (.tar.bz2
) 或.conda
文件,其中包含在 Conda 环境中安装特定软件所需的一切。这包括:
软件/库本身和任何必需的依赖项。
有关包的元数据(版本、作者等)。
安装脚本和配置信息。
适用于不同作系统和架构的预编译二进制文件。
您可以使用 conda 包管理器从 Anaconda 的公共存储库访问和安装 8,000 多个开源数据科学和机器学习 conda 包,以及在 Anaconda.org 和 conda-forge 上发布的数千个社区开发的包。
您甚至可以使用conda-build
工具,然后通过将它们上传到 Anaconda.org 或 conda-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 会检查当前环境(或由 install 命令指定的环境)中安装的 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-gap)计算机上安装软件包#
要从本地计算机直接安装 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 找不到该文件,请尝试使用绝对路径名而不是相对路径名。
谨慎
直接从本地文件安装包不会解决其依赖关系。如果已安装的软件包不起作用,则可能缺少需要手动解决的依赖项。