Developing Slicer modules in Visual Studio / Visual Studio Code?

I´m a newbie in module scripting and I should like to manage code with Visual Studio or Visual Studio Code instead of Notepad++.
How can I setup VS/VSC for this porpuse? Is there any tutorial o video about?

Thanks on advance

Slicer will open .py files with the editor you configured in you Windows system settings (file associations), but you can open the files in any editor. You can also do step-by-step debugging in Visual Studio Code, Visual Studio, PyCharm, Eclipse, LiClipse, etc..

I have Debug Connection made, but Slicer Python libraries are not recognited by Visual Studio Code. This is my real issue. How can I add them?

In VScode you need to configure your python module, just let VSCode know you want the one in Slicer, {"python.pythonpath"="/path/to/slicer/bin/PythonSlicer"} in your project configuration /project/folder/.vscode/settings.json. Thats it, now you need to install pylint, rope and autopep8 inside Slicer with normal slicer.util.pip_install("pylint") , now your vscode works natively in Slicer.

Thanks @Alex_Vergara for your reply.
It looks a not so easy procedure for newbies…
It should be nice to have any kind of stpe-by-step tutorial. I´ve been surfing Developer Documentation and I couldn´t find it.

I’ve executed slicer.util.pip_install("pylint rope autopep8") in Slicer’s Python console and set python.pythonpath in VS Code to c:\Users\andra\AppData\Local\NA-MIC\Slicer 4.11.0-2019-12-11\bin\PythonSlicer.exe. After this I restarted VS Code, opened a file, and entered:

import vtk
a=vtk.vtkImageData()
a.Get

At this point, I would expect pressing Ctrl-space would bring up Get… methods of vtkImageData, but only a few method names show up (that are used in the file).

@Alex_Vergara does auto-complete or documentation work for you for VTK and Slicer classes? Do you use Jedi as IntelliSense engine (python.jediEnabled)? Seeing a few screenshots and/or videos about what you have working in VS Code would be great.

Here you can see VSCode recognizes the python inside Slicer

My settings.json inside project .vscode folder
image

the vscode general settings.json

You need to add the extrapaths to your python, I know I have them in the wrong place, but I am being lazy to move them. anyways, you need the python.autocomplete.extrapaths added to your settings too.
As you can see ctrl+space already gives me all necesary hints

This is for Linux, right? What “settings.json” and “extrapaths” should I have to change in Windows?
Thanks

On Windows, I’ve tried to add the extra paths but nothing changed. It still does not recognize anything.

Ok, I have fixed my settings to be module specific, now they are as

and this worked for me in mac, linux and windows

This sounds very promising! Can you copy here your settings for Windows? (and if possible, not as screenshot, but as text) Thank you.

I was able to get auto completion within Visual Studio Code on Windows following these settings:

“C:\Users\JamesButler\AppData\Roaming\Code\User\settings.json”

{
    "python.pythonPath": "C:\\Program Files\\Slicer 4.11.0-2019-12-06\\bin\\SlicerPython",
    "python.autoComplete.extraPaths": [
        "C:\\Program Files\\Slicer 4.11.0-2019-12-06\\",
        "C:\\Program Files\\Slicer 4.11.0-2019-12-06\\lib\\Python",
        "C:\\Program Files\\Slicer 4.11.0-2019-12-06\\lib\\QtPlugins",
        "C:\\Program Files\\Slicer 4.11.0-2019-12-06\\lib\\Slicer-4.11",
        "C:\\Program Files\\Slicer 4.11.0-2019-12-06\\bin",
    ],
}

2 Likes

Thanks! Do you have auto-complete for VTK or Slicer MRML classes, too?

It appears to only be working for these pure python modules like util with the settings that I used.

In the image below you can see some of the auto-complete for the slicer module (there are more if I scrolled down in the pop-up). There is no mrmlScene so unfortunately no slicer.mrmlScene.XX auto-complete. Also missing items for vtk. Maybe a setting can be changed, but I was just using the reference from @Alex_Vergara.
autocomplete-vscode2 autocomplete-vscode3

1 Like

Ok. Now, I have libraries recognition and it´s really cool. I still have problems with pylint rope and autopep8. Python console throw this error.

image

Thank you all.

The package name is slicer.util (and not slicer.utils).

In windows project settings does not work, you need to edit global settings for vscode for it to work. Here it is my settings.json in C:\Users\alexv\AppData\Roaming\Code\User\settings.json

