[Python] Import module from other extension

Keyboard macros or custom scripts

[Python] Import module from other extension

Postby Bastien » Tue Oct 12, 2021 2:17 pm

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
Bastien
 
Posts: 61
Joined: Mon May 12, 2014 2:45 pm

Re: [Python] Import module from other extension

Postby Villeroy » Tue Oct 12, 2021 2:54 pm

Have a look at the StarBasic code delivered with MRI.
Code: Select all   Expand viewCollapse view
  oMRI = CreateUnoService( "mytools.Mri" )
  oMRI.inspect( MriTargetObject )

You can do the same with Python:
Code: Select all   Expand viewCollapse view
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
User avatar
Villeroy
Volunteer
 
Posts: 30156
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: [Python] Import module from other extension

Postby Bastien » Tue Oct 12, 2021 3:28 pm

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   Expand viewCollapse view
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
Bastien
 
Posts: 61
Joined: Mon May 12, 2014 2:45 pm

Re: [Python] Import module from other extension

Postby Villeroy » Tue Oct 12, 2021 5:26 pm

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   Expand viewCollapse view
ctx = uno.getComponentContext()
smgr = ctx.ServiceManager
oBal = smgr.createInstance('com.rdt.comp.Bal')
oBal.trigger('')

Same in Basic
Code: Select all   Expand viewCollapse view
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
User avatar
Villeroy
Volunteer
 
Posts: 30156
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: [Python] Import module from other extension

Postby j.kornelsn » Tue Oct 12, 2021 9:03 pm

LO / AOO on Ubuntu / Windows
j.kornelsn
 
Posts: 14
Joined: Wed Oct 06, 2010 6:26 am

Re: [Python] Import module from other extension

Postby Bastien » Thu Oct 14, 2021 5:21 pm

Indeed, this topic is to be pursued in the asklibreoffice.org forum :-)
LibreOffice 6.2.3.2 on Ubuntu 19.04
Bastien
 
Posts: 61
Joined: Mon May 12, 2014 2:45 pm


Return to OpenOffice Basic, Python, BeanShell, JavaScript

Who is online

Users browsing this forum: No registered users and 3 guests