You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

pybind11最简模块编译成功后无法导入,求排查原因

Pybind11编译DLL后无法导入的问题

问题详情

刚接触pybind11,尝试官方最简示例,代码如下:

#include <pybind11/pybind11.h>

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

PYBIND11_MODULE(example, m) {
    m.doc() = "pybind11 example plugin"; // 可选的模块文档字符串

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

使用Visual Studio 2022编译该代码为DLL,无编译错误,在x64/Debug目录生成以下文件:

example.dll
example.exp
example.lib
example.pdb

进入x64/Debug目录,运行与pybind11版本匹配的Python3.12,执行import example时出现错误:

>>> import example
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'example'

编译输出中开启详细日志后得到的cl.exe参数如下:

CL.exe /c /IC:\Users\usr\AppData\Local\Programs\Python\Python312\include /I"C:\Users\usr\source\repos\python_cxx_interaction\python_cxx_interaction\.venv\Lib\site-packages\pybind11\include" /Zi /nologo /W3 /WX- /diagnostics:column /sdl /O2 /Oi /GL /D NDEBUG /D _CONSOLE /D _WINDLL /D _UNICODE /D UNICODE /Gm- /EHsc /MD /GS /Gy /fp:precise /Zc:wchar_t /Zc:forScope /Zc:inline /permissive- /Fo"python_c.f6c111de\x64\Release\\" /Fd"python_c.f6c111de\x64\Release\vc143.pdb" /external:W3 /Gd /TP /FC /errorReport:prompt example.cpp

请问哪里操作出错了?


内容的提问来源于stack exchange,提问作者roi_saumon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 04:45:05