No definition found for function – VSCode Python

Question:

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?

Asked By: Mihir

||

Answers:

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.

Answered By: Jill Cheng

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

Answered By: Bart

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

Answered By: Tomasz Bartkowiak

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.

Answered By: Yug Gupta

You can use pylint, which worked for me.

Install pylint by running:

pip install -U pylint --user
Answered By: Joker81

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.

Answered By: scott

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)".

Answered By: ixaixim

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
Answered By: user15420598
  • install pylance extension
  • preferences -> settings, serach "languageserver", select Pylance
Answered By: zc huang
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.