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.
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
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.
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
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.
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 …
There is no difference. Originally, we added SlicerPython, but then we realized then PyCharm only accept interpreter name if it starts with “python”. We kept “SlicerPython” for backward compatibility but probably we’ll remove it at some point.
Note that autocomplete, breakpoints, step-by-step debugging, stack walking, running local commands, inspecting and modifying variables, etc. all already works perfectly if you attach Python debugger to a running Slicer and put a breakpoint into the method that you are editing. So, all that we are trying to achieve here would just offer some additional convenience.
C++ Tips: Using the CMake Tool by Microsoft really helped as it also provides the option to add generated build files to intellisense automatically (also need to have the c++ extension by microsoft installed). I’ve also attached my tasks.json and launch.json that launches slicer with my loadable c++ extension by pressing F5.
I open vscode in the base directory of my extension Make sure to change to your extensions name to launch correctly
launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(Linux) Launch Extension",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/SlicerWith<NAME OF EXTENSION>",
"args": [
"--attach-process"
],
"preLaunchTask": "(Linux) build",
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"launchCompleteCommand": "exec-run",
"linux": {
"MIMode": "gdb",
"miDebuggerPath": "/usr/bin/gdb"
},
"symbolLoadInfo": {
"loadAll": true,
"exceptionList": ""
},
"environment": [],
"externalConsole": true,
"additionalSOLibSearchPath": "${workspaceFolder}/build/lib/Slicer-4.11/qt-loadable-modules;<PATH TO SLICER BUILD>/Slicer-build/lib/Slicer-4.11/qt-loadable-modules"
}
]
}
Make sure to change all instances with angle brackets inside to match your computers settings.
I’m still trying to figure out how to debug cpp with vscode instead of having to use gdb. As of now, you will have to open gdb separately in a terminal after starting the above code (basically running your extension ./SlicerWith(Extension Name) --atach-process) to get your process id. Then in a new terminal:
gdb -p <process_id>
(gdb) # place breakpoints
(gdb) continue # to run
it still doesn’t work. Do I need to instal slicer using conda for this to work?
python.pythonPath, python.linting.pylinPath and some other keywords in vscode settings.json are labelled as “Unknown configuration setting”, so that’s why it probably does not work. Any help with this?