Python debugger
pudb, the classic
Install
sudo apt install python3-pudb
or
sudo pip3 install pudb
Trigger
import pudb
pudb.set_trace()
or, even shorter
import pudb;pu.db
Example
odoo/addons/base/models/res_partner.py
def copy(self, default=None):
import pudb; pu.db
self.ensure_one()
chosen_name = default.get('name') if default else ''
new_name = chosen_name or _('%s (copy)') % self.name
default = dict(default or {}, name=new_name)
return super(Partner, self).copy(default)
Then, either:
- start an Odoo server, open your browser, log in and click the Duplicate button of a contact. Then come back in your terminal where you started the server.
- launch a shell (with
-d dbname) and call the method:./odoo-bin shell -d masterenv['res.partner'].search([], limit=1).copy()
Controls
?: open the help. The most used commands are the ones explaiend in the first section of the help.CTRL+p: open the configurationCTRL+x: focus the integrated command lineSHIFT+v: focus the variables panelSHIFT+s: focus the callstack panelSHIFT+b: focus the breakpoints panel!: Open a Python shell maximizedCTRL+l: re-render, in case you have logs broking the display. If you are sick of the logs, redirect the logs to a file using--logfilewhen starting odoo.
It is highly recommended to use the theme agr-256 to please Fabien Meghazzi (fme). He is the theme's author. Each time you choose monokai-256 instead, you stab him it in the back.
Panels:
- Main window: the code
- Variables: local variables. You can add new ones with
nonce this view focused. - Stack: the call stack. You can jump from one call to another using the arrows once this view focused.
- Breakpoints: edit breakpoints. To add a breakpoint, hit
bin the line you want in the main window. Then, you can configure conditional breakpoints in this panel, or disable/remove the breakpoints. - Command line: execute arbitrary commands, using the local available variables.
CTRL+pto browse back the history. When there is a lot of ouput to display, you would rather use the maximized command line using!.
Visual Studio Code integrated debugger, the modern
Install
Install the Visual Studio Code Python package
Install ptvsd:
sudo pip3 install ptvsd
Trigger
Remote attach
Start Odoo in a terminal using ptvsd
python3 -m ptvsd --host localhost odoo/odoo-bin --addons-path=odoo/addons,enterprise
The path to start your Odoo server (odoo/odoo-bin in the above example) must match the path in Visual Studio code. Or you will have to use an advanced configuration, pathMappings.
Click in the gutter where you want to add a breakpoint
And then launch the debugger. Either:
F5- In the 🪲 debug panel, hit the
Start debuggingbutton
Then,
- Choose Remote Attach,
- Leave inputs by default: localhost, 5678
Then,
- open Odoo in your web browser,
- click on the view or button that will trigger the breakpoint.
If you need Odoo to wait you attached VSCode's debugger,
for example if you run a unit test and set a breakpoint in the unit test, you can add --wait to ptvsd.
python3 -m ptvsd --wait --host localhost odoo/odoo-bin --addons-path=odoo/addons,enterprise -d master --test-file=odoo/odoo/addons/base/tests/test_acl.py
Launch Odoo from VScode
In the 🪲 debug panel, hit the "create a launch.json file"
and then add the below configuration. Adapt it to your installation folders.
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Odoo",
"type": "python",
"request": "launch",
"stopOnEntry": false,
"program": "${workspaceFolder}/odoo/odoo-bin",
"args": ["--addons-path=${workspaceFolder}/odoo/addons,${workspaceFolder}/enterprise"]
},
]
}
Click in the gutter where you want your breakpoint
And then launch the debugger. Either:
- F5
- in the 🪲 debug panel, hit the
Start debuggingbutton
Controls
In the top of the window, you have the buttons to continue, step in, step out, ... For each, there is a keyboard shortcut that you can see by hovering your mouse cursor on them.
On the left, you have different panels. This is very similar to PuDB:
- Variables: the local variables
- Watch: add new variables, to see their content,
- Call Stack: jump in the different calls of the call stack
- Breakpoints: current breakpoints, add new ones, and edit them to have conditional breakpoint.
