Wing IDE is an integrated development environment that can be used to write, test, and debug Python code that is written for web2py, an open source web development framework. Wing provides auto-completion, call tips, a powerful debugger, and many other features that help you write, navigate, and understand Python code.
To get started using Wing, refer to the tutorial in the Help menu in Wing and/or the Wing IDE Quickstart Guide.
Wing IDE allows you to graphically debug a web2py application as you interact with it from your web browser. Breakpoints set in your code from the IDE will be reached, allowing inspection of your running code's local and global variables with Wing's various debugging tools. In addition, in Wing IDE Pro, the Debug Probe tab allows you to interactively execute methods on objects and get values of variables that are available in the context of the running web app.
There is more than one way to do this, but in this document we focus on an "in process" method where the web2py server is run from within Wing as opposed to attaching to a remote process.
Setting up a Project
In order to debug web2py code, you need the web2py sources. Download and unpack these, then launch Wing and then create a new project from the Project menu. Add the web2py directory and go to the Project view by right clicking and selecting Add Directory. Then right click on the file web2py.py in the project view and select Set As Main Debug File.
On Windows you will also need to:
- Install pywin32
- Use Python version 2.5 instead of Python 2.6 or greater (as of Dec 2009)
Now you can debug web2py by launching it from Wing. Just click on the green Debug icon in the toolbar and wait for the web2py console to appear. Enter a password and start the server as usual.
Once web2py is running, open a file in Wing that you know will be reached when you load a page of your web2py application in your web browser. Place a breakpoint in the code and load the page in your web browser. Wing should stop at the breakpoint. Use the Stack Data tool or Debug Probe (in Wing Pro) to look around.
Notice that the Debug Probe is similar to running a shell from web2py (with python web2py.py -S myApp -M) but additionally includes your entire context. You can easily inspect or modify variables, manually make function calls, and continue debugging from your current context.
Setting Run Arguments
When you start debugging, Wing will show the File Properties for web2py.py. This includes a Run Arguments field under the Debug tab where you can add any web2py option. For example, adding -a '<recycle>' will give you somewhat faster web2py startup since it avoids showing the Tk dialogs and automatically opening a browser window. This is handy once you already have a target page in your browser. Run python web2py.py --help for a list of all the available options.
To avoid seeing the File Properties dialog each time you debug, un-check the "Show this dialog before each run" check box. You can access it subsequently with the Current File Properties item in the Source menu or by right clicking on the editor and selecting Properties.
Hung Cron Processes
Web2py may spawn cron sub-processes that fail to terminate on some OSes when web2py is debugged from Wing IDE. This can lead to unresponsiveness of the debug process until those sub-processes are killed. To avoid this, add the parameter -N to prevent the cron processes from being spawned.
Skip this section if you are using web2py version 1.62 or newer.
As shipped, web2py version 1.61 and earlier contain a catch-all exception handler to report unexpected errors in your web browser as tickets. This is useful when tracking problems on a live site.
To make debugging more convenient, change the except Exception, exception clause in the definition of restricted at the end of the file src/gluon/restricted.py in your web2py installation to read as follows:
except Exception, exception: # XXX Show exception in Wing IDE if running in debugger if __debug__ and 'WINGDB_ACTIVE' in os.environ: etype, evalue, tb = sys.exc_info() sys.excepthook(etype, evalue, tb) raise RestrictedError(layer, code, '', environment)
Now you will get exceptions reported in Wing's Exceptions tool and can conveniently move up or down the stack and inspect the program state at the time of the exception.
Better Static Auto-completion
Working in your code when the debugger is not runnng by default misses some auto-completion options because of how web2py works. For example, auto-completion after typing db. will fail because db is not explicitly defined. To fix this, you can add some hints for Wing as follows at the top of the file:
# XXX This makes auto-completion work; also need to alter Python Path # XXX in project properties. if 0: import db
Then go into Project properties in the Project menu and add the following path under Python Path:
Replace /path/to according to where you unpacked web2py. This path may vary depending on which app you are working with.
Now, typing db. should bring up an auto-completer with the contents of db even if the debugger is not running.
Wing IDE provides many other options and tools. For more information:
|« 2.5. Using Wing IDE with Turbogears||Table of Contents||2.7. Using Wing IDE with mod_wsgi »|