我正在学习PYBIND,并遵循本教程https://pybind11.readthedocs.io/en/stable/installing.html

我的行为

python3.10 -m venv venv
source venv/bin/activate
pip install pybind11

然后,我将创建Example.cpp

#include <pybind11/pybind11.h>

int add(int i, int j) {
    return i + j;
}

PYBIND11_MODULE(example, m) {
    m.doc() = "pybind11 example plugin"; // optional module docstring

    m.def("add", &add, "A function that adds two numbers");
}

并try 着

g++ -O3 -Wall -shared -std=c++11 -fPIC $(python3 -m pybind11 --includes) example.cpp -o example$(python3-config --extension-suffix)

我犯了一个错误:

未定义的建筑符号ARM 64: "_PyBaseObject_Type",引用自: 示例中的pybind11::detail::make_object_base_type(_typeobject*)-243936.o

-undefined dynamic_lookup的帮助下修正了错误

g++ -O3 -Wall -shared -std=c++11  -undefined dynamic_lookup   -fPIC $(python3 -m pybind11 --includes) example.cpp -o example$(python3-config --extension-suffix)

然后我收到了DLL库Example.cpython-39-darwin.so 我正试着用图书馆

# python
Python 3.10.5 (v3.10.5:f377153967, Jun  6 2022, 12:36:10) [Clang 13.0.0 (clang-1300.0.29.30)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pybind11
>>> import example
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'example'
>>> 

如何修复导入错误?

细节

MacOS version 12.2.1

pip list
Package    Version
---------- -------
pip        22.0.4
pybind11   2.10.4
setuptools 58.1.0

Python 3.10.5

g++ --version
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX12.3.sdk/usr/include/c++/4.2.1
Apple clang version 13.0.0 (clang-1300.0.27.3)
Target: arm64-apple-darwin21.3.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

推荐答案

虽然添加$(python3-config --embed --ldflags)作为注释可能会有所帮助,提供嵌入Python解释器(用作g++ -O3 -Wall -shared -std=c++11 -undefined dynamic_lookup -fPIC $(python3 -m pybind11 --includes) example.cpp -o example$(python3-config --extension-suffix) $(python3-config --embed --ldflags) )所需的链接器标志,但这不能解释最初的错误:

ModuleNotFoundError: No module named 'example'

That error should mean the shared library (example.cpython-39-darwin.so) isn't in a directory where Python can find it.
When you try to import example, Python looks in several places (like the current directory, and the directories listed in your PYTHONPATH environment variable) for a file named example.py or example.so.

所以试着把example.cpython-39-darwin.so人的文件夹加到你的PYTHONPATH里:

export `PYTHONPATH=/path/to/directory:$PYTHONPATH`

Python相关问答推荐

强制venv在bin而不是收件箱文件夹中创建虚拟环境

DuckDB将蜂巢分区插入拼花文件

使用scipy. optimate.least_squares()用可变数量的参数匹配两条曲线

如何在图片中找到这个化学测试条?OpenCV精明边缘检测不会绘制边界框

try 与gemini-pro进行多轮聊天时出错

. str.替换pandas.series的方法未按预期工作

有症状地 destruct 了Python中的regex?

Python中绕y轴曲线的旋转

我如何根据前一个连续数字改变一串数字?

Pandas DataFrame中行之间的差异

在pandas中使用group_by,但有条件

Geopandas未返回正确的缓冲区(单位:米)

如何创建引用列表并分配值的Systemrame列

ruamel.yaml dump:如何阻止map标量值被移动到一个新的缩进行?

Python pint将1/华氏度转换为1/摄氏度°°

如果有2个或3个,则从pandas列中删除空格

多个矩阵的张量积

如何写一个polars birame到DuckDB

仅取消堆叠最后三列

Django更新视图未更新