Search code examples
pythonvisual-studio-codevscode-remote

VSCode "go to definition" uses wrong Python module


I am importing a module foo found in the same directory as my importing Python script.

    src/proj
           \_ main.py
           |_ foo.py

However, there is a foo installed in /usr/local/lib/python3.6/site-packages as well.

When I try to use VSCode to jump to a method defined in foo ("go to definition", F12), nothing happens (because the system instance of foo does not have such a method). If I hover over the import itself and go to definition, I end up in the system foo (which is a directory containing __init__.py and the rest of the module contents).

How can I force VSCode to use my foo.py for the "go to definition/declaration/etc." actions instead of the system foo? (I believe the correct Python interpreter is configured.)

I've tried adding '.' to extraPaths (Python), but that did not help. I even tried adding '.' to the beginning of sys.path, which also did not seem to help.

Note that I am editing remotely via SSH.


Solution

  • First of all, I was wrong in thinking that only foo was not resolving. In fact, the other modules in the same directory were not resolving either. In those cases, since there was no conflicting name in the system space, I could not "go to definition" for the import bar line at all.

    Second, a solution that seems to work for me is to "Open Folder" instead of opening each file I'm working with. If I open the folder, then VSCode can and does resolve the references correctly, including foo. It does not get confused. Rather, it opens my foo and not the system foo.