24

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
  • 520
  • 2
  • 6
  • 18

9 Answers9

27

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
  • 442
  • 1
  • 3
  • 9
  • 1
    setting it to "Microsoft" fixed the issue for me – wicccked Jun 01 '21 at 22:22
  • After literal months of troubleshooting this problem your answer FINALLY solved it for me. I can go to definition, and intellisense is working properly. Thanks so much! – Samwise Ganges Oct 05 '22 at 23:18
  • 1
    Interestingly, in my case switching from Pylance to Jedi fixed it – John Paul R Oct 29 '22 at 19:24
  • 1
    This concept worked for me and switching from default to Pylance worked. (Microsoft wasn't an available option for me) – JGFMK Nov 17 '22 at 08:32
  • Microsoft was not present for me also, Pylance did not work, but Jedi does fix the problem for me. So weird. – juan Apr 14 '23 at 13:45
13

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
  • 9,179
  • 1
  • 20
  • 25
3

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
  • 83
  • 8
  • 2
    Error editing settings.json: Value is not accepted. Valid values: "Default", "Jedi", "Pylance", "None". – quickdraw Sep 14 '22 at 03:53
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
  • 12,154
  • 4
  • 57
  • 62
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
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
  • 30,962
  • 25
  • 85
  • 135
0

You can use pylint, which worked for me.

Install pylint by running:

pip install -U pylint --user
person_v1.32
  • 2,641
  • 1
  • 14
  • 27
0

Some of the answers regarding python interpreter are out of date on version 1.68 as interpreter selection is explicitly done on start of jupyter notebook files. But I had the same problem , what worked for me was one or more of the following steps - not sure exactly what fixed it.

  1. uninstalled kite (saw a post somewhere saying it interferes with go to defintion functionality)
  2. CTRL SHIFT P - Reload with all extensions disabled
  3. CTRL SHIF P - Reload Window on this last reload an option at the right bottom allowed reload with all extension enabled after this, the go to definition started working for functions I had defined in other .py files
user15420598
  • 107
  • 7
0
  • install pylance extension
  • preferences -> settings, serach "languageserver", select Pylance