Debugging Python Scripts in Visual Studio Code
We assume an up-to-date Visual Studio Code (VS Code) intallation with the ms-python.python extension installed. Please read the official Python debug configurations in Visual Studio Code guide for a general overview.
Note the following points:
•This guide uses raptorxml-python as the command to execute RaptorXML Server as a Python interpreter.
•The raptorxml-python executable is available in the bin folder of your RaptorXML Server application folder.
Overview
We introduce two methods to use VS Code to debug Python scripts in RaptorXML Server.
•Method 1 also works for servers and RaptorXML Python callbacks (--script option).
•Method 2 doesn't require any source code modifications. It is a modified invocation of RaptorXML. Method 2 doesn't work for servers and RaptorXML Python callbacks (--script option).
•Both methods work with a standard Python interpreter and the imported RaptorXML Python module ('import altova_api.v2 as altova').
Method 1: Change your source code
Carry out the following steps:
1.Run: raptorxml-python -m pip install --upgrade debugpy
2.Add the following lines to your Python source code:
python
import debugpy
debugpy.listen(5678)
debugpy.wait_for_client()
debugpy.breakpoint()
3.Copy this launch configuration to VS Code launch.json (defaults will do for the above values) and select it for Run.
json5
{
"name": "Python: Remote Attach",
"type": "python",
"request": "attach",
"connect": {
"host": "localhost",
"port": 5678
},
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "."
}
]
}
You can also run by using the menu command Run->Add Configuration...->Python->Remote Attach with the defaults accepted.
4.Run your Python script (or RaptorXML with --script callbacks) as usual.
5.Start debugging (usually with the shortcut F5).
Method 2: Use a modified command line
Carry out the following steps:
1.Add a launch configuration (as in Method 1 above), and select it for Run.
2.Set a breakpoint in your Python script.
3.Run the command: raptorxml-python -m debugpy --listen 0.0.0.0:5678 --wait-for-client your-script-.py
4.Start debugging (usually with the shortcut F5).
Note: | Debugging also works with containers and remote servers. You have to change the host key of the connect entry in the launch configuration. You may also use other ports as long as code or command line and launch.json have consistent values. |
Setting raptorxml-python.exe as VS Code's default interpreter
It is possible to configure raptorxml-python.exe as the default Python interpreter of VS Code. Do this by adding the following to your VS Code settigs.json file:
json
"python.defaultInterpreterPath": "/path/to/raptorxml-python.exe"
...
In this case, it is also possible to use a "Current File" launch configuration that starts the script for debugging. Consult the official VS Code documentation for details.