Windows Python 安装 plyvel

在 Windows 上安装 Python 的 LevelDB 库 plyvel 时,会遇到报错:

fatal error C1083: Cannot open include file: 'leveldb/db.h': No such file or directory

解决步骤如下:

1、安装 vcpkg 库。这是来自微软的 C/C++ 包管理程序,开源安装方式如下。

git clone https://github.com/microsoft/vcpkg
.\vcpkg\bootstrap-vcpkg.bat

2、使用 vcpkg 安装 leveldb 库和头文件。注意这里要根据编译的目标平台区分是x86和x64。

.\vcpkg\vcpkg install leveldb --triplet=x64-windows

3、然后在执行 pip 时加上如下参数:

pip install plyvel --config-settings=build_ext --config-settings="-I[PATH TO vcpkg]\installed\x64-
windows\include" --config-settings="-L[PATH TO vcpkg]\installed\x64-windows\lib" --config-settings="-lleveldb"

build_ext 命令的参数如下:

Options for 'build_ext' command:
--build-lib (-b) directory for compiled extension modules
--build-temp (-t) directory for temporary files (build by-products)
--plat-name (-p) platform name to cross-compile for, if supported (default: linux-x86_64)
--inplace (-i) ignore build-lib and put compiled extensions into the source directory alongside your pure Python modules
--include-dirs (-I) list of directories to search for header files (separated by ':')
--define (-D) C preprocessor macros to define
--undef (-U) C preprocessor macros to undefine
--libraries (-l) external C libraries to link with
--library-dirs (-L) directories to search for external C libraries (separated by ':')
--rpath (-R) directories to search for shared C libraries at runtime
--link-objects (-O) extra explicit link objects to include in the link
--debug (-g) compile/link with debugging information
--force (-f) forcibly build everything (ignore file timestamps)
--compiler (-c) specify the compiler type
--swig-cpp make SWIG create C++ files (default is C)
--swig-opts list of SWIG command line options
--swig path to the SWIG executable
--user add user include, library and rpath
--help-compiler list available compilers

上述用到的参数为指定 include 目录的 -I、指定库目录的 -L 和指定库的 -l,即命令列表中加粗的部分。参数后直接跟路径或库名。上述完整命令如下。

pip install plyvel --global-option=build_ext --global-option="-LC:\Users\User\basedir\vcpkg\installed\x64-windows\include" --global-option="-LC:\Users\User\basedir\vcpkg\installed\x64-windows\lib" --global-option="-lleveldb"

参考资料:
[1] python安装plyvel报错找不到leveldb头文件. https://zhuanlan.zhihu.com/p/666308201
[2] vcpkg overview. https://learn.microsoft.com/en-us/vcpkg/
[3] vcpkg. https://github.com/microsoft/vcpkg
[4] python pip specify a library directory and an include directory. https://stackoverflow.com/questions/18783390/python-pip-specify-a-library-directory-and-an-include-directory

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据