No definition found for function – VSCode Python
Question:
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.
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
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
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.
You can use pylint
, which worked for me.
Install pylint by running:
pip install -U pylint --user
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.
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)".
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.
- uninstalled kite (saw a post somewhere saying it interferes with go to defintion functionality)
- CTRL SHIFT P – Reload with all extensions disabled
- 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
- install
pylance
extension
preferences -> settings
, serach "languageserver", select Pylance
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.
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
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
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.
You can use pylint
, which worked for me.
Install pylint by running:
pip install -U pylint --user
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.
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)".
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.
- uninstalled kite (saw a post somewhere saying it interferes with go to defintion functionality)
- CTRL SHIFT P – Reload with all extensions disabled
- 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
- install
pylance
extension preferences -> settings
, serach "languageserver", selectPylance