How to open symbols of Python dependencies?

Question:

In vscode, how can I open symbols in the dependencies of my Python project?

PyCharm indexes these symbols so I can access them via Navigate – Symbol (⌥⌘O).

Asked By: tekumara

||

Answers:

I think what you want is part of the Autocomplete and Intellisense settings of VS Code.

For your own custom packages/modules

Let’s say I have this sample structure:

.
├── main.py
└── mypkg
    ├── __init__.py
    └── mymod.py

where main.py contains:

from mypkg import mymod
mymod.myfunc()

1st, you need to add the paths to your custom packages/modules in your workspace’s .vscode/settings.json:

"python.autoComplete.extraPaths": [
    "/path/to/mypkg"
]

Next, it’s best to reload VS Code for changes in settings.json to take effect.

Then, you should now be able to right-click on the symbol (ex. myfunc) and either do Go to Definition or Go to Declaration or Peek.

go to definition

peek

For external or 3rd-party symbols

You just have to make sure that the currently selected Python interpreter is the same one where you installed the external or 3rd-party package. You can set the interpreter from the command palette as “Python: Select Interpreter“. You can check the currently set interpreter on the lower-right section of the status bar (ex. shown on the image below as “test-py37”).

status bar

Then make sure to set that same Python interpreter in your workspace’s .vscode/settings.json:

"python.pythonPath": "/path/to/.venvs/test-py37/bin/python",

AFAIK, you only have to set the interpreter once (“Python: Select Interpreter“), which would automatically set python.pythonPath in settings.json. Then save your workspace. Every time you open the workspace again, it will use the same interpreter.

Then it will be the same way as for your custom packages/modules, you can use Go to Definition or Go to Declaration or Peek. For example, I installed numpy in my test-py37 virtual environment. I can do Peek > Peek Definition to navigate to any of numpy’s functions.

enter image description here

Note

Some Python packages are actually implemented in C, and so they are installed in your site-packages as .so files. For example, OpenCV for Python will be installed as cv2.cpython-37m-darwin.so (on a Mac). For these kinds of packages, the Go to Definition or Peek features won’t work as well:

peek opencv

Answered By: Gino Mempin

The Pylance language server will index symbols in files opened during the session. So, if a 3rd party dependency file outside the workspace is opened, its symbols will be indexed and appear in Go to Symbol in Workspace (⌘T)

See also this issue: Provide a way to search for symbols in libraries #35

Answered By: tekumara

The latest Pylance Language Server (2022.9.20) has a bug that it fails to find source files if no [tool.pyright] section in pyproject.toml

I’ve found this issue.
https://github.com/microsoft/pylance-release/issues/3366

After adding [tool.pyright] section in pyproject.toml, ⌘T works well for me.

Answered By: Hoseung Kim