Are there any tricks for getting VSCode to see gen...
# general
c
Are there any tricks for getting VSCode to see generated Python code in
dist/codegen
? E.g.,
_pb2.py
from
protobuf_sources
. Pointing VSCode at the virtualenv created by
pants export
gives me a mostly usable VSCode experience, but I still get mypy errors inside VSCode about missing
_pb2
modules. I found that I can add symlinks in the same directories as my
.proto
files that point to the generated files in
dist/codegen
. And add
*_pb2.py
and
*_pb2.pyi
to
.gitignore
. But this feels, uh, pretty kludgy.
h
Yeah, I was able to adjust a python interepreter setting for it. Let me go dig up what I did.
I added this to my
.vscode/settings.json
file
Copy code
"python.analysis.extraPaths": [
    "dist/codegen/<source root here>",
    "dist/codegen/<another source root here>"
  ]
After that, it just worked ™️
c
Hm, that doesn't seem to work for me. Do you have your
.proto
files in the same dir as code that uses them? E.g., I have something like
awesome/data.proto
and
awesome/stuff.py
which has a
from awesome import data_pb2
.
h
Yup, I did
<source root here>
came from
source.root_patterns
in my
pants.toml
file.
c
Still can't get it to work, but I can see in the pylance trace logs that it at least sees the file. Will have to keep debugging, maybe I can get it to work. Helpful to know that it works for someone else though, thanks for the lead!
👍 1
q
I ran into some problems getting this working at first and it turned out that PyCharm (in our case) wasn't able to resolve imports correctly when we had multiple top-level python source roots as well as
dist/codegen
— once we isolated the python source tree everything worked
c
I think I found a solution to this, and an explanation why simply adding codegen paths to
"python.analysis.extraPaths"
Just Works for some. PEP 420 (implicit namespace packages) means that Python will merge packages in different directories as long as none of the directories have an
__init__.py
. So removing the
__init__.py
file from my checked in source directory means that Python can merge the checked in source and the codegen source as part of the same package, and everything seems to work.
👍 1