2020-08-16

46: Using an External Python for LibreOffice or OpenOffice

<The previous article in this series | The table of contents of this series | The next article in this series>

With a binary LibreOffice/OpenOffice distribution, for this article. Not every version is OK. For using a full and bug-fixed Python.

Topics


About: UNO (Universal Network Objects)
About: LibreOffice
About: Apache OpenOffice
About: The Python programming language

The table of contents of this article


Starting Context


  • The reader has a basic knowledge on LibreOffice or Apache OpenOffice.
  • The reader has a basic knowledge on the Python programming language.

Target Context


  • The reader will know how to use an external Python for Python macros for a LibreOffice or Apache OpenOffice binary distribution.

Orientation


This article is for using an external Python for Python macros for a LibreOffice or Apache OpenOffice binary distribution: if you are developing your external Python UNO program, most of the measures in this article will not be necessary; if you are building your LibreOffice or Apache OpenOffice from a source distribution, another way will be more versatile.

Linux and Windows platforms will be discussed, although the measures should be similar for macOS.

For this article, it is not that any Python version can be used; if you want to use a Python version that cannot be made used by the measures of this article, you will have to build your LibreOffice or Apache OpenOffice from a source distribution (will be discussed in a future article).

Stage Direction
Here are Hypothesizer 7, Objector 46A, and Objector 46B in front of a computer.


Main Body


1: Your LibreOffice or Apache OpenOffice May be Using an Internal Python or an External Python


Hypothesizer 7
Is your LibreOffice or Apache OpenOffice using an internal Python or an external Python?

Stage Direction
Objector 46A shakes his head, seeing which, Objector 46B shakes her head, likewise.

Hypothesizer 7
. . . What does that mean, sir? Isn't it using either?

Objector 46A
I don't know. It might be using something, because my Python macros run.

Hypothesizer 7
Sir, it is definitely using an internal Python or an external Python.

Objector 46A
Whatever.

Hypothesizer 7
How about you, madam?

Objector 46B
I don't know. How am I supposed to know?

Hypothesizer 7
If you have installed your LibreOffice or Apache OpenOffice with a downloaded binary package from a Web page, most probably, an internal Python is being used.

Objector 46B
Of course, I did; how else am I supposed to install it?

Hypothesizer 7
If you use Linux, you can install it via the packages manager of the Linux distribution.

Objector 46B
"Linux"? I don't use "Linux", whatever that is . . .

Objector 46A
I have installed it with a downloaded binary package from a Web page, in Linux.

Hypothesizer 7
To know more certainly, you can check whether the 'program' directory of your LibreOffice or Apache OpenOffice installation has a 'python-core-%version%' directory, which contains the internal Python.


2: Why You May Want to Use an External Python


Objector 46A
Anyway, some things are missing in the Python being used! For example, I cannot import 'tkinter'!

Hypothesizer 7
Yes, that is because the internal Python is a limited Python.

Objector 46A
More critically, 'pip' is missing and can't be installed!

Hypothesizer 7
I guess so.

Objector 46A
What am I supposed to do?!

Hypothesizer 7
You seem to be supposed to endure in the limitation, but in fact, you do not need to: you can make your LibreOffice or Apache OpenOffice use an external full Python.


3: It Is Not That You Can Use Any Arbitrary Python


Objector 46A
So, I can just install a Python and register the path of it, right?

Hypothesizer 7
Not right, sir, actually: it is not that you can use any arbitrary Python.

Objector 46A
Huh? That doesn't make sense. Why can't I use the Python I like, in my computer!

Hypothesizer 7
Remember that Python is not so great in binary compatibility; you know that 'pyc' files or Python extensions in C or C++ are mostly not compatible between versions. In this case, a critical problem is the 'pyuno.so' (for Linux) or 'pyuno.pyd' (for Windows) Python extension, which has been built with a specific Python version.

Objector 46A
Well, that's regrettable.

Hypothesizer 7
You can definitely use the exact specific version Python and can probably use a same minor version Python, but can unlikelily use any different minor version Python.

Objector 46A
What is "minor version"?

Hypothesizer 7
For example, '3.7' for '3.7.8'.

Objector 46A
Well, is it meaningful to use '3.7.8' instead of '3.7.6'?

Hypothesizer 7
I think it is: micro versions are for fixing bugs, which may be biting your program.

Objector 46A
True. But I can't use '3.8'?

Hypothesizer 7
In the context of this article, probably not; if you really want to use '3.8', you will have to build your LibreOffice or Apache OpenOffice from a source distribution.

Objector 46A
Can I use any version that way?

Hypothesizer 7
I do not say "any version" because there could be a source incompatibility, but most practical versions should be OK.

Objector 46A
What do you mean by "practical versions"?

Hypothesizer 7
You will not try to use '3.0' now, will you , sir?

Objector 46A
Probably, not.


4: Doing It


Hypothesizer 7
So, let us do it.

1st, we install a valid Python.

Objector 46B
How?

Hypothesizer 7
Well, you can do as you like, but as we are installing a specific version, probably, we will download it from the Python download page.

Stage Direction
Hypothesizer 7 opens the the Python download page and downloads the '3.7.8' "XZ compressed source tarball" file to the directory, '~/myData/downloadedFiles'.

