Import from second, embedded in document, python module

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?

The best way to import modules is to add them to a pythonpath subfolder in the user or shared directories, as explained at Transfer from Basic to Python - Apache OpenOffice Wiki. 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.path to append an embedded directory and then import modules from there. See Alternative script organizer for Python macro (View topic) • Apache OpenOffice Community 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.

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.

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

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.

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!