Debugging Python on Cloud9
By Brady Dowling • 23 March 2017
The following is a guest post from Cyril Morisse. We are always interested in guest posts on the Cloud9 blog about experiences, successes, contributions, and anything related to Cloud9. Thank you, Cyril!
It all started as a coding challenge
In June 2016 I was, once again, struggling with a potential customer trying to setup an Odoo Python 2 development environment on a Windows PC.
As I failed at convincing him to use either a Linux PC or a Mac, I decided to take a tour of the available online IDEs. A few hours later, I discovered several gems that Cloud9 has to offer:
- Live coding collaboration in the cloud
- Built-in Linux support
- Freedom to work from any computer
- Handy keyboard shortcuts
- And the awesome SSH workspaces
But there was no Python debugger.
As some of my Audaxis coworkers needed to renew their hardware, I tried to convince them to switch to Cloud9 using PuDB as their debugger but they weren’t having it.
I spent a weekend studying the SDK Documentation and planning a potential implementation of a Python debugger. I quickly realized that debugging support is not integrated directly into the Python language but as external modules. While those were great, they were hard to integrate or understand so I decided to start with a fork of pdb. Ultimately that turned into a complete rewrite a few weeks later: IKPdb.
IKPdb supports most of Cloud9 standard debugging features including:
- Breakpoints and conditional breakpoints
- Multithreaded code debugging
- Variables inspections and hot modifications
- Call stack inspection
- Interactive mode
For people working on "big" python projects (for example, Odoo) IKPdb has an interesting feature named Turbo Mode.
The underlying idea of Turbo Mode is that as soon as there is no active breakpoint, IKPdb removes the tracing system in all threads so the program runs speedily, as if it were launched without debugger support. In Turbo Mode, the debugger is dormant but will activate as soon as an unmanaged exception occurs to allow the call stack and variable inspection to be reactivated.
Note: The debugger exposes the same
post_mortem() API as pdb which allows it to activate an exception handler. See Integration Guide in the debugger documentation.
- Create a new Cloud9 workspace using the Python template and open it.
- Install the debugger by running this command in the terminal tab:
sudo pip install ikpdb
- Open the file app.py in the folder
- Right-click the file, and then choose Run.
- Cloud9 will create a run configuration, launch the program and generate an error. This is because the program was run from the wrong folder. Cloud9 tries to run the program from the workspace root. It is supposed to be run from the
ex50 folder instead. Let's fix this:
- Choose Stop.
- Choose CWD in the runner tab.
- Choose the
- Choose Select.
- Now activate the debugger by clicking the bug icon on the right-hand side of the run panel. Then click on the Run button on the left hand side as shown on this picture:
- Refresh the preview window and you should get
I just want to say Hello World. The program is now running in debug mode.
- Click on the
app.py tab, add a breakpoint at line 14 by clicking in the left gutter (at the left of the line number). A red dot should appear.
- Go back to the preview window, click on the refresh icon.
- The debugger should stop on line 14 and open the Debugger panel as shown below.
Now you can:
- Inspect the call stack. Note that you can't open files that are not in the workspace.
- Inspect local variables
- To try this, replace
Hello World with
Hello Cloud9, then resume execution and check the result in the preview tab.
- Make immediate changes
- To try this, while stopped on the breakpoint, click on Immediate in the debugger panel. The Immediate tab opens. There you can enter Python expressions or statements that are run in the context of the program breakpoint.
See https://docs.c9.io/docs/debugging-your-code to discover available debugger features.
Debug programs using virtualenvs
To debug a program running in a virtualenv, you must:
- Install ikpdb in your virtualenv
- Create a run configuration and a Cloud9 runner that references the Python interpreter in your virtualenv.
All of this is explained further in the IKPdb User Guide for Cloud9.
After I switched to Cloud9 as my main development environment, my coworkers followed my lead a few weeks later. I then tagged the debugger and Cloud9 plugin as 1.0 and started to migrate most of our customers.
As of the beginning of 2017, the debugger is now available in all Cloud9 workspaces and I am convinced that Cloud9 is not simply a feature rich online IDE. It is an open and hackable development platform.
Cyril works as a consultant for Audaxis where he deploys ERPs. He is a programmer passionate about web technologies and their application to business applications. When he does not code, Cyril enjoys spending time with his family.