Page 1 of 1
[Python] Import module from other extension
Posted: Tue Oct 12, 2021 2:17 pm
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.
Re: [Python] Import module from other extension
Posted: Tue Oct 12, 2021 2:54 pm
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.
Re: [Python] Import module from other extension
Posted: Tue Oct 12, 2021 3:28 pm
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.
Re: [Python] Import module from other extension
Posted: Tue Oct 12, 2021 5:26 pm
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.
Re: [Python] Import module from other extension
Posted: Tue Oct 12, 2021 9:03 pm
by j.kornelsn
Re: [Python] Import module from other extension
Posted: Thu Oct 14, 2021 5:21 pm
by Bastien
Indeed, this topic is to be pursued in the asklibreoffice.org forum
