Did you ever reach a point where your code looks like this:
print("################ here")
'''
bunch of code
'''
print("################ here2", value)
'''
bla bla lala
'''
if(something):
print("################ here3", another_value)
and you have to force your code to produce an error to make sour it reach that line?
As you might know odoo cli provide debug option see Command-line interface: odoo-bin
and you can debug now
yah...
you want something a little bit friendly
OK as you wish
let's start 🔥
⚠️ we gonna need to have vscode installed in our machine
and then install python plugin you can find it here
- First run this command in your terminal
pip3 install debugpy
it's an implementation of the Debug Adapter Protocol for Python check it!
- Then go to where you usually start your odoo server. It should be something like this.
python3 odoo-bin --addons-path=paths/to/your/addons
then change it to
python3 -m debugpy --listen 5678 odoo-bin --addons-path=paths/to/your/addons
5678 is just a port number,So chose whatever available port in your machine( this port is only used for debugging and is not related to odoo services )
- then open your addons folder in vscode (if you have more than one folder or you want to see your custom code alongside odoo standard addons put them all in a workspace see how!)
you will see some icons in the left sidebar choose the one that looks like this
click on create a configuration (launch.json) file
you should see something like this:
select the third option Remote Attach
you will be asked about hostname and port (just press enter)
vscode will open a new file called launch.json
replace the content of that file with
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Attach",
"type": "python",
"request": "attach",
"connect": {
"host": "localhost",
"port": 5678
}
}
]
}
if you have changed the port number in the running step change it here too.
- To start debugging press Start Debugging button as in:
During remote debugging, the debugging toolbar appears as below:
then you can add breakpoints where you want in your python files like this:
and 💥 everything is working now
TL;DR
check here
Top comments (10)
Do you know a practical way to do this but using Docker (Odoo official image)?
yes of course:
and then restart the container
after that go to launch.json and change the host and port according to what you set in the init file
But the IP of the container will change on every "recreation", isn't?
you are right
so you can expose the port at the run step
Sounds good, the only problem is that I'm used to have multiple independent projects (like 5 at the same time), and assign manually an IP for each of them doesn't feel right.
Anyway, thanks for your replies :)
if you exposed the port at run step and used launch.json for each project, you can use localhost but with different port in each project according to the configuration .
you are welcome :)
Nice Work
but Pycharm already has debug
what is the different?
Odoo doens't stop on breakpoints I put by vscode ui....I am using python 3.7.3...
How could I get it working?
Two things
I would still prefer a ipdb (pypi.org/project/ipdb/) and debug it from any line I want.