Using Wing with web2py
Wing is a Python IDE that can be used to develop, test, and debug Python code written for web2py, an open source web development framework. Two versions of Wing are appropriate for use with this document: Wing Pro is the full-featured Python IDE for professional programmers, and Wing Personal is a free alternative with reduced feature set.
If you do not already have Wing installed, download it now.
To get started using Wing, refer to the tutorial in the Help menu in Wing or the Quickstart Guide. <ref:howtos/quickstart>`__.
Wing allows you to debug Python code and templates running under web2py 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 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
Download and install web2py. On some OSes you can use the regular install, but at least on Windows you need to use the web2py sources instead because the regular install is missing modules necessary for debugging. When the sources are being used, you will also need to install Python if you don't already have it.
Then launch Wing and create a new project from the Project menu. Select web2py as your project type, and point the Python Executable at the Python executable (python or python.exe) used for web2py. Click OK and then save the project (for example, as web2py.wpr within the web2py directory).
Next add the web2py directory to your project by going to the Project view, right clicking, and selecting Add Directory. After the project view populates, find and right click on the file web2py.py and select Set As Main Debug File.
On Windows, if you are working from sources, you may also need to install pywin32
You can now debug web2py by clicking on the green Debug icon in Wing's toolbar and waiting 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.
An example is to set a breakpoint in applications/welcome/views/default/index.html, which is loaded when you go to the URL http://127.0.0.1:8000/welcome/default/index (assuming local web2py install running on port 8000).
Notice that breakpoints work both in Python code and HTML template files.
Wing's Debug Probe (in the Tools menu) is similar to running a shell from web2py (with python web2py.py -S myApp -M) but additionally includes your entire context and provides auto-completion. 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. 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.
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.
Exception Reporting in Old Web2Py Versions
This section is only relevant if you are using a very old web2py, before version 1.62 .
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 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.
Wing provides many other options and tools. For more information: