Difference between revisions of "Documentation/Nightly/Developers/Tutorials/Debugging Python in Visual Studio"

From Slicer Wiki
Jump to: navigation, search
m (Add header)
(moved to github)
Tags: 2017 source edit, Replaced
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
<noinclude>{{documentation/versioncheck}}</noinclude>
+
{{documentation/banner
 
+
| text  = [https://github.com/SlicerRt/SlicerDebuggingTools This page has been moved to Github.]
==Python Tools for Visual Studio==
+
| background-color = 8FBC8F }}
On Windows, [https://github.com/Microsoft/PTVS 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 <tt>C:\D\S4D\python-install</tt>, 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 [https://pypi.python.org/pypi/ptvsd ptvsd], the PTVS remote debugging server, in Slicer's Python environment:
 
* Install pip if necessary: <tt>./Slicer --launch "C:\D\S4D\python-install\Scripts\easy_install.exe" pip</tt>
 
* Install ptvsd using pip: <tt>./Slicer --launch "C:\D\S4D\python-install\Scripts\pip.exe" install ptvsd</tt>
 
 
 
==Configure the remote debugging server==
 
The PTVS page provides detailed [https://github.com/Microsoft/PTVS/wiki/Cross-Platform-Remote-Debugging 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:
 
<pre>
 
import ptvsd
 
ptvsd.enable_attach(secret='slicer')
 
ptvsd.wait_for_attach()
 
</pre>
 
Here, calling <tt>enable_attach()</tt> enables the remote debugging server. The <tt>secret</tt> parameter specifies a password that must be specified in Visual Studio when connecting to the debugger; it may be <tt>None</tt>.
 
 
 
Calling <tt>wait_for_attach()</tt> blocks Slicer until the debugger attaches.
 
 
 
Alternatively, this code could be added to <tt>~/.slicerrc.py</tt> 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 <tt>secret</tt> specified in <tt>enable_attach()</tt>.
 
** 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.
 

Latest revision as of 03:39, 14 April 2021

Home < Documentation < Nightly < Developers < Tutorials < Debugging Python in Visual Studio