Hypothesizer 7
We are going to use '3.7.8' "XZ compressed source tarball" because this is a Linux machine and the internal Python for my LibreOffice is '3.7.6'.

Objector 46B
Is this "Linux"? . . . It's gross!

Hypothesizer 7
. . . I do not understand your sense, but for 64-bits Windows, you will probably download a "Windows x86-64 executable installer".

Anyway, we install it at '~/myApplications/execution/python', like this.

Stage Direction
Hypothesizer 7 opens a terminal and executes these commands.

@bash Source Code
mkdir -p ~/myData/downloadedFiles/python
tar -xvf Python-3.7.8.tar.xz -C ~/myData/downloadedFiles/python --strip-components=1
cd ~/myData/downloadedFiles/python
./configure --enable-shared --prefix=${HOME}/myApplications/execution/python
make
make install

Objector 46B
Gross!

Hypothesizer 7
. . . For Windows, you can just execute the installer (let us suppose that the destination is 'D:\python').

Stage Direction
The commands end successfully.

Hypothesizer 7
Then, we set the path of the Python main shared (or dynamic, for Windows) libraries ('~/myApplications/execution/python' for this case) into 'LD_LIBRARY_PATH' (or 'PATH', for Windows) in precedence over the LibreOffice or Apache OpenOffice program path (if the LibreOffice or Apache OpenOffice program path is in the environment variable).

Objector 46A
So, you register the path, after all!

Hypothesizer 7
I did not say that I would not, sir; I said that that was not enough.

Objector 46A
. . .

Hypothesizer 7
So, we edit the file named 'pythonloader.unorc' in Linux or 'pythonloader.uno.ini' in Windows, in the 'program' directory, like these.

@pythonloader.unorc Source Code
[Bootstrap]
PYUNO_LOADER_PYTHONHOME=file:///home/tanichida/myApplications/execution/python
PYUNO_LOADER_PYTHONPATH=$PYUNO_LOADER_PYTHONHOME/lib/python3.7 $PYUNO_LOADER_PYTHONHOME/lib/python3.7/lib-dynload $PYUNO_LOADER_PYTHONHOME/lib/python3.7/site-packages $ORIGIN

@pythonloader.uno.ini Source Code
[Bootstrap]
PYUNO_LOADER_PYTHONHOME=file:///D:/python
PYUNO_LOADER_PYTHONPATH=$PYUNO_LOADER_PYTHONHOME/Lib $PYUNO_LOADER_PYTHONHOME/Lib/site-packages $ORIGIN

Stage Direction
Hypothesizer 7 edits the file.

Objector 46A
That is setting some Python modules paths, right?

Hypothesizer 7
Yes, sir.

Now, the configuration has finished.

Of course, if a LibreOffice or Apache OpenOffice instance is up, we will have to restart it though.


5: Testing It


Hypothesizer 7
So, let us test it.

We create a Python module file, 'PythonEnvironmentChecker.py', at 'share/Scripts/python' in the LibreOffice or Apache OpenOffice directory, with these contents.

@Python Source Code

import sys
from com.sun.star.frame import XDesktop2
from com.sun.star.lang import XComponent
from com.sun.star.script.provider import XScriptContext
from com.sun.star.text import XText

XSCRIPTCONTEXT: XScriptContext

def checkPythonEnvironment () -> None:
	l_unoDesktop: XDesktop2 = XSCRIPTCONTEXT.getDesktop ()
	l_currentUnoDocument: XComponent = l_unoDesktop.getCurrentComponent ()
	if hasattr (l_currentUnoDocument, "Text"):
		l_unoText: XText = l_currentUnoDocument.getText ()
		l_unoText = l_unoText.getEnd ()
		l_unoText.setString ("The Python environment: version -> {0:s}, paths -> {1:s}\n".format (sys.version, str (sys.path)))
		return None

Stage Direction
Hypothesizer 7 shows the file.

Objector 46B
. . . What is that doing?

Hypothesizer 7
That code is writing a string in the current Writer document, but how to do so is not any issue here; just note that the string to be written consists of the Python version and the Python modules paths, which will prove the effectiveness of our configuration.

Stage Direction
Hypothesizer 7 starts a LibreOffice instance, opens a Writer document in it, and executes the macro from the 'Tools' -> 'Macros' -> 'Run Macro...' menu item. This is written in the document, which Objector 46A and Objector 46B stares vacantly.

@Output
The Python environment: version -> 3.7.8 (default, Jul 31 2020, 08:59:27) 
[GCC 7.5.0], paths -> ['/home/tanichida/myApplications/execution/python/lib/python3.7', '/home/tanichida/myApplications/execution/python/lib/python3.7/lib-dynload', '/home/tanichida/myApplications/execution/python/lib/python3.7/site-packages', '/opt/libreoffice6.4/program', '/home/tanichida/myApplications/libraries/python', '/home/tanichida/myApplications/execution/python/lib/python37.zip']

Hypothesizer 7
. . . Why don't you people react? Did I bore you?

Objector 46B
Well, don't worry; that certainly shows the right version, yes.


References


  • Python Software Foundation. (2020). Download Python | Python.org. Retrieved from https://www.python.org/downloads/
<The previous article in this series | The table of contents of this series | The next article in this series>