Ask Your Question

Python - how to call function from another py file?

asked 2018-10-30 02:24:42 +0100

UserRyan gravatar image

I can't figure out how to separate my python macros into different files/modules. What I am trying to do is:

Have a LibreOffice button call: foo() and be able to call a function from another file (in the same directory) ->$foo (user, Python)

from bar import bar
def foo():

def bar():
    print("Why does this not work?")

I keep getting different variations of: Message: <class 'importerror'="">: No module named 'bar' Message: <class 'importerror'="">: attempted relative import with no known parent package (or '.bar' is unknown)

What I have tried: (They are in the same directory for all of these)

  • have the files in ~/.config/libreoffice/4/user/Scripts/python/
  • have the files in /opt/libreoffice6.1/share/Scripts/python/
  • All different types of from * import
  • add empty
  • make bar a class and import it as and object

Full error I am getting: File "/opt/libreoffice6.1/program/", line 1021, in getScript mod = self.provCtx.getModuleByUrl( fileUri ) File "/opt/libreoffice6.1/program/", line 458, in getModuleByUrl exec(code, entry.module.__dict__) File "~/.config/libreoffice/4/user/Scripts/python/", line 19, in <module> from . import bar File "/opt/libreoffice6.1/program/", line 425, in _uno_import raise uno_import_exc File "/opt/libreoffice6.1/program/", line 347, in _uno_import return _builtin_import(name, optargs, *kwargs)

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2018-10-30 05:47:47 +0100

Ratslinger gravatar image

updated 2018-10-30 06:07:57 +0100


You could add these lines:

import sys

before your 'bar' import (note this is a Linux directory) but the proper method is to add to pythonpath. See answer ( and links) in this answer -> import from second, embedded in document, python module

Your test seems to be from a button on a calc sheet. If so, foo needs *args as an argument. Also in bar, the print will be to the console only.

This may be a better test for you:

from bar import bar
def foo(*args):
    oDoc = XSCRIPTCONTEXT.getDocument()
    oSheet = oDoc.CurrentController.ActiveSheet
    oCell = oSheet.getCellRangeByName('B1')
    oCell.setString("Hello There!")
    oCell = oSheet.getCellRangeByName('B2')

def bar():
    my_str = "Hello Back 2 U!"
    return my_str

This will place text in cells B1 & B2 of the current sheet.

edit flag offensive delete link more


Worked like a charm. Thanks as always

UserRyan gravatar imageUserRyan ( 2018-10-30 19:30:07 +0100 )edit

It does not work when adding new functions to another file (bar) unless restarted. I'm assuming this is because the script is read at boot up so the import only takes the current functions. Is there any way to get the calc file to re-import other files/functions? reload() does not work

UserRyan gravatar imageUserRyan ( 2018-10-30 23:34:03 +0100 )edit

@UserRyan Don't recall off-hand a workaround for that issue. However, if you are going to be doing a lot of interactive testing, take a look at this post -> Christopher Bourez's blog .

Ratslinger gravatar imageRatslinger ( 2018-10-31 00:25:53 +0100 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2018-10-30 02:24:42 +0100

Seen: 988 times

Last updated: Oct 30 '18