DEV Community

Alfadil mustafa
Alfadil mustafa

Posted on • Updated on

Clean Odoo debugging

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)

Enter fullscreen mode Exit fullscreen mode

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
Alt Text

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
Enter fullscreen mode Exit fullscreen mode
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
Enter fullscreen mode Exit fullscreen mode

then change it to

python3 -m debugpy --listen 5678 odoo-bin --addons-path=paths/to/your/addons
Enter fullscreen mode Exit fullscreen mode
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

debug-icon

click on create a configuration (launch.json) file

you should see something like this:
debug-icon

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
            }
        }
    ]
}
Enter fullscreen mode Exit fullscreen mode
if you have changed the port number in the running step change it here too.
  • To start debugging press Start Debugging button as in: debug-icon

During remote debugging, the debugging toolbar appears as below:
debug-icon

then you can add breakpoints where you want in your python files like this:
debug-icon

and đź’Ą everything is working now

TL;DR
check here

Top comments (10)

Collapse
 
afromonkey profile image
Moisés Alejandro Navarro Presas

Do you know a practical way to do this but using Docker (Odoo official image)?

Collapse
 
alfadil profile image
Alfadil mustafa

yes of course:

  1. you must know how to run odoo image .
  2. go to odoo.com/page/download, And download the odoo source (for the version you want)
  3. in the download directory go to addons/init.py and add
try:
    import ptvsd
    ptvsd.enable_attach(address=('0.0.0.0', 3000))
except:
    pass
Enter fullscreen mode Exit fullscreen mode
  1. run the docker image with the source you just downloaded as a volume like :
-v path/to/downloaded/source/:/usr/lib/python3/dist-packages/odoo
Enter fullscreen mode Exit fullscreen mode
  1. after that run
docker exec -it container_name pip3 install ptvsd
Enter fullscreen mode Exit fullscreen mode

and then restart the container

docker restart container_name
Enter fullscreen mode Exit fullscreen mode

after that go to launch.json and change the host and port according to what you set in the init file

Collapse
 
afromonkey profile image
Moisés Alejandro Navarro Presas

But the IP of the container will change on every "recreation", isn't?

Thread Thread
 
alfadil profile image
Alfadil mustafa

you are right
so you can expose the port at the run step

Thread Thread
 
afromonkey profile image
Moisés Alejandro Navarro Presas

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 :)

Thread Thread
 
alfadil profile image
Alfadil mustafa

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 :)

Collapse
 
ahefni profile image
ahefni

Nice Work
but Pycharm already has debug
what is the different?

Collapse
 
sz23 profile image
Lorenzo

Odoo doens't stop on breakpoints I put by vscode ui....I am using python 3.7.3...

How could I get it working?

Collapse
 
alfadil profile image
Alfadil mustafa

Two things

  1. Did you start debugging in vs code?
  2. Did you have multi worker setup in odoo config? if so you need to stop using workers cause the debugging process would work.
Collapse
 
bhavsar667 profile image
Akash Bhavsar

I would still prefer a ipdb (pypi.org/project/ipdb/) and debug it from any line I want.