[Tutorial] Import uno module to a different Python install

Home made tutorials, by users, for users
Forum rules
No question in this forum please
For any question related to a topic, create a new thread in the relevant section.

[Tutorial] Import uno module to a different Python install

Postby Ian Weisser » Sat Dec 04, 2010 5:29 am

This question comes up a lot on win32 machines.
This tutorial is for Windows. Python and OO integration is slightly different in Linux.

How to get pyuno to work with a non-OpenOffice-installed version of Python.
Without editing system variables.
Without editing config files.
Without breaking anything.


Terms:
Let's call the version of Python installed with OpenOffice the 'OO-Python'.
Let's call the version of Python installed separately (to c:\Python26, for example) the 'System-Python'.


You need to know SIX things to sucessfully import the uno module into your System-Python interpreter.
1) The OO-Python version number. Your System-Python version number must be the same as your OO-Python version number.
    > c:\Program Files\OpenOffice.org 3\program\python.exe -V
    This will tell you the python version number (for example, Python version 2.6.1 is included in OO 3.1)
    So this tutorial won't work for Python 2.7 or 3.1 with OO 3.1.
    You can use other versions of Python with Openoffice - see viewtopic.php?f=45&t=36608&p=167909#p167909 for a way to use the OO API using the COM bridge instead of the pyuno bridge.
2) You need to know where to start the OO-Python version:
    c:\Program Files\OpenOffice.org 3\program\python.exe
3) You need to copy OO-Python's URE_BOOTSTRAP environment variable into your System-Python
    os.environ['URE_BOOTSTRAP']
4) You need to copy OO-Python's UNO_PATH environment variable into your System-Python
    os.environ['UNO_PATH']
5) You need to copy OO-Python's PATH environment variable into your System-Python
    os.environ['PATH']
6) You need to add the python-uno location to your System-Python's PYTHONPATH, so your system's python interpreter can find uno.
    sys.path.append('C:\\Program Files\\OpenOffice.org 3\\Basis\\program')


How to get the environment variables from OO-Python:
Open a cmd window: Start -> Run -> cmd <enter>
Copy-and-paste the following commands, and copy the results
Code: Select all   Expand viewCollapse view
>c:\Program Files\OpenOffice.org 3\program\python.exe       #2 - Open the OO version of python.
>>>import os

>>>print(os.environ['URE_BOOTSTRAP'])                              #3
vnd.sun.star.pathname:c:\Program Files\OpenOffice.org 3\program\fundamental.ini

>>>print(os.environ['UNO_PATH'])                                       #4
c:\Program Files\OpenOffice.org 3\program\

>>>print(os.environ['PATH'])                                               #5
c:\Program Files\OpenOffice.org 3\\URE\bin;c:\Program Files\OpenOffice.org 3\Basis\program;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem

Or you can use this handy single command:
Code: Select all   Expand viewCollapse view
>c:\"Program Files"\"OpenOffice.org 3"\program\python.exe "-cimport os ;print(os.environ['URE_BOOTSTRAP']) ;print(os.environ['UNO_PATH']) ;print(os.environ['PATH'])"



How to add the environment variables to System-Python:
You can add the environment variables to Windows (My Computer -> Properties -> Advanced -> Environment Variables) so all System-Python scripts can take advantage of them. Don't forget to document what you did, so it's maintainable when the paths change due to an OO upgrade or radical change to Python.

Or you can simply add them to your python script. This has the advantages of changing no system settings at all, and self-contained simplicity, but the disadvantage of spreading the maintenance work among many scripts.

Or you can create a library function based on the below uno-import-script that all your uno-scripts import. This also changes no system settings, and keeps maintenance in one place...but now we are getting complicated with an extra file.

Here is an example of adding them to your System-Python script:
Code: Select all   Expand viewCollapse view
import os
import sys