{
    "python.pythonPath": "C:\\Users\\alexv\\AppData\\Local\\NA-MIC\\Slicer 4.11.0-2019-11-05\\bin\\PythonSlicer.exe",
    "python.linting.pylintPath": "C:\\Users\\alexv\\AppData\\Local\\NA-MIC\\Slicer 4.11.0-2019-11-05\\lib\\Python\\Scripts\\pylint.exe",
    "python.formatting.autopep8Path": "C:\\Users\\alexv\\AppData\\Local\\NA-MIC\\Slicer 4.11.0-2019-11-05\\lib\\Python\\Scripts\\autopep8.exe",
    "python.autoComplete.extraPaths": [
        "C:\\Users\\alexv\\AppData\\Local\\NA-MIC\\Slicer 4.11.0-2019-11-05\\",
        "C:\\Users\\alexv\\AppData\\Local\\NA-MIC\\Slicer 4.11.0-2019-11-05\\bin\\",
        "C:\\Users\\alexv\\AppData\\Local\\NA-MIC\\Slicer 4.11.0-2019-11-05\\lib\\Python\\",
        "C:\\Users\\alexv\\AppData\\Local\\NA-MIC\\Slicer 4.11.0-2019-11-05\\lib\\QtPlugins\\",
        "C:\\Users\\alexv\\AppData\\Local\\NA-MIC\\Slicer 4.11.0-2019-11-05\\lib\\Slicer-4.11\\",
        "C:\\Users\\alexv\\AppData\\Local\\NA-MIC\\Slicer 4.11.0-2019-11-05\\lib\\Python\\Scripts\\",
    ],
    "python.linting.enabled": true,
    "git.autofetch": true
}

And you don’t need any python installed in your system, just python extension in vscode.

Thank you. With the above settings I get auto-complete for native python classes (slicer.util, etc). This is good! Even VTK and other C++ wrapped libraries show up and classes are listed, but unfortunately method names and method documentation does not show up and auto-complete does not work either.

Maybe VTK’s Python wrapping could be made more friendly to IDEs, but with this we’ll have to wait until we upgrade to latest VTK master early next year.

I think that is because vtk and ctk are located in a very different folder

E15TY:dosimetry4d alexvergaragil$ ls /Applications/Slicer.app/Contents/bin/Python/
SlicerWizard            ctk                     mrml.py                 qt                      slicer                  vtkAddon.py             vtkSegmentationCore.py  vtkmodules
__pycache__             freesurfer.py           mrmlDisplayableManager  sitkUtils.py            vtk.py                  vtkITK.py               vtkTeem.py

So adding it to autocomplete paths


and

My configuration file is now

{
    "python.pythonPath": "/Applications/Slicer.app/Contents/bin/SlicerPython",
    "python.autoComplete.extraPaths": [
        "/Applications/Slicer.app/Contents",
        "/Applications/Slicer.app/Contents/lib/Python",
        "/Applications/Slicer.app/Contents/lib/QtPlugins",
        "/Applications/Slicer.app/Contents/lib/Slicer-4.11",
        "/Applications/Slicer.app/Contents/bin/",
        "/Applications/Slicer.app/Contents/bin/Python/",
        "/Applications/Slicer.app/Contents/bin/Python/vtkmodules"
    ],
    "python.linting.pylintPath": "/Applications/Slicer.app/Contents/lib/Python/bin/pylint",
    "python.formatting.autopep8Path": "/Applications/Slicer.app/Contents/lib/Python/bin/autopep8",
    "python.linting.enabled": true,
    "git.autofetch": true
}

I still have problems trying to fix Visual Studio Code configuration for Windows.

Proposed steps:

1º - Open Slicer Python Interector and install pylint, rope and autopep8:
slicer.util.pip_install(“pylint rope autopep8”)

2º - Open Visual Studio Code settings.json (located in C:\Users"user"\AppData\Roaming\Code\User)

3º - Items to add:

{
“python.pythonpath”: “C:\Users\user\AppData\Local\NA-MIC\Slicer 4.11.0-2019-12-02\bin\SlicerPython.exe”,
“python.linting.pylintPath”: “C:\Users\user\AppData\Local\NA-MIC\Slicer 4.11.0-2019-12-02\lib\Python\Lib\site-packages\pylint\”,
“python.formatting.autopep8Path”: “C:\Users\user\AppData\Local\NA-MIC\Slicer 4.11.0-2019-12-02\lib\Python\Scripts\autopep8.exe”,
“python.autoComplete.extraPaths”: [
“C:\Users\user\AppData\Local\NA-MIC\Slicer 4.11.0-2019-12-02\”,
“C:\Users\user\AppData\Local\NA-MIC\Slicer 4.11.0-2019-12-02\lib\Python\”,
“C:\Users\user\AppData\Local\NA-MIC\Slicer 4.11.0-2019-12-02\lib\QtPlugins\”,
“C:\Users\user\AppData\Local\NA-MIC\Slicer 4.11.0-2019-12-02\lib\Slicer-4.11\”,
“C:\Users\user\AppData\Local\NA-MIC\Slicer 4.11.0-2019-12-02\bin\”,
“C:\Users\user\AppData\Local\NA-MIC\Slicer 4.11.0-2019-12-02\lib\Python\Lib\site-packages\”,
“C:\Users\user\AppData\Local\NA-MIC\Slicer 4.11.0-2019-12-02\lib\Python\Scripts\”,
],
“python.linting.enabled”: true,
“git.autofetch”: true
}

Now I have no errors in VSC but autocomplete is not working.

I´ve tried @Alex_Vergara code but VSCode throws error in import libraries and others…

I have no clear idea of which are the right directies of Slicer we need to add to extraPaths nor there is any difference between pythonpath “SlicerPython” or “PythonSlicer.exe”.

If this issue is fixed, muybe it should be added to documentation …

Thanks to all