在Visual Studio 2013中Debug Slicer Python脚本

来源:互联网 发布:mac磁盘工具找不到硬盘 编辑:程序博客网 时间:2024/05/22 14:14

1.安装PTVS

下载并安装PTVS:Python Tools for Visual Studio。
打开VS2013配置PTVS:
  • 工具->Python Tools->Python Environments。
  • 点击"+Custom"。
  • 在description处添加描述信息,在Prefix path处选择python-install目录,例如:D:\S\D\python-install,点击Auto Detect。
  • 点击Apply,并保证新环境处于顶部。

2.安装Remote Debug Server

为了调试Slicer中的Python实例,需要安装ptvsd,是PTVS的remote debugging server,在slicer.exe所在目录下打开命令行,例如:D:\S\D\Slicer-build,运行以下命令:
  • 安装pip:slicer.exe --launch "D:\S\D\python-install\Scripts\easy_install.exe" pip。
  • 安装ptvsd:slicer.exe --launch "D:\S\D\python-install\Scripts\pip.exe" install ptvsd。
ptvsd安装在D:\S\D\python-install\lib\site-packages目录下。

3. 配置Remote Debug Server

在Visual Studio中打开需要调试的Slicer scripted module,即相关的.py文件,在其中加入以下代码:
import ptvsdptvsd.enable_attach(secret='slicer')ptvsd.wait_for_attach()
保存之后在Visual Studio中启动Slicer进行调试,当运行到该模块的.py文件时,会启动一个remote debugging server,名为slicer,此时在Visual Studio中进行以下操作:
  • 点击调试->附加到进程。
  • 选择Python remote (ptvsd)作为传输。
  • 输入tcp://slicer@localhost作为限定符。
  • 点击刷新,slicer的进程会出现在列表中,选择该进程并点击Attach。
此时debugger attaches,可以在python代码中使用断点,单步调试代码,并查看相关变量,在运行C++代码时可以自动切换到C++的debug模式下,但是仍无法在C++的调用堆栈中查看python代码,只能在运行到python代码时设置断点调试。
参考资料:Debugging Python in Visual Studio

0 0