Sublime Forum

Dynamically loading module st2/st3

#1

I’m trying to get my package to work with python 3.3 and I have an import statement that dynamically loads a module

__import__('dictionaries.lang', globals(), locals(), 'TAGS','FUNCTIONS','VARIABLES'])

with ST2 a relative module path is fine, but with ST3 I need to prefix my module name with my package name (eg: ‘mypackage.dictionaries.lang’).
I tried changing the levels for the import but they seem to behave differently in both versions.

Should I just test for the current ST version and change the module name accordingly?

Also it seems that sublime.load_settings() wont work in my constructor; the get() method returns None.
Is there an easier way to load my module using a string from my settings file?

0 Likes

#2

You can use the imp module with an absolute path to the python file you want to load. Like this:

parsehelp = imp.load_source("parsehelp", os.path.join(os.path.dirname(os.path.abspath(__file__)), "parsehelp/parsehelp.py"))
0 Likes

#3

Thanks quarnster.

Any advice on getting settings.load_settings to work? I’d like to use it to load my module.

0 Likes

#4

From this response from Jon: viewtopic.php?f=2&t=10780&start=40#p42444

The sublime module is not available from the top-level of your plugin. You’ll probably need to move your settings loading code into your plugin class somewhere.

0 Likes

#5

[quote=“sapphirehamster”]From this response from Jon: viewtopic.php?f=2&t=10780&start=40#p42444

The sublime module is not available from the top-level of your plugin. You’ll probably need to move your settings loading code into your plugin class somewhere.[/quote]

Thanks! I must have glossed over that.

0 Likes