How Do I Attach a Debugger to My TestStand Sequence for Python?

Updated Oct 27, 2020

Environment

Software

  • TestStand

I would like to attach a debugger to Python code modules run using the Python Adapter. How can I do this?

Python steps do not support debugging Python code modules using a step-into functionality. To debug a code module, you can attach a Python debugger to the external process running the Python code. The debugger is attached using the process ID You can add the following code to your module to produce a dialog box with the process ID. 

For Python 2.7 you can use the following code:

import os 
import ctypes 

Ctypes.windll.user32.MessageBoxA(None, “Process name: niPythonHost.exe and Process ID: “  str(os.getpid()), “Attach debugger”, 0)

For Python 3.6 you can use the following code:

import os 
import ctypes 

ctypes.windll.user32.MessageBoxW(None, “Process name: niPythonHost.exe and Process ID: “  str(os.getpid()), “Attach debugger”, 0)

The import statements should reside at the top of the file, and the function call should be placed in the function that is being called from TestStand. If you are using Visual Studio, you can open the Python File and select Debug>>Attach to a Process to find the process listed in the popup menu. 

Additional Information