Ask Your Question
1

import from second, embedded in document, python module

asked 2018-05-21 11:46:17 +0200

Xoristzatziki gravatar image

I have embedded in a LO document a python Library named TheEmbeddedLibrary with two python modules named: Modulewithfunctions and Modulewithclasses

If these were user or shared modules, importing from one to another is done by just issuing: from Modulewithclasses import myfirstclass (assuming they are in the same directory)

But when these modules are embedded in the document this no longer works.

Neither TheEmbeddedLibrary.Modulewithclasses works.

For the moment I included all in one big module, but is there any other way to import a class from that second module?

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
1

answered 2018-05-21 20:39:05 +0200

Jim K gravatar image

updated 2018-05-21 20:47:19 +0200

The best way to import modules is to add them to a pythonpath subfolder in the user or shareddirectories, as explained at https://wiki.openoffice.org/wiki/Pyth.... This also works in extensions.

If you create a pythonpath subfolder embedded in a document, it will not automatically be added to sys.path. It is possible to modify sys.pathto append an embedded directory and then import modules from there. See https://forum.openoffice.org/en/forum... for an example of how this would work.

However, you're probably better off combining the files into one big module. You could even write the code in separate modules and then write a script to combine them into a single large module before embedding.

edit flag offensive delete link more

Comments

Thanks for any answer. As for appending "." 1. this is not working in LO and 2. Is not needed in Python 3 (used by LO) outside LO. Now, using XSCRIPTCONTEXT we can directly import any script (BASIC, javascript etc.), so basically no need to pollute the pythonpath, and I 'd like to avoid it. But It seems that is unavoidable.

Xoristzatziki gravatar imageXoristzatziki ( 2018-05-22 06:50:06 +0200 )edit

No need for creating special subfolder. It already exists and is the name of the Library...

Xoristzatziki gravatar imageXoristzatziki ( 2018-05-22 07:06:15 +0200 )edit

I am not sure about the usefulness of creating a secondary script that must be checked, rechecked, double-checked, triple-checked etc. unless my source editor is not capable of handling big files. It is much easier and less error prune to create later (when and if solved) a splitter for splitting a python script in "known-marked" lines than creating a "joiner" that will combine an unknown number of different scripts which may accidentally have same name for different internal functions.

Xoristzatziki gravatar imageXoristzatziki ( 2018-05-23 16:39:37 +0200 )edit
0

answered 2019-03-07 15:13:09 +0200

updated 2019-03-12 08:54:19 +0200

Python Script Help (work in progress) extends from that same source to illustrate the import/cleanup of embedded modules. My take In order not to mess up the Python environment is to make sure you:

  • remove ALL imported modules at document closure
  • remove ALL appended libraries at document closure

Happy Python!

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2018-05-21 11:46:17 +0200

Seen: 397 times

Last updated: Mar 12