Importerror:无模块名称< module_name> Macos上的Pybind11
我正在尝试导入使用PYBIND11创建的C ++模块到Python脚本。 目录结构为:
pybind_test:
main.cpp
build
CMakeLists.txt
test.py
pybind11 (github repo clone)
它成功构建,并创建了文件module_name.cpython-39-darwin.so
。但是,在运行测试时。我会得到:
File "../test.py", line 2, in <module>
from build.module_name import *
ImportError: No module named build.module_name
我的cmakelists文件:
cmake_minimum_required(VERSION 3.4...3.18)
set(CMAKE_CXX_STANDARD 17)
project(pybindtest)
add_subdirectory(pybind11)
pybind11_add_module(module_name main.cpp)
如何像普通的python模块一样将此模块导入python?
I am trying to import C++ module created using pybind11 to python script.
The directory structure is:
pybind_test:
main.cpp
build
CMakeLists.txt
test.py
pybind11 (github repo clone)
It builds successfully and the file module_name.cpython-39-darwin.so
is created. However when running test.py I get:
File "../test.py", line 2, in <module>
from build.module_name import *
ImportError: No module named build.module_name
My CMakeLists file:
cmake_minimum_required(VERSION 3.4...3.18)
set(CMAKE_CXX_STANDARD 17)
project(pybindtest)
add_subdirectory(pybind11)
pybind11_add_module(module_name main.cpp)
How would I import this module into python like a normal python module?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如您注意到的那样,编译的库必须位于Python可执行文件中可以到达的路线(在文件路径方面)(您是通过将
test.py
来实现的,而构建库为build Directory )。您也可以将编译的库移至Python的
站点包装
文件夹,其中存储了其他模块。As you noticed, the compiled library must be in a route (in terms of file path) reachable by the Python executable (which you achieved by moving
test.py
to the build directory, where your compiled library is).You could also move the compiled library to your Python's
site-packages
folder, where other modules are stored.