Python debugging in Visual Studio Code

Visual Studio Code is an awesome source code editor and IDE - free, multi-platform (Windows, Linux, and Mac), and highly extensible.

Support has been added in recent versions of Slicer to connect from Visual Studio Code to Slicer’s embedded Python interpreter and add breakpoints, inspect variables, run the code step-by-step, etc. See more information and setup instructions here.

4 Likes

Has anyone been able to get this work with Slicer 4.8.1 and the current VSCode? I am not able to. I follow the steps exactly and Slicer just keeps hanging, vscode starts and then stops the debugger and gives no output.

One thing that’s different is that I get a warning in vscode in the launch.json file, it says, “Property secret is not allowed”. Am I supposed to somehow specify a different Python interpreter or something?

There doesn’t seem to be any online help for slicer/vscode, and I don’t want to have to install Eclipse just for this. Thanks!

Slicee-4.8 is more than a year old! Latest VS Code uses new version of Python debugger libraries that are incompatible with old versions.

You need to use at least Slicer-4.10 with recent VS Code releases.

Hi everyone,

I am updating this thread. I am trying to use VSCode 1.48 (last version) and Slicer-4.11. I have followed
https://www.slicer.org/wiki/Documentation/Nightly/Extensions/DebuggingTools#Debugging_in_VisualStudio_Code. Except some minor changes in VSCode I was able to follow through all the steps. However when I started the python debugging server on Slicer side, and started the debugging session in VSCode nothing seemed to happen.

I am trying to debug this code just for the test:

markupsNode = getNode(‘plan’)
normal = [ 0, 0, 0 ]
markupsNode.GetNormal(normal)

Do I miss something when debugging a python code that does not start at Slicer startup. Is it possible to do such thing ?

Thanks in advance for your help!
Pierre

You can do step-by-step debugging by attaching to Slicer’s Python environment from the debugger and putting a breakpoint into your code. If you want to step through your code then probably you need to create a skeleton module using Extension wizard and copy-paste the code there. It also allows you to relead and run the code (using Reload and Reload&test buttons in the module GUI).

Thanks, any experience with last version of VSCode and recent Slicer?
I’ll try to investigate the issue, it may be a config issue (eventhough there is not much to configure ^^ ).
I’ll let you know if I can resolve this.

I got a problem to attach. I use vscode 1.49 and slicer 4.11. I follow the instruction to attach but when I click “start debugging”, it seems success, because the slicer dialog disappear. But nothing happens in vscode, no variables showed in vscode. Could anybody give any suggestions? Is this the problem about vscode version?

图像 1

That’s very confusing. Even when I change the vscode version to 1.29 or 1.35, it’s still the same. Really need some advice! Thanks.

DebuggingTools extension documentation describes how to attach a debugger to a running Slicer application instance. In this case, you don’t run a script but you put breakpoint(s) into the code and execution is paused when that line is reached. A that point you can open the console, step through the code, and run commands there.

If you want to run script in Slicer’s Python environment then I would recommend to use Jupyter notebooks, using SlicerJupyter extension.

You may be able to execute some simplified scripts in Slicer’s Python environment, without starting the application, but this would be limited to using libraries, such as MRML, vtkITK, and vtkSegmentationCore, and some module logics. It might be interesting to see how Slicer can be used like this (essentially, as a set of Python libraries), but since attaching a debugger and using Jupyter notebooks seems to cover needs of most people, we don’t have plans to work on this in the near future.

1 Like