Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Installing pyuno (LibreOffice) for private Python build

There are a few related threads about this topic here ad here but they seem a bit dated.

I just downloaded LibreOffice 4 which has a Python 3.3.0 built in. Using that Python I can import and use UNO just fine, and control Office from my Python script. However, many of my other modules are missing from that Python—and UNO is the only one missing from my Python.

Is there any way that I can install pyuno for my local Python? The LibreOffice source tree includes a pyuno/ source tree, but I'm not sure how to go about building/integrating this into another Python tree.

Any experiences here? Help? Hints? Dos, Don'ts, Dohs?

EDIT The answer below works just fine for Linux, and I have no problem there extending the PYTHONPATH to import uno. Matters are different on the Mac, so take a look at the other answer.

EDIT Absolutely take this anwer into consideration when tinkering with Python paths!

like image 298
Jens Avatar asked Mar 05 '13 11:03

Jens


2 Answers

Once you try to run PyUNO off any other python executable than the one provided with LO, things do get rough.

The SEGV on Mac is because LO's libpyuno.dylib (loaded via libuno.dylib, which in turn is loaded via "import uno") references @loader_path/LibreOfficePython.framework/Versions/3.3/LibreOfficePython (run "otool -L" on that file; path as on current LO master; paths are a little different on various LO versions). When run from a different python process than LO's, that means there'll be two python runtimes in the process (and the LO one not even properly initialized, probably), and that leads to a SEGV somewhere in that LibreOfficePython. (This happens to work better on Linux, where libpyuno.so references libpython3.3m.so, and normally finds the LO python one's next to itself via its RPATH, but if any libpython3.3m.so happens to already be loaded into the process (from the other python), the Linux loader happily re-uses that one.)

One gross hack on Mac is to use install_name_tool to "rewire" libpyuno.dylib to reference the other python's Python.framework/Versions/3.3/Python (by absolute path) instead of @loader_path/LibreOfficePython.framework/Versions/3.3/LibreOfficePython.

Another gotcha is that LO's python (on Linux and Mac) is actually a shell script around the true python executable. It needs to set up a number of env vars (whose purpose is even documented in the script). To make PyUNO work from a different python you'll want to set up these env vars too, esp. UNO_PATH, URE_BOOTSTRAP, and the parts of PYTHONPATH that find the LO-specific libs (rather than those that come with python itself). Note that the details of those env vars' values differ among LO versions.

like image 104
Stephan Bergmann Avatar answered Sep 24 '22 18:09

Stephan Bergmann


It is a late answer and I don't have the exact same setup as you have, but for me, I could simply adjust PYTHONPATH so that the directory where uno.py lives is known to python.

bash> export PYTHONPATH=${PYTHONPATH}:/usr/lib/libreoffice/program
bash> python
>>> import uno

A requirement is that your LibreOffice/OO python has the same version as your regular one: Python will compile the .py to .pyc, and that format is not transferable between versions (at least, that is not guaranteed).

Do a locate uno.py if you are not sure where your file is. Inspecting where /usr/bin/libreoffice links to may also help.

like image 45
dirkjot Avatar answered Sep 24 '22 18:09

dirkjot