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

Pylance无法识别带子模块的原生Python绑定库存根文件问题求助(已在pylance-release仓库提交Issue)

VS Code Python语言服务器无法识别我的多子模块绑定库存根文件

我正在开发一个基于pybind11的Python绑定库lg_hello_imgui,它会向外暴露imgui、hello_imgui和implot三个子模块。在PyCharm或者CLion里,我可以毫无障碍地导航到这些子模块对应的存根文件,但到了Visual Studio Code这边就彻底卡壳了:

  • 使用Jedi语言服务器时,偶尔还能跳转到函数定义,但用个几分钟就完全失效;
  • 换成Pylance语言服务器的话,从一开始就根本识别不了这些存根文件。

这个问题在我的Mac M1设备(OSX系统),还有通过Parallels运行的Windows ARM环境里都出现了,我不确定是不是环境配置的问题。

库的结构与实现细节

已安装的包目录结构

.venv/lib/python3.9/site-packages/lg_hello_imgui/
├── __init__.py
├── _lg_hello_imgui.cpython-39-darwin.so*
├── hello_imgui.pyi
├── imgui.pyi
├── implot.pyi
└── py.typed

pybind11的模块定义

_lg_hello_imgui是原生库,通过pybind11定义子模块的代码如下:

PYBIND11_MODULE(_lg_hello_imgui, m) {
    auto module_imgui = m.def_submodule("imgui");
    py_init_module_imgui(module_imgui);
    auto module_himgui = m.def_submodule("hello_imgui");
    py_init_module_hello_imgui(module_himgui);
    auto module_implot = m.def_submodule("implot");
    py_init_module_implot(module_implot);
}

主模块的导入逻辑

lg_hello_imgui/__init__.py中的导入语句:

from lg_hello_imgui._lg_hello_imgui import imgui
from lg_hello_imgui._lg_hello_imgui import hello_imgui
from lg_hello_imgui._lg_hello_imgui import implot

最小复现示例

我已经制作了一个最小复现的仓库,可以重现VS Code语言服务器的这个识别问题。

补充说明

目前这个库还没有提供wheel包,构建的时候需要耗时1-2分钟。


内容的提问来源于stack exchange,提问作者Pascal T.

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.27 20:12:26