Documentation/4.6/Developers/Tutorials/Debugging Python in Visual Studio

From Slicer Wiki
Jump to: navigation, search
Home < Documentation < 4.6 < Developers < Tutorials < Debugging Python in Visual Studio


For the latest Slicer documentation, visit the read-the-docs.


Python Tools for Visual Studio

On Windows, Python Tools for Visual Studio (PTVS) enables debugging Python inside Visual Studio. Its remote debugging capability allows attaching the debugger to Slicer's embedded Python environment.

Setup and configuration

  • Download the PTVS installer from https://github.com/Microsoft/PTVS. Note that as of October 2015 the latest version requires Visual Studio 2013 or Visual Studio 2015. PTVS installs as an extension to Visual Studio.
  • Optional: configure Slicer's Python environment in Visual Studio. This enables IntelliSense specific to this Python environment.
    • Select View > Other Windows > Python Environments to show the Python Environments window.
    • Click "+ Custom"
    • Add a description, enter the Prefix path to the python-install directory in your build tree, like C:\D\S4D\python-install, then click Auto Detect. The remaining fields should be populated automatically.
    • Click Apply and make sure that the new environment is selected at the top of the window.

Install remote debugging server

To attach to a Slicer's Python instance it's first necessary to install ptvsd, the PTVS remote debugging server, in Slicer's Python environment:

  • Install pip if necessary: ./Slicer --launch "C:\D\S4D\python-install\Scripts\easy_install.exe" pip
  • Install ptvsd using pip: ./Slicer --launch "C:\D\S4D\python-install\Scripts\pip.exe" install ptvsd

Configure the remote debugging server

The PTVS page provides detailed remote debugging instructions. In brief, the steps are:

  • In Visual Studio, open the script—the Slicer scripted module—to debug.
  • Add the following code to the script to enable remote debugging:
import ptvsd
ptvsd.enable_attach(secret='slicer')
ptvsd.wait_for_attach()

Here, calling enable_attach() enables the remote debugging server. The secret parameter specifies a password that must be specified in Visual Studio when connecting to the debugger; it may be None.

Calling wait_for_attach() blocks Slicer until the debugger attaches.

Alternatively, this code could be added to ~/.slicerrc.py so that it's executed when Slicer starts.

Attach the debugger

  • Start Slicer.
  • In Visual Studio, select Debug > Attach to Process to display the Attach to Process window, then:
    • Choose Python remote (ptvsd) as the Transport.
    • Enter "tcp://slicer@localhost" as the Qualifier. Here, "slicer" should match the secret specified in enable_attach().
    • Press Enter or click Refresh. The Slicer process should appear in the list below. Select the Slicer process and click Attach.

Once the debugger attaches, you can use breakpoints, step through the code, and examine variables in the Watch window.

Notes

  • It might be necessary to configure Windows Firewall to allow the Slicer executable to listen on port 5678.
  • Debugging Slicer's C++ code and Python code simultaneously is possible by using separate instances of Visual Studio.