Tutorial: Debugging Code Not Launched by the IDE

Index of All Documentation » Wing Tutorial » Tutorial: Debugging »

So far we've been debugging code launched from inside of Wing. Wing can also debug processes that are running within a web framework, as scripts in a larger application, or that get launched from the command line. These are cases where a debug process cannot be launched from the IDE, so another method is needed to initiate debug.

Let's try this now with example2.py in your tutorial directory. First, copy wingdbstub.py out of install directory listed in Wing's About box. Place this in the same directory as example2.py. Next, click on the bug icon in the lower left of Wing's main window and select Accept Debug Connections. Then set a breakpoint on lines 10 and 22 of example2.py:


If you are working on OS X, using the Windows zip install, or using the Linux tar install of Wing, you will need to edit wingdbstub.py to set WINGHOME to the full path to the Wing installation directory (on OS X, this is the name of Wing's .app directory). This is done automatically by the regular Windows installer and Debian and RPM installs on Linux. If you are using one of those you can skip this step.

Now we're ready to debug example2.py when it is launched from outside of the IDE. To launch it, use the DOS Command prompt on Windows, a bash or similar command prompt on Linux, or Terminal or an xterm on OS X by typing:

python example2.py

You may need to specify the full path to python if it is not on your path.

This should start up the code, print some messages, connect to the IDE, and stop on the breakpoint on line 22. Read through the code and the messages printed to understand what is happening. You can verify that the debugger attached by looking at the color of the bug icon in the lower left of the IDE window, and by hovering the mouse over it:


Once you are stopped at a breakpoint or exception in externally launched code, the debugger works just as it would had you launched the debug process from the IDE. The only difference is that the environment is set up by the process itself and the settings specified in Project Properties and File Properties are not used.

When you continue the debugger from the toolbar or Debug menu, the program should print the value of x and exit.

This is a very simple example to illustrate how externally launched code can be debugged. The import of wingdbstub can also be placed in functions or methods, and there is a debugging API that provides control over starting and stopping debugging.

See Debugging Externally Launched Code for details and the How-To guides for information on setting this up with specific web frameworks, compositing and rendering tools, and other applications.

Remote Debugging

Using the same mechanism, but with some additional configuration, it is also possible to debug Python code launched on another machine. This is documented in Remote Debugging in the Wing manual.

Wing Pro also has the ability to work remotely through a secure SSH tunnel to a configured remote host. This supports all of Wing's features, so you can edit, search, debug, test, and manage remote code as if the remote files and directories were stored locally, and you can run Python Shell and OS Commands on the remote host. This is the preferred way to work with code on a remote host, although you may still need to use wingdbstub to initiate debug if your code cannot be launched from the IDE. For details, see Remote Hosts.