|
| 1 | +# Debug |
| 2 | + |
| 3 | +How to debug IoP code. |
| 4 | + |
| 5 | +Before anything, I will put some context about IoP and IRIS. |
| 6 | + |
| 7 | +Next I will show how to debug the code. |
| 8 | + |
| 9 | +Finally, we will give some tips about debugging. |
| 10 | + |
| 11 | +## Context |
| 12 | + |
| 13 | +IoP is a Python library that helps to build interoperability solutions with IRIS. |
| 14 | + |
| 15 | +IoP is based on [Embedded Python in IRIS](https://docs.intersystems.com/iris20243/csp/docbook/DocBook.UI.Page.cls?KEY=AFL_epython). |
| 16 | + |
| 17 | +Embedded Python is a feature of IRIS that allows you to run Python code inside IRIS. |
| 18 | + |
| 19 | +That means the python code is not running by a python interpreter, but by the IRIS process. |
| 20 | + |
| 21 | +That comes with some limitations, like the lack of a debugger. It's planned to have a debugger in the future. eg: IRIS 2025.2+. |
| 22 | + |
| 23 | +Meanwhile, we **have** some ways to debug the code. |
| 24 | + |
| 25 | +## Debugging |
| 26 | + |
| 27 | +Today (2025.2-), the best way to debug the code is to use an native python interpreter and run the code outside IRIS. This way you can use the python debugger. |
| 28 | + |
| 29 | +The main issue with this approach is that you must have a local instance of IRIS to be able to run the code. |
| 30 | + |
| 31 | +When I mean a local instance, it can be a local installation of IRIS, or a docker container running IRIS. |
| 32 | + |
| 33 | +I will explain first with a local installation of IRIS. Then I will show how to do the same with a docker container. |
| 34 | + |
| 35 | +### Local Installation |
| 36 | + |
| 37 | +To debug the code with a local installation of IRIS, you need to have the following: |
| 38 | + |
| 39 | +- A local installation of IRIS |
| 40 | + - https://docs.intersystems.com/irislatest/csp/docbook/Doc.View.cls?KEY=PAGE_deployment_install |
| 41 | +- A python interpreter compatible with the version of IRIS |
| 42 | + - https://docs.intersystems.com/iris20243/csp/docbook/Doc.View.cls?KEY=GEPYTHON_prereqs#GEPYTHON_prereqs_version |
| 43 | +- Iris embedded python wrapper |
| 44 | + - https://github.com/grongierisc/iris-embedded-python-wrapper |
| 45 | + |
| 46 | +After you have all the requirements, you can start the IRIS instance and run the python code. |
| 47 | + |
| 48 | +#### Debugging |
| 49 | + |
| 50 | +To debug the code, you can use the python debugger. |
| 51 | + |
| 52 | +Example with VsCode: |
| 53 | + |
| 54 | +1. Open the code in VsCode to an existing folder. |
| 55 | + |
| 56 | +For example, you can use this git repository: |
| 57 | + |
| 58 | +```bash |
| 59 | +git clone https://github.com/grongierisc/iris-python-interoperability-template |
| 60 | +``` |
| 61 | + |
| 62 | +Open the folder `iris-python-interoperability-template` in VsCode. |
| 63 | + |
| 64 | +```bash |
| 65 | +code iris-python-interoperability-template |
| 66 | +``` |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | +2. Install the python extension. |
| 71 | + |
| 72 | +Go to the extensions tab and search for `Python`. |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | +3. Create a virtual environment and select it. |
| 77 | + |
| 78 | +```bash |
| 79 | +python -m venv .venv |
| 80 | +source .venv/bin/activate |
| 81 | +``` |
| 82 | + |
| 83 | +And select the virtual environment in VsCode. |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | +4. Install the dependencies. |
| 88 | + |
| 89 | +```bash |
| 90 | +pip install -r requirements.txt |
| 91 | +``` |
| 92 | + |
| 93 | +5. Run the code in debug mode. |
| 94 | + |
| 95 | +Open the file `src/python/reddit/bo.py`, put a breakpoint in the line 26. |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | +And run the code in debug mode. |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | +And you can debug the code. |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | +### Docker Container |
| 108 | + |
| 109 | +The general idea is the same as the local installation. |
| 110 | + |
| 111 | +The main difference is that you need to have a attached VSCode to the docker container. |
| 112 | + |
| 113 | +For that, you can use the [Remote - Containers](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers) extension. |
| 114 | + |
| 115 | +Then you can attach the VSCode to the docker container and follow the same steps as the local installation. |
| 116 | + |
| 117 | +## Tips |
| 118 | + |
| 119 | +### Use unit tests |
| 120 | + |
| 121 | +The best way to debug the code is to use unit tests. |
| 122 | + |
| 123 | +You can use the python unittest library to write unit tests or pytest. |
| 124 | + |
| 125 | +You have examples in [iris-python-interoperability-template](https://github.com/grongierisc/iris-python-interoperability-template/tree/master/src/python/tests). |
| 126 | + |
| 127 | +### Use if __name__ == '__main__' |
| 128 | + |
| 129 | +You can use the `if __name__ == '__main__':` to run the code only when you run the python file. |
| 130 | + |
| 131 | +Example: |
| 132 | + |
| 133 | +```python |
| 134 | +def my_function(): |
| 135 | + print('Hello World') |
| 136 | + |
| 137 | +if __name__ == '__main__': |
| 138 | + my_function() |
| 139 | +``` |
| 140 | + |
| 141 | +You also have examples in [iris-python-interoperability-template](https://github.com/grongierisc/iris-python-interoperability-template/blob/master/src/python/reddit/bo.py#L86). |
| 142 | + |
| 143 | + |
| 144 | + |
| 145 | + |
| 146 | + |
0 commit comments