SOLVED External dependencies question

Hello,

I have a plugin that contain multiple subplugins (.pyp files) and want to know how to insert my lib modules to the system path and that affect all the plugin .pyp files. (I have already seen the post: External dependencies: The right way to do and I did not find an answer concerning this point )

lib\n    module1.py, module2.py, module3.py,...
res\n    <resource files>
plugin_1.pyp
plugin_2.pyp
plugin_3.pyp
...

Must I use the "sys.path.inser()" method on each of my subplugins?
I noticed that the subplugins is loaded one by one and in alphabetical order. so, for a test I created a new file and I rename it "a.pyp" (to be loaded in the first) and then I put inside it the following code:

import os
import sys
  
dirname = os.path.dirname(__file__)
lib_path = os.path.join(dirname, 'lib')
sys.path.insert(0, lib_path)

This it works, and affect all the .pyp files of my plugin.

Is there a right way to do that?

Thanks.

Here you go: https://github.com/NiklasRosenstein/py-localimport
That helper is also available as a minified version.

Hi @mfersaoui there is nothing wrong with the way you are doing, but I would like to point out that the sys.path is a global variable shared over the current python environment. Meaning that 3rd party will be able also to import your own module so it's recommended to have an ambiguous name (e.g. if your module1.py is called util.py) people will be able to do import util and this can be really misleading so it's preferred to have a unique name (maybe you can start with a prefix).

Finally, the solution offered by @mp5gosu is the cleanest one as it will create a local import space, so your util.py will only be importable by your own pyp file that actually calls the local import so it's so far the cleanest way as you are not "polluting" the global sys.path.

But which one is the best, only you can decide according to what you are aiming to produce.
Cheers,
Maxime.