Home » Support » Index of All Documentation » Wing IDE Reference Manual » Advanced Debugging Topics »

Sometimes it is useful to debug Python code launched by an application produced by py2exe -- for example, to solve a problem only seen when the code has been packaged by``py2exe``, or so that users of the packaged application can debug Python scripts that they write for the app.

When py2exe produces the *.exe, it strips out all but the modules it thinks will be needed by the application and may miss any required by scripts added after the fact. Also, py2exe runs in a slightly modified environment (for example the PYTHONPATH environment is ignored). Both of these can cause problems for Wing's debugger, but can be worked around with some modifications to the packaged code, as illustrated in the following example:

# Add extra environment needed by Wing's debugger
import sys
import os
extra = os.environ.get('EXTRA_PYTHONPATH')
if extra:
    sys.path.extend(extra.split(os.pathsep))
print(sys.path)

# Start debugging
import wingdbstub

# Just some test code
print("Hello from py2exe")
print("frozen", repr(getattr(sys, "frozen", None)))
print("sys.path", sys.path)
print("sys.executable", sys.executable)
print("sys.prefix", sys.prefix)
print("sys.argv", sys.argv)

You will need to set the following environment variables before launching the packaged application:

EXTRA_PYTHONPATH=\Python25\Lib\site-packages\py2exe\samples\simple\dist;\Python25\lib;\Python25\dlls
WINGDB_EXITONFAILURE=1
WINGHOME=\Program Files\Wing IDE 4.1

To debug, an installation of Python matching the one used by py2exe must be present and referenced by the EXTRA_PYTHONPATH environment variable. This example assumes the installation of Python 2.5 at \Python25 was used by py2exe.

The directory \Python25\Lib\site-packages\py2exe\samples\simple\dist is where wingdbstub.py was placed; this can be altered as desired. Also, WINGHOME should be altered to match the location where Wing is installed and isn't needed at all if the value set in wingdbstub.py is correct (which it usually will be if copied out of the Wing installation).

When trying this out, be sure to Accept Debug Connections in Wing IDE by clicking on the bug icon in the lower left of the main window. For more information on using wingdbstub to debug, see Debugging Externally Launched Code

Enabling End Users to Debug

The above example is geared at the primary developers trying to find bugs in packaged code. If the packaged application is one that allows the end user to write add-on scripts and they want to debug these in Wing's debugger, then the import wingdbstub in the above example should be replaced with the following imports:

import socket
import select
import traceback
import struct
import cPickle
import site
import string

This forces py2exe to bundle the modules needed by Wing's debugger with the .exe, so that the end user can place include wingdbstub in their scripts instead.

Of course it's also possible to conditionally include the import wingdbstub in the main code, based on an environment variable or checking user settings in some other way. For example:

import os
if os.environ.has_key('USE_WING_DEBUGGER'):
  import wingdbstub

A combination of the above techniques can be used to craft debugging support appropriate to your particular py2exe packaged application.

The above was tested with py2exe run with -q and -b2 options.

« 13.8. Debugging Non-Python MainloopsTable of Contents13.10. Debugger Limitations »

Debugging Code Running Under Py2exe