LibreOffice extensions with Python – part 2: Debugging
In my previous blog post on creating LibreOffice extensions with Python, I have discussed how to write a Python code that works with LibreOffice API, and can be run and debugged in an IDE, and packed later in an extension. Now I discuss how to debug the Python code.
Here you can read the previous post:
Preparing the Debug Environment
First, you need the minimal extension, and the small Python script inside it. The complete extension as described in the previous post, is available below, and the plan is to make it available among the other LibreOffice SDK examples:
https://gerrit.libreoffice.org/c/core/+/159938
For debugging, you can open the Python file main.py
, and start editing it, adding the required functionalities.
To debug the Python program, the best way is to use an IDE with the LibreOffice internal Python as the interpreter. You only need to install LibreOffice, and LibreOffice SDK from libreoffice.org/download.
The LibreOffice internal Python interpreter resides in the program folder of the LibreOffice installation. The Python package for accessing UNO components is already installed.
In Linux, the default location of the interpreter is (for LibreOffice 7.6):
/opt/libreoffice7.6/program/python
In macOS, the default location is:
/Applications/LibreOffice.app/Contents/Resources/python
And on Windows, the default location is:
c:\Program Files\LibreOffice\program\python.exe
Configuring the Interpreter in an IDE
This is an example python configuration for PyCharm, a popular Python IDE.
With this configuration, you should be fine to start the debugging. By just clicking on the debug button, you can start running the script line by line, and use the debugging utilities in the IDE to check the value of the variables in order to be diagnose the possible problems.
As described in the previous part of the blog post, in the example Python code, the context is handled in different situations. Thus, you can use the same Python script and pack it as an extension.
Auto-Completion
Unfortunately, the autocomplete does not currently work for the methods that are available in the UNO components. This is because they are bridged dynamically, and thus autocomplete can not create the list of usable methods.
This can be improved with the use of Python auto-completion feature called REPL, and a utility package. Here I suggest using ptpython:
First, you need to install pip in LibreOffice internal python using PIP bootstrap, or ZazPip package. To do that, please refer to this question and answer on Ask LibreOffice:
Then you should be able to use Python interactive console with auto-completion. To use it, you have to run this small code:
from ptpython.repl import embed embed(globals(), locals())
This method works both on Windows and Linux, but not on APSO console. It is important to mention that this is not ideal as it only works in interactive console, and also does not provide the details about the function parameters.
Further Help
For further information, please refer to the TDF Wiki page:
There you will find configuration for various IDEs to set up environment for debugging Python scripts that work with LibreOffice API.
The SDK is not necessary in the context you describe. Unless I’m mistaken some justification for installing SDK is necessary.
You are correct. Beside the lack of SDK examples, and some documentation / tools, everything should work fine for Python. I changed SDK to API in some places, which is more precise. But, anyway it is helpful to install SDK to have examples, and some tools.