Clean Odoo debugging
Tags: odoo, debugging, vscode
Published: 2020-05-15 12:21:09

Did you ever reach a point where your code looks like this:
```python
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](https://www.odoo.com/documentation/13.0/reference/cmdline.html)
and you can debug now 
![Alt Text](https://media.giphy.com/media/xT3i0V9CFe3YP3juV2/giphy.gif)

yah...
you want something a little bit friendly   
OK as you wish 
####let's start :fire:

:warning: we gonna need to have [vscode](https://code.visualstudio.com/Download) installed in our machine
and then install python plugin you can find it [here](https://marketplace.visualstudio.com/items?itemName=ms-python.python)
- First run this command in your terminal
```bash
pip3 install debugpy
```
######it's an implementation of the Debug Adapter Protocol for Python [check it!](https://github.com/microsoft/debugpy/)

- Then go to where you usually start your odoo server. It should be something like this.
 ```bash
python3 odoo-bin --addons-path=paths/to/your/addons
```
then change it to 
 ```bash
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!](https://code.visualstudio.com/docs/editor/multi-root-workspaces))

you will see some icons in the left sidebar choose the one that looks like this 

![debug-icon](https://code.visualstudio.com/assets/docs/python/debugging/debug-start.png "debug-icon")

#####*click on create a configuration (launch.json) file*
you should see something like this:
![debug-icon](https://code.visualstudio.com/assets/docs/python/debugging/debug-configurations.png "python debug menu")

#####*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
 ```json
{
    "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:
![debug-icon](https://code.visualstudio.com/assets/docs/python/debugging/debug-start-button.png "start debug")

During remote debugging, the debugging toolbar appears as below:
![debug-icon](https://code.visualstudio.com/assets/docs/python/debugging/remote-debug-toolbar.png "debug toolbar")


then you can add breakpoints where you want in your python files like this:
![debug-icon](https://code.visualstudio.com/assets/docs/editor/debugging/bpts-in-overview.png "debug breakpoints")

and :boom: everything is working now

TL;DR
[check here](https://code.visualstudio.com/docs/python/debugging)