Resolving LibreOffice python imports in VS Code (com.sun.star...)

I’m working with setting up VS Code like an editor for Python scripts run by LibreOffice.

The Pylance component in VS Code stumbles on all imports on this pattern:

from com.sun.star.text.TextContentAnchorType import AS_CHARACTER
from com.sun.star.awt import Size

And also on the variable XSCRIPTCONTEXT.

I know these references are generated, as I cannot seem to find any Python files that declare them. Where/how are they declared ?

I would like to add some path or file pointer to VsCode - so that PyLance could resolve these imports.

Advice ?

Hints for the use of IDEs (but not VSCode) are in the help-files:
https://wiki.documentfoundation.org/Macros/Python_Basics

Maybe the hints mentioned here:
https://wiki.documentfoundation.org/Macros/Python_Design_Guide
are also useful, especially the reference to the french “XSCRIPTCONTEXT revisete”

The Python runtime needs to be compiled by the same compiler as the office suite. The runtime can be found at <Install_dir>/program/python[.exe].
With a Linux distribution everything the binaries fall out of the same compiler, so you can use the distribution’s native Python and office suite.

Currently I have both LibreOffice and Vs Code running on Windows 10.

The Python interpreter is there as: c/Program Files/LibreOffice/program/python-core-3.8.10/bin/python.exe - installed by LibreOffice setup.

Inside of Vs Code I have swapped to this python interpreter.

On my Linux system XSCRIPTCONTEXT is defined in pythonscript.py
The entire object hierarchy comes from a framework called “Unified Network Objects” (UNO).

Thanks, I located the pythonscript.py file. I would like PyLance (or the language server) to by default read this file. But I can only add directories to that one (where it finds the imported modules).

I guess the UNO bindings are generated by C/C++, and somehow exposed to the Python interpreter. And that there are no corresponding Python source files (?) .

All languages use the same Unified Network Object. All it takes is a little bit of glue code.

Yes, and somehow Python can then use this code:

from com.sun.star.util import XModifyListener

But there is nowhere in the file system a file com/sun/star/util.py (as would be for most other import statements).

How can an external tool (like a language server) get hold of the same information as the Python interpreter ?

Maybe I need to search this piece from other sources…

https://api.libreoffice.org/docs/idl/ref/namespacecom_1_1sun_1_1star_1_1util.html
[Tutorial] Introduction into object inspection with MRI

Both links are interesting and bring me new knowledge. The MRI is an interesting tool (I had used it but the articles points out new useful things to me).

You could say my original question was more how to make the code analyzer in VS Code go quiet - by resolving the import paths. I do use the output of the code analyzer - as it picks up bugs. It gets a bit overloaded now by these unnecessary references to UNO function calls it cannot resolve.

I’ll live with it for now.

I attach an image illustrating the warnings I wanted to resolve.

So your question is more on configuration of pylance. I could find how to silence missing imports via google (pylance ignore missing imports) at Disable specific Pylance linting messages in VS Code settings.json like with "python.linting.pylintArgs" - Stack Overflow

Thanks. I really would like it to be able to resolve those calls. But if there’s no way, then it is so. Your suggestion removes the symptoms (and disables this whole category of warnings from the code analyzer) - better than no solution at all.