Python无法加载boost.Python dll

2024-06-25 05:54:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我在一个简单的boostpython设置中遇到了一些问题。 我看到很多其他人都有问题,但没有一个问题与我的问题相同,因为他们的决议都不起作用。 作为参考,我在Windows 10上,使用MINW64 10.2作为McS2的一部分用于我的C++编译器。我使用该编译器构建boost进行调试和优化,并使用该编译器构建了一个针对boost.python的dll链接

我的Cmake文件:

cmake_minimum_required(VERSION 3.20)
 
project(test)
set(CMAKE_CXX_STANDARD 20)

find_package(Python3 REQUIRED COMPONENTS Interpreter Development) 
include_directories(${Python3_INCLUDE_DIRS})


set(Boost_ARCHITECTURE -x64)
set(Boost_NO_WARN_NEW_VERSIONS ON) 
# set(Boost_DEBUG ON)
set(Boost_INCLUDE_DIR "C:/Devel/install/include/boost-1_76")
set(Boost_LIBRARY_DIR "C:/Devel/install/lib")
SET(Boost_USE_STATIC_LIBS OFF)
SET(Boost_USE_STATIC_RUNTIME OFF)
add_definitions("-DBOOST_ALL_DYN_LINK")
add_definitions("-DBOOST_UUID_USE_SSE2")
add_definitions("-DBOOST_UUID_USE_SSE3")
add_definitions("-DBOOST_PYTHON_STATIC_LIB")
SET(CMAKE_INCLUDE_PATH ${CMAKE_INCLUDE_PATH} "C:/Devel/install/include")
SET(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH} "C:/Devel/install/lib")

find_package(Boost 1.76.0 REQUIRED COMPONENTS python39)
include_directories(${Boost_INCLUDE_DIRS})

file(GLOB_RECURSE PythonBindings_SOURCES CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/PythonBindings/*.cpp")
add_library(PythonBindings SHARED ${PythonBindings_SOURCES})
target_link_libraries(PythonBindings Boost::python39 Python3::Module)
set_target_properties(PythonBindings PROPERTIES LINKER_LANGUAGE CXX PREFIX "" SUFFIX ".pyd" IMPORT_PREFIX "" IMPORT_SUFFIX ".pyd.a")
target_compile_definitions(PythonBindings PRIVATE EXPORT_PYTHONBINDINGS) 

Cpp文件:

#include <boost/python.hpp>

char const* 
helloWorld()
{
  return "Hello, world!";
}

using namespace boost::python;

BOOST_PYTHON_MODULE(PythonBindings)
{
  def("hello_world", helloWorld);
}

这将成功编译为“PythonBindings.pyd”。在dependency walker中打开它,我可以看到它导出符号“PyInit_PythonBindings”

当我尝试从python(python -vv py/helloworld.py)使用此dll时

import PythonBindings;

PythonBindings.hello_world()

我得到:

Traceback (most recent call last):
  File "C:\Devel\Working\test\py\helloworld.py", line 18, in <module>
    import PythonBindings;
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 666, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 565, in module_from_spec
  File "<frozen importlib._bootstrap_external>", line 1173, in create_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
ImportError: DLL load failed while importing PythonBindings: The specified module could not be found.

我不确定是什么问题。My$PATH变量包含PythonBindings.pyd(libboost_python39-mgw10-mt-x64-1_76.dll、kernel32.dll、MSVCRT.dll、libgcc_seh-1.dll、libstdc++-6.dll、python39.dll)的所有依赖项的搜索路径

我把DLL命名为Python模块,我知道有人已经把它们绊倒了,我用同一个C++编译器和Python版本来构建Boost和My库,我用了相同的Python版本来链接我运行Python脚本的那些。p>

关于如何解决这个问题,我完全没有主意了


Tags: incmakeincludelinedevelimportlibbootstrapfile
1条回答
网友
1楼 · 发布于 2024-06-25 05:54:34

我在doqtor的评论之后阅读了教程here,发现问题在于python没有加载几个与PythonBindings.pyd运行时相关的DLL

这是通过添加

import sys
import os
[os.add_dll_directory(dir) for dir in sys.path if os.path.isdir(dir)]

import PythonBindings之前

python不在sys.path中搜索dll依赖项,这让我感到非常奇怪。它需要搜索的路径也在os.environ['PATH']上,但它同样没有搜索它们。我不确定是否有更好的方法。如果有,我想听听

相关问题 更多 >