Importerror:无模块名称< module_name> Macos上的Pybind11

发布于 2025-01-25 11:04:00 字数 672 浏览 4 评论 0原文

我正在尝试导入使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

看海 2025-02-01 11:04:00

正如您注意到的那样,编译的库必须位于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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文