Python interpreter and modules not working reliably

I’ve been having endless problems trying to get my Python macro to work reliably and I’m starting to think that my LibreOffice Python is corrupt. Here’s what’s happening:

  1. Most LO libraries fail to import. Anything from “com.sun.star.awt.” always fails - uno and msgbox work for a while and then seem to randomly stop working. In PyCharm and Visual Studio the import statements sometimes show errors (either saying the import couldn’t be resolved or the module was not found) and sometimes they don’t.

  2. Most crucially, ThisComponent and XSCRIPTCONTEXT do not work reliably. They were working yesterday and then without making any changes to my script, they suddenly started returning null today.

This is incredibly perplexing behavior, and it only happens with LibreOffice Python (other Python versions work 100% reliably). While trying to work my way through the labyrinth of Python documentation; I found this page: Python : Importing Modules which says that Python modules are stored in “(User Profile)/Scripts/python/pythonpath”; but I have no such folder.

I installed LibreOffice 7 yesterday which came with Python 3.8 and seemed to fix some of my issues. But upon opening my spreadsheet today, I find that my imports aren’t working again and my document reference is null. The script still runs, so the python installation is working to some degree.

I’m wondering if there is any definitive guide on how to set all of this up and if anyone can confirm that modules are stored in “(User Profile)/Scripts/python/pythonpath”. If so, how can I repair my LO Python? Updating LO clearly didn’t work. Maybe someone can upload the modules for me?

Here is my Basic code:

function cev(a as String) as double
	Dim scriptPro As Object, myScript As Object
	Dim a1(1), b1(0), c1(0) as variant
	a1(0) = ThisComponent
	a1(1) = a
    scriptPro = ThisComponent.getScriptProvider()
    myScript = scriptPro.getScript( _
           "vnd.sun.star.script:Cell_Functions.py$calcEffectValue?language=Python&location=user")
	cev = myScript.invoke(a1, b1, c1)
end function

And the relevant Python code:

def calcEffectValue(doc, effects):
    nameDict = {}
    #doc = XSCRIPTCONTEXT.getDocument()

    # Populate dictionary with named ranges - ["name"] = value:
    for range in doc.NamedRanges:
        nameDict[range.getName()] = range.getReferredCells().getDataArray()[0][0]

This code fails because “doc” is null. This happens regardless of whether I rely on the reference passed in by the basic script or if I use XSCRIPTCONTEXT.getDocument() (which also sometimes shows an error in my IDEs, sometimes not).

This is happening with:
Windows 11
LibreOffice 7.4.5.1 with the included Python 3.8.16
PyCharm 2019.3
Visual Studio 2019 v16.11.23

This Python script is critical to an important project I’m working on. I’ve spent countless hours troubleshooting these issues and would love to find a permanent solution. Any help is appreciated.

https://api.libreoffice.org/docs/idl/ref/interfacecom_1_1sun_1_1star_1_1script_1_1provider_1_1XScript.html


	cev = myScript.invoke(Array(ThisComponent, a), Array(), Array())

doc is still null using that code.

@Villeroy Do you have the “(User Profile)/Scripts/python/pythonpath” directory? If so then it would appear something is wrong with my LO installation. Or are the docs wrong?

I feel like I’m going crazy here, but all of this just started working again. All I did was re-add the LO Python environment in Visual Studio and now; not only does my doc reference work again, but even the “com.sun.star.awt.” imports are working now (these have never worked before).

This shouldn’t even matter to LibreOffice, should it? Correct me if I’m wrong, but LibreOffice should just be reading the text of my script and using its own internal interpreter to run it. There’s no need for any communication between LibreOffice and the IDE - all this should affect is errors and autocomplete in my IDE, right?

I’ve never really experienced anything quite this finicky in my 7+ years of development. All I can imagine that would be causing this is a stale config or something that gets “bumped” when I update the interpreter. Maybe my LO installation is broken - I’d still like to know if the “(User Profile)/Scripts/python/pythonpath” directory is supposed to exist.

No, because there are to many options. IMHO you are mixing use-cases and expecting the IDE to fill the gaps…

  • Most examples you have will be macros called by a running LibreOffice. Today you can use APSO (extension) to put this python-scripts in the right place.
  • From the other thread we know you use PyCharm for development. IMHO, even if you setup the python inside the LibreOffice -folder to be called, you will not have a running LibreOffice when you call a script inside PyCharm. You may fix compile-errors by providing uno-module to PyCharm, but this will not fix the Problem, of LibreOffice not beeing reachable for your script, as it is in a separate process, even when started before.
  • Also in the other thread there was a link (by KamilLanda ?) wich described to setup LibreOffice to accept commands via a port. I have not tried this before, but I guess this kind of programs need another kind of coding. The typical macro will be called at some time and can look at the event, it was called by and work on reference “this document”. Sitting outside LO and communicating via port requires first to load/create some document or get access to something worked on, before you can start using it.
  • My python programs (at this time) are completly outside LO, and only preparing data. I’m using Atom but this kind of external programs is what you usually see in python-tutorials. No reference to LibreOffice.

You have always to make sure, for wich Environment your python-code is designed, in any other environment it will fail. (You may create an adaptive layer …)
.

Then, before re-installing rename your profile, as the install will (usually) KEEP your profile, including any errors (also including all macros you stored in the profile, so it is a good feature to keep the profile)

I’m not actually expecting anything from the IDE at this point - I’m just happy if the script runs (auto-complete on LO libraries and removal of false alarm errors would just be a bonus at this point).

I am not running my macros from within my IDE (I switched to Visual Studio BTW). My macros only run from within Calc - I pass cell data to a macro in the formula bar and this is how the macro is called.

This is why it’s so mysterious to me that changes in the IDE are fixing problems with script execution. It should be completely independent of the IDE - just reading the text of the script file. I could use Notepad as my IDE and it shouldn’t change anything.

For Windows (typical installation) your module from topic start message should have the path:

C:\Users\YourName\AppData\Roaming\LibreOffice\4\user\Scripts\python\calcEffectValue.py 

Yes, I have because I created one. It contains one module with frequently used helpers.

import OfficeHelper

def test_MRI():
    ofc = OfficeHelper.Office()
    ofc.mri(XSCRIPTCONTEXT.getDesktop())

OfficeHelper.py is in (User Profile)/Scripts/python/pythonpath

1 Like

I see. So this directory is only for installing modules that aren’t included with LibreOffice. Thanks for that info.