Importing custom modules in scripts for plugin manager

My scripts are available through the plugin manager.

I have a custom module that resides in the root of my script folder, in the folder jkGlyphsScripts. I can import from that module in my scripts that are in different subfolders locally without problem e.g.:

from jkGlyphsScripts.forSelected import forSelectedLayers
from jkGlyphsScripts.hintingPS.ocd import fix_hint_directions

forSelectedLayers(Glyphs.font, fix_hint_directions)

… but when the scripts are installed through the plugin manager, they are in a subfolder of Glyphs’ scripts folder, and the import doesn’t find the module anymore. Is there a solution to make this work?

Not tested in your case, but I use sth. like this to dynamically add the script’s location to the sys.path in a unit test script of mine. Maybe you could tweak it to make it work?

from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parent.parent))

from import MFFooBar as App # Foo is the folder, bar is the python file in there

Thanks! My concern with this method would be that the path would have to be added each time Glyphs was started?

I’ve solved this in a different way: I’ve moved the imported module to a separate GitHub repo, and set it as a dependency for my scripts in the plugin manager.

Well, every time the script runs, no? And you could check and pass if it’s already prepended, if that would be a problem.

Your solution sounds good. Seems to be the proper way :saluting_face:

Yeah, that kind of defeats the purpose of using the module in the first place, to make the user-facing scripts shorter and simpler :slight_smile:

I saw code that would check the path several times already and it seems to be ok. Adding the module makes sense if there might like to use it, too, otherwise use relative imports or the sys.path.