vscode python自动完成在单独目录中生成的代码
我使用裤子管理使用协议缓冲区的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
替换您的
python.Analysis.extrapaths
以下程度:并将以下代码添加到您的code.py:
Replace your
python.analysis.extraPaths
with the following extent:And adding the following code to your code.py:
您可以使用python隐式命名空间套件( pep 420 )来完成此工作。允许命名空间软件包在同一软件包中具有不同目录中的模块。当代码分配在
src
和dist/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
anddist/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.