has anyone else had issues with VS Code and Python...
# general
f
has anyone else had issues with VS Code and Python imports? I can see the package in
dist/export/python/virtualenvs/python-default/...
but
Pylint
is telling me that it can't find the 'member' for
Module 'A'
when doing
import A
and calling
A.func()
. Similarly doing
from A import X, Y
also errors saying no name 'X' in module 'A'. It seems to know about
A
but not what's inside? not sure if this is pants related or VS Code specific but either way I'm find it hard to find any info
f
I think what you are describing is that VSCode is not picking up the PYTHONPATH or the package isn’t included. The steps on this page work for me https://www.pantsbuild.org/docs/setting-up-an-ide. I get caught by the fact that I work in a devcontainer and the virtual environment is deleted on a rebuild
👀 1
f
I'll try that - thanks!
b
I got this to work once, but then I have to regenerate the venv after installing new packages, and now VS Code fails to find the new one.
f
I usually have to restart VSCode after regenerating the venv
b
Did that multiple times. It seems to track follow the symlink of python into asdf install, and then lost track of the venv.
f
Are you sure the symlink under venv isn’t getting corrupted somehow?
b
I deleted the entire thing and re-exported it about 12 times today 😞
just for me to verify, this is the command I used to regenerate the venv:
Copy code
pants export --py-resolve-format=symlinked_immutable_virtualenv --resolve=reqs
f
in pants.toml I have
Copy code
resolves = { python-default = "python-default.lock"}
and in the command
--resolve=python-default
b
That's just a different name for the resolve.
f
right. so we are doing the same thing
b
I have:
Copy code
[python]
interpreter_constraints = [">=3.10,<=3.12"]
enable_resolves = true
default_resolve = "reqs"
f
does default_resolve create a lockfile?
(not that it seems like it should matter)
actually, are you recreating the link after pants export?
b
Lockfile was created, but I don't have to recreate the link. It generates a new one.
f
OK, well, I admit I haven’t looked at the instructions lately, but the script I’m using runs
pants export
and then
ln -snf
linking venv to the virtual environment directory for the resolve. it seems the links under venv do change
b
Can you share the content of that script that?
f
Copy code
#!/bin/sh
REPO_DIR=/workspaces/flywheel-gear-extensions

ROOTS=$(pants roots)
python3 -c "print('PYTHONPATH=\"./' + ':./'.join('''${ROOTS}'''.split('\n')) + ':\$PYTHONPATH\"')" > .env

if [ ! -e python-default.lock ]; then
    pants generate-lockfiles
else
    echo "Skipping generation of lock file"
fi

pants export --py-resolve-format=symlinked_immutable_virtualenv --resolve=python-default

ln -snf dist/export/python/virtualenvs/python-default $REPO_DIR/venv
some details hard coded
b
My path looks a little different:
dist/export/python/virtualenvs/reqs/3.11.3
. Looks like I have a python version tagged on. Do you have the same thing?
f
Yes. To do the same thing as I’m doing you would link to dist/export/python/virtualenvs/reqs
b
I have now learned more about how VS code find venv from pants that I ever thought. It turns out the only way I can get VS code to latch on to the generated venv is to use the actual path in the case, and not the symlink. E.g., it only works if I set the python interpreter to
~/.cache/pants/named_caches/pex_root/venvs/229ed584e43343414b852a697540d96f1016b78f/aad4b57db4397cb1d79fcc114dd2cd89a3311113/bin/python3.11
. Not really sure why.
😮 1
😑 1
f
are you finding that path manually? and how are you setting the interpreter—within a
settings.json
for a specific project in the Workspace?
b
Yes, I just look at the symlink created in dist/export/python/
To set the interpreter, I use the command palette "Python: Select Interpreter" and paste in the full path. Sometimes I need to do it a couple of times before it sticks. I think it may be a bit buggy on the VS code side.
f
definitely agree on VS Code being flaky about finding the virtual environment even when you follow the guidance
f
does setting it via the command palette set it globally though?
b
It sets it for the project.
VS code keeps track of what env is used for each folder in a sqlite db somewhere, globally.
I've tried to override it with workspace setting, and I haven't gotten that to work yet.
f
ah okay, thanks I'll give it a try
Yes, I just look at the symlink created in dist/export/python/
how exactly do you check the symlink? I can't see how to link it to the cache, I have multiple folders inside
~/.cache/pants/named_caches/pex_root/venvs/
b
find the path from your repo's root directory
ls -l dist/export/python/virtualenvs/python-default/3.x.x/
and see what it points to. In my case, I see this:
Copy code
$ ls -l dist/export/python/virtualenvs/reqs/3.11.6 
lrwxr-xr-x@ 1 joe  staff   133B Oct 18 22:23 dist/export/python/virtualenvs/reqs/3.11.6@ -> /Users/joe/.cache/pants/named_caches/pex_root/venvs/7ea401a8a5c94fa5f10b264a4e04c88708efabb6/aad4b57db4397cb1d79fcc114dd2cd89a3311113
👀 1