<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://www.slicer.org/w/index.php?action=history&amp;feed=atom&amp;title=Documentation%2F4.5%2FDevelopers%2FTutorials%2FDebugging_Python_in_Visual_Studio</id>
	<title>Documentation/4.5/Developers/Tutorials/Debugging Python in Visual Studio - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://www.slicer.org/w/index.php?action=history&amp;feed=atom&amp;title=Documentation%2F4.5%2FDevelopers%2FTutorials%2FDebugging_Python_in_Visual_Studio"/>
	<link rel="alternate" type="text/html" href="https://www.slicer.org/w/index.php?title=Documentation/4.5/Developers/Tutorials/Debugging_Python_in_Visual_Studio&amp;action=history"/>
	<updated>2026-04-04T02:05:23Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.33.0</generator>
	<entry>
		<id>https://www.slicer.org/w/index.php?title=Documentation/4.5/Developers/Tutorials/Debugging_Python_in_Visual_Studio&amp;diff=43802&amp;oldid=prev</id>
		<title>UpdateBot: Nightly -&gt; 4.5</title>
		<link rel="alternate" type="text/html" href="https://www.slicer.org/w/index.php?title=Documentation/4.5/Developers/Tutorials/Debugging_Python_in_Visual_Studio&amp;diff=43802&amp;oldid=prev"/>
		<updated>2015-11-12T22:10:48Z</updated>

		<summary type="html">&lt;p&gt;Nightly -&amp;gt; 4.5&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&amp;lt;noinclude&amp;gt;{{documentation/versioncheck}}&amp;lt;/noinclude&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Python Tools for Visual Studio==&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
==Setup and configuration==&lt;br /&gt;
* 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.&lt;br /&gt;
* Optional: configure Slicer's Python environment in Visual Studio. This enables IntelliSense specific to this Python environment.&lt;br /&gt;
** Select ''View &amp;gt; Other Windows &amp;gt; Python Environments'' to show the Python Environments window.&lt;br /&gt;
** Click &amp;quot;''+ Custom''&amp;quot;&lt;br /&gt;
** Add a description, enter the ''Prefix path'' to the python-install directory in your build tree, like &amp;lt;tt&amp;gt;C:\D\S4D\python-install&amp;lt;/tt&amp;gt;, then click ''Auto Detect''. The remaining fields should be populated automatically.&lt;br /&gt;
** Click ''Apply'' and make sure that the new environment is selected at the top of the window.&lt;br /&gt;
&lt;br /&gt;
==Install remote debugging server==&lt;br /&gt;
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:&lt;br /&gt;
* Install pip if necessary: &amp;lt;tt&amp;gt;./Slicer --launch &amp;quot;C:\D\S4D\python-install\Scripts\easy_install.exe&amp;quot; pip&amp;lt;/tt&amp;gt;&lt;br /&gt;
* Install ptvsd using pip: &amp;lt;tt&amp;gt;./Slicer --launch &amp;quot;C:\D\S4D\python-install\Scripts\pip.exe&amp;quot; install ptvsd&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Configure the remote debugging server==&lt;br /&gt;
The PTVS page provides detailed [https://github.com/Microsoft/PTVS/wiki/Cross-Platform-Remote-Debugging remote debugging instructions]. In brief, the steps are:&lt;br /&gt;
* In Visual Studio, open the script—the Slicer scripted module—to debug.&lt;br /&gt;
* Add the following code to the script to enable remote debugging:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import ptvsd&lt;br /&gt;
ptvsd.enable_attach(secret='slicer')&lt;br /&gt;
ptvsd.wait_for_attach()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Here, calling &amp;lt;tt&amp;gt;enable_attach()&amp;lt;/tt&amp;gt; enables the remote debugging server. The &amp;lt;tt&amp;gt;secret&amp;lt;/tt&amp;gt; parameter specifies a password that must be specified in Visual Studio when connecting to the debugger; it may be &amp;lt;tt&amp;gt;None&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Calling &amp;lt;tt&amp;gt;wait_for_attach()&amp;lt;/tt&amp;gt; blocks Slicer until the debugger attaches.&lt;br /&gt;
&lt;br /&gt;
Alternatively, this code could be added to &amp;lt;tt&amp;gt;~/.slicerrc.py&amp;lt;/tt&amp;gt; so that it's executed when Slicer starts.&lt;br /&gt;
&lt;br /&gt;
==Attach the debugger==&lt;br /&gt;
* Start Slicer.&lt;br /&gt;
* In Visual Studio, select ''Debug &amp;gt; Attach to Process'' to display the ''Attach to Process'' window, then:&lt;br /&gt;
** Choose ''Python remote (ptvsd)'' as the ''Transport''.&lt;br /&gt;
** Enter &amp;quot;''tcp://slicer@localhost''&amp;quot; as the ''Qualifier''. Here, &amp;quot;''slicer''&amp;quot; should match the &amp;lt;tt&amp;gt;secret&amp;lt;/tt&amp;gt; specified in &amp;lt;tt&amp;gt;enable_attach()&amp;lt;/tt&amp;gt;.&lt;br /&gt;
** Press Enter or click ''Refresh''. The Slicer process should appear in the list below. Select the Slicer process and click ''Attach''.&lt;br /&gt;
&lt;br /&gt;
Once the debugger attaches, you can use breakpoints, step through the code, and examine variables in the Watch window.&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
* It might be necessary to configure Windows Firewall to allow the Slicer executable to listen on port 5678.&lt;br /&gt;
* Debugging Slicer's C++ code and Python code simultaneously is possible by using separate instances of Visual Studio.&lt;/div&gt;</summary>
		<author><name>UpdateBot</name></author>
		
	</entry>
</feed>