vscode python自动完成在单独目录中生成的代码

发布于 2025-02-05 14:48:55 字数 770 浏览 1 评论 0原文

我使用裤子管理使用协议缓冲区的Python项目。裤子将生成的_pb2.py_pb2.pyi文件放在单独的dist/codegen tree下。使用_PB2模块时,是否可以使VS代码自动完成工作?

文件树看起来像这样:

.
|-- dist/
|   `-- codegen/
|       `-- src/
|           `-- project/
|               |-- data_pb2.py
|               `-- data_pb2.pyi
`-- src/
    `-- project/
        |-- __init__.py
        |-- code.py
        `-- data.proto

并且在code.py中我有类似的导入语句:

from project import data_pb2

我尝试设置python.analysis.Analysis.extrapaths to [ /codegen/src“]settings.json中。这使得Pylance停止抱怨data_pb2缺少。但是AutoComplete仍然不起作用,并且Pylance对于data_pb2的成员没有类型的信息。

I use pants to manage a Python project that uses protocol buffers. Pants places the generated _pb2.py and _pb2.pyi files under a separate dist/codegen tree. Is it possible to get VS Code autocomplete to work when using the _pb2 modules?

The file tree looks like this:

.
|-- dist/
|   `-- codegen/
|       `-- src/
|           `-- project/
|               |-- data_pb2.py
|               `-- data_pb2.pyi
`-- src/
    `-- project/
        |-- __init__.py
        |-- code.py
        `-- data.proto

And in code.py I have import statements like this:

from project import data_pb2

I've tried setting python.analysis.extraPaths to ["dist/codegen/src"] in settings.json. This makes pylance stop complaining that data_pb2 is missing. But autocomplete still does not work, and pylance has no type information for members of data_pb2.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

你げ笑在眉眼 2025-02-12 14:48:55

替换您的python.Analysis.extrapaths以下程度:

    "python.analysis.extraPaths": [
    "./dist/codegen/src"
     ],

并将以下代码添加到您的code.py:

import sys
sys.path.append(".\dist\codegen\src")

Replace your python.analysis.extraPaths with the following extent:

    "python.analysis.extraPaths": [
    "./dist/codegen/src"
     ],

And adding the following code to your code.py:

import sys
sys.path.append(".\dist\codegen\src")
等待圉鍢 2025-02-12 14:48:55

您可以使用python隐式命名空间套件( pep 420 )来完成此工作。允许命名空间软件包在同一软件包中具有不同目录中的模块。当代码分配在srcdist/codegen/codegen/src之间时,允许钢丝和其他工具正常工作。

要使用隐式命名空间软件包,您只需要删除src/package/__ init __. py ,然后离开“ python.analysis.extrapaths”设置为[ /codegen/src“]

另请参见GitHub问题 Microsoft/pylance-rease#2855555555555 隐式名称空间软件包可以使远程在类似情况下正常工作。

You can use Python implicit namespace packages (PEP 420) to make this work. Namespace packages are allowed to have modules within the same package reside in different directories. Which allows pylance and other tools to work correctly when code is split between src and dist/codegen/src.

To use implicit namespace packages, you just need to remove src/package/__init__.py, and leave "python.analysis.extraPaths" set to ["dist/codegen/src"].

See also the GitHub issue microsoft/pylance-release#2855, which describes using implicit namespace packages to make pylance work correctly in a similar situation.

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