10

I am using VSCode for Python along with the Microsoft for Python extension enabled in VSCode.

For Python v3.9.0 I am getting No definition found if I try to seek a function definition.

enter image description here

However, I do not get the error if I use my Conda Virtual environment for Python 3.7.0

What might be the problem?

Mihir
  • 320
  • 2
  • 3
  • 14

7 Answers7

9

When I used the code you provided and disabled the Python extension, I encountered the same problem as you.

Since "Go to Definition" is supported by the corresponding language service extension, it is recommended that you check that the current Python extension is available and confirm that the selected python interpreter is also available. In addition, please try to reload VSCode.

Jill Cheng
  • 7,195
  • 1
  • 13
  • 22
6

I had the same problem and changing the python language server from Jedi to Microsoft or Pylance did fix the problem. To do this go to the settings with Cntrl + , and search for python.languageserver

Bart
  • 87
  • 7
1

Making sure you're using a correct interpreter will allow you to import third-party packages installed for that interpreter.

However, if you're trying to lookup a definition for your own module, make sure your PYTHONPATH is set correctly and that you have an empty __init__.py inside your module.

Let's say your module is defined inside

[project_root]/src/my_library

Your PYTHONPATH defined in .env file (located in the project root) should contain:

PYTHONPATH=$PYTHONPATH:./src

(note that, in Windows, the separator is ; and not :). Also, make sure you have created the file [project_root]/src/my_library/__init__.py

Tomasz Bartkowiak
  • 8,661
  • 2
  • 48
  • 56
1

after upgrading python to 3.9 I had this issue. I found that Python: Default Interpreter Path was still pointing at 2.7.

I also switched the Python: Language Server to Pylance. I'll experiment with Language server to see if that has an effect. before this I was getting Language server has crashed 3x not restarting it. After reinstalling that, I don't get that error anymore.

scott
  • 11
  • 1
1

I encountered the same problem after installing some conda packages that may have interfered with the already present pip packages.

My solution was the following:

  • set your Python Interpreter (you find it by typing "interpreter" in Command Palette) to the correct version of Python.
  • add within your settings.json file the option: "python.languageServer" : "Microsoft". Save file.
  • restart VS Code.

Note on how to open Command Palette: simply type ctrl-P (on Windows).

Note on how to open settings.json file (if you are a noob like me): open the command palette and search for "settings", then "Preferences: Open Settings (JSON)".

ixaixim
  • 43
  • 6
0

Recently, I faced a similar issue. In my case, it was an issue of go modules.

Just running go mod verify worked like a charm.

Dharman
  • 26,923
  • 21
  • 73
  • 125
0

You can use pylint, which worked for me.

Install pylint by running:

pip install -U pylint --user
person_v1.32
  • 1,737
  • 10
  • 20