[Python] Import module from other extension

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
Bastien
Posts: 61
Joined: Mon May 12, 2014 2:45 pm

[Python] Import module from other extension

Post by Bastien »

Hi,

I made an extension my_utils as an uno_package.

In a second extension I need some of the functions I did in the previous package. Is there a way to import module from the previous pakcage my_utils ?

For now, I tried to play with pythonloader or pythonscript looking for a way to list available with no concrete result.
Last edited by Bastien on Thu Oct 14, 2021 5:22 pm, edited 1 time in total.
LibreOffice 6.2.3.2 on Ubuntu 19.04
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: [Python] Import module from other extension

Post by Villeroy »

Have a look at the StarBasic code delivered with MRI.

Code: Select all

  oMRI = CreateUnoService( "mytools.Mri" )
  oMRI.inspect( MriTargetObject )
You can do the same with Python:

Code: Select all

ctx =  uno.getComponentContext
smgr = ctx.ServiceManager
oMRI = smgr.createInstance( "mytools.Mri" )
oMRI.inspect( MriTargetObject )
If your extension is just some macro code not providing any UNO service,
<user_profile>/Scripts/python/pythonpath/ is the path where macros can find user defined modules. You may experiment with symlinks in that directory pointing to your script file.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Bastien
Posts: 61
Joined: Mon May 12, 2014 2:45 pm

Re: [Python] Import module from other extension

Post by Bastien »

So I followed your advice and indeed, seems I get closer ! I created extensions, so I don't use the
<user_profile>/Scripts/python/pythonpath/.

Code: Select all

ctx = uno.getComponentContext()
smgr = ctx.ServiceManager
oBal = smgr.createInstance('com.rdt.comp.Bal')
There is no method inspect() in oBal. It's a pyuno object which not seems to contains macros from the main module (called rdt_bal.py) in the package.
LibreOffice 6.2.3.2 on Ubuntu 19.04
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: [Python] Import module from other extension

Post by Villeroy »

inspect(obj) is a function of the mri service.
Does your service include com.sun.star.task.Job with interface com.sun.star.task.XJobExecutor having method trigger?

Code: Select all

ctx = uno.getComponentContext()
smgr = ctx.ServiceManager
oBal = smgr.createInstance('com.rdt.comp.Bal')
oBal.trigger('')
Same in Basic

Code: Select all

oBal = CreateUnoService("com.rdt.comp.Bal")
oBal.trigger("")
The string argument (empty here) can be used to pass additional info about the caller.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
j.kornelsn
Posts: 14
Joined: Wed Oct 06, 2010 6:26 am

Re: [Python] Import module from other extension

Post by j.kornelsn »

LO / AOO on Ubuntu / Windows
Bastien
Posts: 61
Joined: Mon May 12, 2014 2:45 pm

Re: [Python] Import module from other extension

Post by Bastien »

Indeed, this topic is to be pursued in the asklibreoffice.org forum :-)
LibreOffice 6.2.3.2 on Ubuntu 19.04
Post Reply