Python - built-in libraries fail to import

I’m trying to debug a Python script using the methods documented here: Input/Output to Screen with Python - The Document Foundation Wiki

I’ve been unable to implement any of these options because the imports, such as “msgbox” and "
com.sun.star.awt" are not being resolved and fail to install via pip.

I’m using LibreOffice 6.3.4.2; Python 3.8; and PyCharm 2019.3

Is there an easier way to debug a Python macro? I tried using Python’s built-in logging, but it doesn’t work when the script is called from LibreOffice.

Lets start with a question of your OS. If you are on windows there would be two versions of python one with pycharm, one inside LibreOffice…
.
Then it is clearly stated you can’t import Basics MsgBox. So what do you try to import from where? The entry to the api-function?

Sorry, forgot to put my OS. Yes, it’s Windows 11 - and you are correct… looking in the LibreOffice/program folder there is an installation of Python 3.5.9 there. I tried setting the PyCharm interpreter to use that version of Python, but I get: “Error: Standard Python ‘venv’ module not found”.

As for how I’m importing; I am simply using the import examples from the page I linked:

import uno
from msgbox import MsgBox 

Uno I was able to install via pip, but not MsgBox.

EDIT:

Not sure why, but the MsgBox import started working (or at least there isn’t a compiler error). The “com.sun.star.awt.” imports are still failing though with: “AssertionError: SRE module mismatch”.

It’s late now; I’ll try tomorrow and see if MsgBox actually imported - if so, that may be enough for debugging.

In the text you link you find:

Using msgbox.py
This module comes bundled with LibreOffice

so you have to find the module in the Installation of LibreOffice and copy it to your location fir import. It is not available at a place, where pip could find it.
.
I hope you know: An external python script is not running inside LibreOffice, so even import uno will not automatically activate LibreOffice as runtime-environment… The examples are meant to be used “inside” LibreOffice as macros.

1 Like

there is a »uno« -package on pipy BUT this is NOT THE UNO which is included in Libreoffice

1 Like

I think, this is not true. Start the office suite from a console and the output of your Python macros goes to that console.
For debugging you can also use the apso extension (alternative python script organizer)… It contains a simple msgbox with text and OK button only.

from apso_utils import msgbox, mri 

imports the msgbox and a method to call the mri object inspector.

1 Like

OK, so it looks like MsgBox requires uno to use and if the uno found by pip is the wrong one, then that obviously won’t work.[quote=“Villeroy, post:6, topic:87076, full:true”]

I am using APSO v1.2.6.2 and it seems like this would work for debugging, but unfortunately that import doesn’t work for me either (“Unresolved reference ‘apso_utils’”).

I am opening my script using the APSO → edit menu. Does that automatically pass the required references to PyCharm, or do I need to somehow point PyCharm to those libraries?

Well, it appears this solution is not consistent for some reason. I opened my spreadsheet today and got an (almost) endless wall of error popups saying: “No module named ‘apso_utils’”. Holding escape eventually allows the document to load.

The crazy thing is that if I open my script via APSO, make any change, and then save the script; it compiles and runs just fine until I restart Calc. I tried starting Calc with that parameter: "–calc --accept=“socket,host=localhost,port=2002;urp;”, but nothing changed. How would PyCharm know to communicate with LO via this port? Would I need to set this connection up manually?

Here is basic explanation: Macro Programming in OpenOffice/LibreOffice with using Python[EN] | by Rebah Özkoç | Analytics Vidhya | Medium


And here is printscreen for PyCharm to set Base Interpreter to LibreOffice “python.exe” Starting LibreOffice with Python — Macro Programming in OpenOffice/LibreOffice with using Python[EN]-2 | by Rebah Özkoç | Analytics Vidhya | Medium

2 Likes

I tried following that guide, but I get, “Error: Standard Python ‘venv’ module not found” when trying to set the interpreter to the LO one, no matter how I do it.

There is note for: Error: standard python 'venv' module not found


I also wasn’t able to run Python for Libre in PyCharm, so I use Pyzo. But the true is I’m beginner in Python and sometimes it is “fight” with Pyzo and ASPO than I run a script successfully :-(.

Yes, I saw that note and tried that but it still fails. I ended up using Visual Studio since that’s what I use for most of my other coding. There were no issues setting the interpreter and environment to use LO Python. It still warns that the imports are unresolved, but there are no errors when running my scripts.

Since 17 years I develop Python macros and extensions with nothing more than a most simple text editor (find/replace, syntax highlighting), MRI, APSO and the pure Python runtime that is shipped with the office suite (no additional packages). For add-ins (extensions with spreadsheet functions) I need the LibreOffice SDK to compile the UNO type definitions.

After some testing, it looks like this is actually just an issue with the PyCharm compiler not being integrated with LibreOffice/APSO. PyCharm shows unresolved reference errors for all of these imports, but LibreOffice is able to resolve them when the script is run from within LO.

There was no need to try and use the Python installation in the LO folder as the interpreter or to try and import any libraries into PyCharm (though this could potentially get rid of the error in PyCharm). Thank you everyone who responded for pointing me in the right direction.

For now, at least, I will just use APSO’s msgbox for debugging. It would be nice to get Python’s built-in logging working so I can have actual logs to look at instead of pop-up dialogues, but this should work for now.

Maybe it is dumbness from me, but did you start Libre with parameter for Python like: C:\Program Files (x86)\LibreOffice 5\program\soffice.exe" --calc --accept="socket,host=localhost,port=2002;urp;

This is not a parameter for Python. It starts LibreOffice listening on port 2002. Any external program (contrary to macros) written in C++, Java, Python can access the API of this running office instance.

With macros the office suite calls some code.
With a listening office, your code calls the running office.

I think that @synthc calls the wrong Python runtime.

2 Likes

Alright, I finally got this partially working. Here’s what I did:

  1. Updated to LibreOffice 7.4.5.1 - this came bundled with Python 3.8 and contained some of the necessary libraries.
  2. Added a PYTHONPATH variable in my environment variables and entered the python 3.8 exe location. I also added this path to my PYTHON and PATH as well just to be safe.
  3. In PyCharm: added a new project interpreter, but selected “System Interpreter” NOT “Virtualenv Environment” and pointed it to python.exe in “…program/python-core-3.8.16/”.

This avoids the “Error: Standard Python ‘venv’ module not found” error and allows some LibreOffice modules to be resolved (and, crucially, it doesn’t lose the references when restarting).

The ones I’ve found that work are uno and msgbox. The ones that still don’t work are anything from
com.sun.star.awt. and apso_utils. MsgBox is a less than ideal, but workable debugging solution for now.

Many docs pages (such as: Python : Importing Modules) reference a folder called “/Scripts/python/pythonpath” where python modules are supposed to be stored. I don’t have this folder and it wasn’t added when upgrading to LibreOffice 7. I could create it, but I have no idea what exactly it’s supposed to contain.

EDIT:

It looks like python’s logging library magically started working too after the update - so I’m good as far as debugging goes.

As is becoming a pattern, this was working great yesterday and then I started up my computer and opened LibreOffice and was welcomed by a barrage of errors. The Basic “ThisComponent” is now returning null. I made absolutely no changes to the script or my LO/Python installation since it was working yesterday, so I have no idea what could have happened.

Because this is become a new, more serious problem, I’ve opened a new thread here: Python interpreter and modules not working reliably