安装库#

有几种方法可以获得 pybind11 源代码,它位于 GitHub 上的 pybind/pybind11。pybind11 开发人员推荐这里列出的三种方法中的一种,即子模块、PyPI 或 conda-forge 来获取 pybind11。

作为子模块包含#

当你在 Git 中做项目时,你可以使用 pybind11 库作为子模块。从你的 git 仓库中,使用:

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

这假设你的依赖放在 extern/ 中,并且你正在使用 GitHub;如果你不使用 GitHub,使用完整的 https 或 ssh URL,而不是相对 URL ../../pybind/pybind11。其他一些服务器也需要 .git 扩展名(GitHub 不需要)。

从这里开始,您现在可以包含 extern/pybind11/include,或者可以使用 pybind11 直接从本地文件夹提供的各种集成工具(请参阅 构建系统)。

使用 PyPI#

您可以使用 pip 从 PyPI 下载源代码和 CMake 文件作为 Python 包。只使用:

pip install pybind11

这将以标准 Python 包格式提供 pybind11。如果您希望 pybind11 在您的环境根目录中直接可用,您可以使用:

pip install "pybind11[global]"

如果您使用系统 Python 进行安装,则不建议这样做,因为它会将文件添加到 /usr/local/include/pybind11/usr/local/share/cmake/pybind11,所以除非这是您想要的,否则建议只在虚拟环境或您的 pyproject.toml 中使用(参见 构建系统)。

使用 conda-forge#

您可以使用 pybind11 借助 conda-forge 打包:

conda install -c conda-forge pybind11

使用 vcpkg 包含#

You can download and install pybind11 using the Microsoft vcpkg dependency manager:

git clone https://github.com/Microsoft/vcpkg.git
cd vcpkg
./bootstrap-vcpkg.sh
./vcpkg integrate install
vcpkg install pybind11

The pybind11 port in vcpkg is kept up to date by Microsoft team members and community contributors. If the version is out of date, please create an issue or pull request on the vcpkg repository.

Global install with brew#

The brew package manager (Homebrew on macOS, or Linuxbrew on Linux) has a pybind11 package. To install:

brew install pybind11

Other options#

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