Code autocompletion on VSCode fails

Problem:
I’m trying to use VSCode to develop a Slicer extension. This is my first time using Slicer. I am trying to set up my development environment such that the code has typical autocomplete functions. I am using Ubuntu 22.04.1 LTS and VSCode version 1.74.0 5235c6bb189b60b01b1f49062f4ffa42384f8c91 x64. My Slicer version is 5.2.1-linux-amd64

Similar Topics:
Here are the other topics I’ve looked at, but neither solved my issue.

Developing Slicer modules in Visual Studio / Visual Studio Code? (slightly out of date as pythonPath is not part of the settings.json file anymore).

What I’ve tried:
I’ve selected the Python contained in Slicer.
image

I then went into the integrated python console of the slicer gui and ran:
slicer.util.pip_install(‘pylint rope autopep8’)

My workspace settings.json file in .vscode is as follows. I have verified that this file is in the correct location.

{
    "python.autoComplete.extraPaths": [
        "/opt/Slicer-5.2.1-linux-amd64/",
        "/opt/Slicer-5.2.1-linux-amd64/lib/",
        "/opt/Slicer-5.2.1-linux-amd64/lib/Python",
        "/opt/Slicer-5.2.1-linux-amd64/lib/QtPlugins",
        "/opt/Slicer-5.2.1-linux-amd64/lib/Slicer-5.2",
        "/opt/Slicer-5.2.1-linux-amd64/bin",
    ],
    "python.linting.pylintPath": "/opt/Slicer-5.2.1-linux-amd64/lib/Python/bin/pylint",
    "python.analysis.extraPaths": [
        "/opt/Slicer-5.2.1-linux-amd64/",
        "/opt/Slicer-5.2.1-linux-amd64/lib/",
        "/opt/Slicer-5.2.1-linux-amd64/lib/Python",
        "/opt/Slicer-5.2.1-linux-amd64/lib/QtPlugins",
        "/opt/Slicer-5.2.1-linux-amd64/lib/Slicer-5.2",
        "/opt/Slicer-5.2.1-linux-amd64/bin"
    ],
    "python.linting.enabled": true,
    "python.formatting.autopep8Path": "/opt/Slicer-5.2.1-linux-amd64/lib/Python/bin/autopep8",
}

After doing this, I would expect to be able to have VSCode autocomplete/provide suggestions on modules etc. However:
image

I do have the suggestions when importing however, suggesting vscode is indeed pointing to the correct Python launcher:
image

I have also tried copying the above settings.json file to the user settings.json file, but to no avail.

Has anyone gotten further than this? I have followed the same path of trying to get this to work and gotten stuck at the same place… slicer. lists a few things as autocomplete options, but not things like slicer.util . However, I can do from slicer import and get util as a suggestion, and then I can get many util. method autocompletion settings. However, if I do slicer.util. I get no autocompletion options. I don’t understand this… shouldn’t slicer.util. be the same as util. after from slicer import util? I’m not sure why one successfully autocompletes but the other doesn’t.

I understand from a previous thread that wrapped C++ code might be a problem as well as things which might be more dynamically constructed like mrmlScene, but I’d love to get autocompletion working at least for all the purely python and static components like util. Any help out there? I did try adding "C:\\Users\\mikeb\\AppData\\Local\\NA-MIC\\Slicer 5.2.1\\bin\\Python\\slicer" (the home of util.py) to the VSCode settings.json list of extra python.autoComplete.extraPaths, but it didn’t seem to help.

@jcfr there are some Slicer pull requests with related changes. Could @mikebind test/work ok those?

To get an idea of the remaining work, see Improve auto-completion support · Issue #6690 · Slicer/Slicer · GitHub.

1 Like

Thanks for the link @jcfr. I would be happy to try to test things on my system. Unfortunately, looking through the changes and remaining work, I doubt I would be capable of contributing in a meaningful way to helping move things forward. While I would consider myself fairly comfortable developing scripted python modules in Slicer, at this point, the build system and even the C++ level of Slicer code are generally hard for me to follow. I can follow directions and be a naive user checking if PR’s work on my system though!

1 Like