Developing Slicer modules in Visual Studio / Visual Studio Code?

I’ve created a script that collects all the above paths from the current Slicer instance and updates Visual Studio Code settings file automatically:

import os
import json

# Make sure all required packages are installed
slicer.util.pip_install("pylint rope autopep8 jedi")

binPath = slicer.app.applicationDirPath()  # 'C:/Users/(username)/AppData/Local/NA-MIC/Slicer (version)/bin'
appPath = os.path.dirname(slicer.app.applicationDirPath())  # 'C:/Users/(username)/AppData/Local/NA-MIC/Slicer (version)'
pythonHomePath = os.getenv('PYTHONHOME')  # 'C:/Users/(username)/AppData/Local/NA-MIC/Slicer (version)/lib/Python'
vsCodeUserSettingsFilePath = os.path.join(os.getenv("APPDATA"),"Code/User/settings.json")

# Read VS code settings file
with open(vsCodeUserSettingsFilePath) as vsCodeUserSettingsFile:
    vsCodeUserSettings = json.load(vsCodeUserSettingsFile)

# Update settings

vsCodeUserSettings["python.pythonPath"] = os.path.normpath(os.path.join(binPath, "SlicerPython.exe"))

vsCodeUserSettings["python.linting.pylintPath"] = os.path.normpath(os.path.join(pythonHomePath, "Scripts/pylint.exe"))
vsCodeUserSettings["python.linting.enabled"] = True

vsCodeUserSettings["python.formatting.autopep8Path"] = os.path.normpath(os.path.join(pythonHomePath, "Scripts/autopep8.exe"))

vsCodeUserSettings["python.autoComplete.extraPaths"].append(os.path.normpath(appPath))
vsCodeUserSettings["python.autoComplete.extraPaths"].append(os.path.normpath(binPath))
vsCodeUserSettings["python.autoComplete.extraPaths"].append(os.path.normpath(pythonHomePath))
vsCodeUserSettings["python.autoComplete.extraPaths"].append(os.path.normpath(os.path.join(pythonHomePath, "Scripts")))
vsCodeUserSettings["python.autoComplete.extraPaths"].append(os.path.normpath(os.path.join(pythonHomePath, "Lib")))
vsCodeUserSettings["python.autoComplete.extraPaths"].append(os.path.normpath(os.path.join(pythonHomePath, "Lib/site-packages")))
vsCodeUserSettings["python.autoComplete.extraPaths"].append(os.path.normpath(os.path.join(pythonHomePath, "Lib/site-packages/pylint")))
vsCodeUserSettings["python.autoComplete.extraPaths"].append(os.path.normpath(os.path.join(binPath, "Lib/site-packages/vtkmodules")))
vsCodeUserSettings["python.autoComplete.extraPaths"].append(os.path.normpath(os.path.join(appPath, "lib/QtPlugins")))
# Remove duplicates
vsCodeUserSettings["python.autoComplete.extraPaths"] = list(set(vsCodeUserSettings["python.autoComplete.extraPaths"]))

# Write VS code settings file
with open(vsCodeUserSettingsFilePath, "w") as vsCodeUserSettingsFile:
    json.dump(vsCodeUserSettings, vsCodeUserSettingsFile, indent=4)

Unfortunately, it does not do much for me. For example, if I type:

import vtkSegmentationCore as sc
s = sc.vtkSegmentation()

then methods of object s do not show up. If anybody can make VTK, MRML, CTK, or Qt method name auto-complete work in VS Code then please let me know.

4 Likes