0

LibreOffice on windows comes with it's own Python version. How can one install new packages for LibreOffice-Python.

I use Linux myself and I've written a working macro that I would like to be usable for windows users as well, but it uses packages that aren't available in standard LibreOffice.

We tried updating by pip, but as expected it only updates the system's python. We are aware that zazpip exists, but apparently it didn't work with the tester. Therefore I am explicitly looking for other solutions.

Natan
  • 728
  • 1
  • 7
  • 23
  • 1
    Do you want to update the Python version or just install a package? If you want to update the Python version, that cannot be done with pip. Also, if you find a way to update Python, be careful, there is a good chance you may break libreoffice itself as it may rely on a specific Python version. – Thomas Feb 18 '22 at 15:02
  • I use Windows, which definitely has it's own version of Python for LibreOffice. In Windows you can install pip with a little bit of work and then install new modules into the LibreOffice. The process for setting up pip in Windows is [outlined in this answer](https://stackoverflow.com/a/68568331/9705687). For Linux, I thought LibreOffice used the system version of Python3. Are you using `pip` or `pip3` to install modules? Maybe `pip3` will work for you. And if I'm wrong about Linux, where does the LibreOffice version of Python reside? Maybe you can adapt my Windows solution to Linux. – bfris Feb 22 '22 at 18:59
  • @bfris the question is about Windows. It works without problems on Linux. Updating python using pip, updates the non-libreOffice python. But your link looks promising and I will try it out as soon as I have the time. – Natan Feb 22 '22 at 19:29

3 Answers3

3

Quickly installing a python package with pip will most likely just make it available in python under your operating system, not LibreOffice. The python docs installing python packages describe pip usage with a warning about using python managed by another package manager (ie LibreOffice).

The python under Windows is a cut-down embedded python version (while most linux distros use their system python) without pip or get-pip.py and there are no supporting python directories in the path. LibreOffice is also installed on Windows with administrator permissions.

Install as follows:

  1. Download the https://bootstrap.pypa.io/get-pip.py file
  2. Start a command prompt, preferably with administrator permissions
  3. Change to the LibreOffice installation directory (eg cd C:\Program Files\LibreOffice\program)
  4. Run python get-pip.py
  5. Close command prompt

This will install or upgrade pip for the LibreOffice version of python without affecting the operating system version. Additionally, it will install setuptools and wheel if they’re not installed already.

The installed location depends on the python installation context. For example with python v3.8.10 a WARNING will be displayed that the scripts are installed in 'C:\Program Files\LibreOffice\program\python-core-3.8.10\Scripts' for an administrator install or alternatively, 'C:\Users\User\AppData\Roaming\Python\Python38\Scripts'.

The files installed with pip and the execution is more complicated than it appears. The easiest way to use the right pip for the right python version is to run it from the python install directory using python -m. This example is a verbose package listing:

C:\Program Files\LibreOffice\program>python -m pip list -v
Package    Version Location                                                                  Installer
---------- ------- ------------------------------------------------------------------------- ---------
pip        22.0.3  c:\program files\libreoffice\program\python-core-3.8.10\lib\site-packages pip
setuptools 60.9.3  c:\program files\libreoffice\program\python-core-3.8.10\lib\site-packages pip
wheel      0.37.1  c:\program files\libreoffice\program\python-core-3.8.10\lib\site-packages pip

To install my-package change to the LibreOffice installation directory and run:

C:\Program Files\LibreOffice\program>python -m pip install my-package

flywire
  • 1,155
  • 1
  • 14
  • 38
  • I assume one has to save `get-pip.py` in `...\program` or use full path to `get-pip.py` otherwise it should be hard for python to find. This looks promising. I'll need a few days before I can verify this solution and then will accept this answer if working. – Natan Mar 05 '22 at 07:27
  • Download `get-pip.py` anywhere running it with full path then discard it after use. Initially, you don't have admin access to `C:\Program Files\LibreOffice\program`. – flywire Mar 05 '22 at 08:25
  • Sadly the user was unable to install anything with the pip version installed in LibreOffice-python. I have the suspicion that he installed pip wrongly, but I can't test myself right now (no time). I will do that after the 05.04 and can then judge whether your answer is correct or not; so far I can only say it is helpful. I am sorry. – Natan Mar 12 '22 at 08:10
  • @Natan, if your users do no have permissions to write files in `C:\Program Files\LibreOffice\program`, then get-pip is not going to work. You might be able to talk your IT personnel into changing permissions on `program` directory and all subdirectories. – bfris Mar 15 '22 at 19:08
  • @bfris that is incorrect, read again - get-pip works for both system and user installation context – flywire Mar 16 '22 at 00:16
  • @flywire. Huh? I find your instructions pretty great for installing pip for LibreOffice. But in a corporate environment, many users will not have permissions to write anywhere inside `Program Files` or subdirectories. – bfris Mar 16 '22 at 16:20
  • pip knows about system and user install. User install is to 'C:\Users\User\AppData\Roaming\Python\Python38\Scripts'. All I can say is try it, I did. I know about corporate environment, real issue is getting access to libraries through firewall: https://stackoverflow.com/questions/50857160/install-miniconda-and-extra-packages – flywire Mar 16 '22 at 19:46
1

If it comes with a specific version of Python, it may need to reference specific functions from that version. The best answer I can give you is: If Python is included in the source code, try forking the source code with your own version of Python, and compiling that.
Or,
If there's a specific package manager for Python included, try using that to update Python.

  • How would I check if there is a package manager included? The "compiling from source"-solution sounds to troublesome to be worth it for my usecase. – Natan Feb 18 '22 at 15:33
  • Short answer is LibreOffice on Windows needs to run the embedded python unless you follow trickery like https://python-ooo-dev-tools.readthedocs.io/en/latest/dev_docs/dev_notes.html#windows – flywire Aug 16 '22 at 07:34
1

A discussion of what others have tried is at https://ask.libreoffice.org/t/install-python-package-for-libre-office/66934/16. I doubt you'll find any better answers than what is described there. Try the various approaches and see what works for your situation.

I did get it to work once but it wasn't easy—some environment variables needed to be set in my case.

Is there a way to write your macro so that it does not depend on a separate package? That is what I ended up doing. You can implement part of the package yourself if needed. In the end it wasn't so bad, and it works on all operating systems.

Jim K
  • 12,824
  • 2
  • 22
  • 51
  • Rewriting the packages is not really an option since my macro includes webscraping (BeautifulSoup) that I really don't want to rewrite. For my use case that is simply not a feasible option. But possible nonetheless. I will take a look at the link. – Natan Feb 20 '22 at 08:49
  • My project required `lxml`, which apparently is used in web scraping (although the project was not web-related). My solution was to import `xml.etree` instead and then write a custom module that used inheritance to add the extra needed functionality. Haven't worked with BeautifulSoup, so not sure if the same concept would work in that case. In addition to parsing, `urllib` is also included with LibreOffice on Windows. Also, for the truly ambitious, the LibreOffice UNO API provides access to high-powered XML libraries, if you can somehow get through the little-documented interface. – Jim K Feb 21 '22 at 12:48