-1

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.

tanager
  • 189
  • 2
  • 10
  • Does this answer your question? [How do I import other Python files?](https://stackoverflow.com/questions/2349991/how-do-i-import-other-python-files) – TomServo Apr 07 '23 at 22:58
  • @TomServo, there is nothing wrong with the way I'm importing `foo`, as far as I know. The script runs correctly. It is just that VSCode insists on resolving the module to the other instance of `foo`. – tanager Apr 07 '23 at 23:28
  • @TomServo the question is about getting **the IDE** to recognize the path, not Python. – Karl Knechtel Apr 08 '23 at 21:06

1 Answers1

1

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.

tanager
  • 189
  • 2
  • 10