How to remote debug Odoo using VSCode

With Python or other programming languages, debugging is an important step with almost all coders. When our code has been deployed to servers, it’s very hard to tracking the bug without some key point we left in code before. So, to resolve this problem, we will have a technique call remote debugging that allows us to debug the program even it’s not on our machine.

So, to do this, we have to make some preparations like below.

Step 1: Environment

  • Install VS Code (a free IDE from Microsoft)
  • Create a new virtual environments

    python3 -m venv

  • Install pip and ptvsd package:

    pip install ptvsd

  • Install git and pull our code to a place you want

Step 2: Configuration

  • Make a new debug configuration
    {
	"name": "DEMO",
	"type": "python",
	"request": "attach",
	"port": 5678, --> this is the communication port between server remote and our machine
	"host": <IP / hostname>,
	"pathMappings": [
		{
			"localRoot": <path to our code folder>,
			"remoteRoot": <path to our code folder on remote>
		}
	]
    }
  • Edit odoo-bin file in remote server. Add extra lines after import odoo and before main function
    import ptvsd

    # Allow other computers to attach to ptvsd at this IP address and port.
    ptvsd.enable_attach(address=('1.2.3.4', 5678), redirect_output=True)

    # Pause the program until a remote debugger is attached
    ptvsd.wait_for_attach()
  • Run the Odoo server with a bit little than normal

python -m ptvsd --host 1.2.3.4 --port 5678 --wait <path_to_odoo-bin> -c <path_to_odoo_config_file>

The program will hold and wait for our local VS Code to press debug button
The debug console with show exactly like remote servers

  • After that, just put breakpoint in our local code like we are in local machine

Have funny coding!

4 Likes

xịn vl a ơi. Hsau training onjob a nhề

1 Like

OK, cháu nào thích chui hết vào đây anh tiếp