3. 安装说明

我们可以在pybind/pybind11 on GitHub获取到pybind11的源码。推荐pybind11开发者使用下面介绍的前三种方法之一,来获取pybind11。

3.1 以子模块的形式集成

当你的项目使用Git管理时,你可以将pybind11当做一个子模块嵌入到你的项目中。在你的git仓库,使用以下命令即可包含pybind11:

  1. git submodule add -b stable ../../pybind/pybind11 extern/pybind11
  2. git submodule update --init

这里假设你将项目的依赖放在了extern目录下,并且使用GitHub。如果你没有使用GitHub,可以使用完整的https或ssh URL来代替上面的相对URL../../pybind/pybind11。一些服务器可能需要.git扩展(GitHub不用)。

到这一步后,你可以直接include extern/pybind11/include目录即可。或者,你可以使用各种集成工具(见Build System一章)来包含pybind11。

3.2 通过PyPI来集成

你可以使用pip,通过PyPI来下载Pybind11的Python包,里面包含了源码已经CMake文件。像这样:

  1. pip install pybind11

这样pybind11将以标准的Python包的形式提供。如果你想在root环境下直接使用pybind11,可以这样做:

  1. pip install "pybind11[global]"

如果你使用系统自带的Python来安装,我们推荐在root环境下安装。这样会在/usr/local/include/pybind11/usr/local/share/cmake/pybind11添加文件,除非你想这样。还是推荐你只在虚拟环境或你的pyproject.toml中使用。

3.3 通过conda-forge集成

You can use pybind11 with conda packaging via conda-forge:

  1. conda install -c conda-forge pybind11

3.4 通过vcpkg集成

你可以通过Microsoft vcpkg依赖管理工具来下载和安装pybind11:

  1. git clone https://github.com/Microsoft/vcpkg.git
  2. cd vcpkg
  3. ./bootstrap-vcpkg.sh
  4. ./vcpkg integrate install
  5. vcpkg install pybind11

3.5 通过brew全局安装

brew包管理(Homebrew on macOS, or Linuxbrew on Linux)有pybind11包。这样安装:

  1. brew install pybind11

3.6 其他方法

Other locations you can find pybind11 are listed here; these are maintained by various packagers and the community.