def import_uno():
    # Add the URE_BOOTSTRAP environment variable       #3
    os.environ['URE_BOOTSTRAP'] = 'vnd.sun.star.pathname:c:\Program Files\OpenOffice.org 3\program\\fundamental.ini'

    # Add the UNO_PATH environment variable               #4
    os.environ['UNO_PATH'] = 'c:\Program Files\OpenOffice.org 3\program\\'

    # Add the PATH environment variable, but weed the duplicates first       #5
    new_paths_string = 'c:\Program Files\OpenOffice.org 3\\URE\\bin;c:\Program Files\OpenOffice.org 3\Basis\program;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\\Wbem;C:\Program Files\Common Files\Intuit\QBPOSSDKRuntime'
    new_paths = new_paths_string.split(';')
    existing_paths = os.environ['PATH'].split(';')
    for path in new_paths:
        if path not in existing_paths:
            existing_paths.append(path)
    os.environ['PATH'] = ';'.join(existing_paths)

    # Add the uno location to PYTHONPATH         #6
    sys.path.append('C:\\Program Files\\OpenOffice.org 3\\Basis\\program')

    return

# Begin Main
import_uno()
import uno
do_more_of_my_work()
do_the_rest_of_my_work()
# End Main
This particular script is awful - the environment variables and paths are all hard-coded instead of pulled from the OO-Python dynamically. So it might break upon OO upgrade. But the point is that it shows how to apply to System-Python the environment variables you copied from OO-Python. See the improved version below:


But wait! Can't it be automated?
Of course it can. Here is an example of a more advanced script:
Code: Select all   Expand viewCollapse view
import os
import sys
import subprocess

def import_uno():

    # Variables
    python_oo_executable = 'c:\\Program Files\\OpenOffice.org 3\\program\\python.exe'    #2
    python_oo_script = '-cimport os ;print(os.environ["URE_BOOTSTRAP"]) ;print(os.environ["UNO_PATH"]) ;print(os.environ["PATH"])'                    #3, #4, #5
    path_to_uno = 'C:\\Program Files\\OpenOffice.org 3\\Basis\\program'        #6

    # Get the environment variables from OO-Python using subprocess
    process = subprocess.Popen([python_oo_executable, python_oo_script], stdout=subprocess.PIPE)
    result = process.communicate()
    environment_variables = result[0].split('\r\n')   # Three items in the list, one for each env var

    os.environ['URE_BOOTSTRAP'] = environment_variables[0]          #3
    os.environ['UNO_PATH'] = environment_variables[1]                   #4

    new_paths = environment_variables[2].split(';')
    existing_paths = os.environ['PATH'].split(';')
    for path in new_paths:
        if path not in existing_paths:
            existing_paths.append(path)
    os.environ['PATH'] = ';'.join(existing_paths)       #5

    sys.path.append(path_to_uno)                        #6

    return

# Begin Main
import_uno()
import uno
do_more_of_my_work()
do_the_rest_of_my_work()
# End Main
This is a bit better. Now Sytem-Python gets the environment variables from OO-Python directly instead of me copy-and-pasting. There is a slight performance cost - this function takes about two seconds to run and sucessfully import uno. The hardcoded-strings function (above) is less flexible, but much faster.

Special thanks to the contributors who solved http://user.services.openoffice.org/en/forum/viewtopic.php?f=45&t=26149. This post is just an expansion and clarification of their work.
OpenOffice 3.2.1 on Ubuntu 10.10 / LibreOffice Beta on Mac OS 10.5 / OpenOffice 3.2.1 Windows XP
Ian Weisser
 
Posts: 5
Joined: Tue Nov 30, 2010 5:05 am

Re: [Tutorial] Import uno module to a different Python insta

Postby ajaxaddicted » Wed Jun 29, 2011 12:42 pm

The tutorial doesn't apply for Ubuntu in my experience. What worked for me was:


Code: Select all   Expand viewCollapse view
# uno.py is in /usr/share/pyshared
import sys
sys.path.append('/usr/share/pyshared')
LibreOffice 3.3 Ubuntu 11.04
ajaxaddicted
 
Posts: 1
Joined: Wed Jun 29, 2011 12:34 pm

Re: [Tutorial] Import uno module to a different Python insta

Postby Alessio1 » Fri Dec 07, 2012 9:09 am

this code (first post) is not working on my window vista home basic . so please let me know what i do ???????????????
OpenOffice 3.1 on Windows Vista
Alessio1
 
Posts: 1
Joined: Fri Dec 07, 2012 9:03 am


Return to Tutorials

Who is online

Users browsing this forum: No registered users and 7 guests