Sublime Forum

Module loading

#1

Hi,

Im porting my plugin to ST3 and found that now my plugin does not get loaded during plugin initialization because it can find the required modules:
Traceback (most recent call last):
File “/Applications/Sublime Text.app/Contents/MacOS/sublime_plugin.py”, line 73, in reload_plugin
m = importlib.import_module(modulename)
File “X/importlib/init.py”, line 88, in import_module
File “”, line 1577, in _gcd_import
File “”, line 1558, in _find_and_load
File “”, line 1525, in _find_and_load_unlocked
File “”, line 586, in _check_name_wrapper
File “”, line 1023, in load_module
File “”, line 1004, in load_module
File “”, line 562, in module_for_loader_wrapper
File “”, line 869, in _load_module
File “”, line 313, in _call_with_frames_removed
File “/Users/alvaro/Library/Application Support/Sublime Text 3/Packages/Rule Editor/unittests.py”, line 35, in
from whoosh.index import create_in
ImportError: No module named ‘whoosh’

Im loading Whoosh from my plugin directory like:
def add_to_path(path):
if os.name == ‘nt’:
buf = create_unicode_buffer(512)
if windll.kernel32.GetShortPathNameW(path, buf, len(buf)):
path = buf.value

if path not in sys.path:
    sys.path.append(path)

common_lib_folder = os.path.join(sublime.packages_path(), ‘Rule Editor’, ‘lib’, ‘common’)
add_to_path(common_lib_folder)
from whoosh.index import create_in
from whoosh.fields import *

That used to work in ST2 but now is not working any longer. The thing is that if I open the plugin py file and save it (effectively reloading the plugin) the plugin just gets loaded correctly but not during initialization time.

The problem is that during initialization time, sublime.packages_path() is empty and after initialization it correctly points to the Packages folder.

Is this a bug in ST3 or is there any way to get the Packages folder during initialization time?

Thanks,
A

0 Likes

#2

Have you looked at the ST3 porting guide? sublimetext.com/docs/3/porting_guide.html

Specifically,

Also take note of the restricted api usage at start up. The only API methods that work prior to plugin loaded are sublime.version(), sublime.platform(), sublime.architecture() and sublime.channel(). You may need to define a module level function “plugin_loaded()” to match some of the behavior you had in ST2.

0 Likes

#3

Thanks! it works!

Now I have a last problem. When opening a new XML file I want to apply a given syntax depending on the first characters of the XML file.

def on_load(self, view):
    if view.match_selector(0, "text.xml") and (view.find('<RulePack', 0) or view.find('<Rules', 0)):
        view.set_syntax_file(os.path.join(str(sublime.packages_path()), 'RuleEditor', 'RulePack.tmLanguage'))

Error loading syntax file “/Users/alvaro/Library/Application Support/Sublime Text 3/Packages/RuleEditor/RulePack.tmLanguage”: Unable to open /Users/alvaro/Library/Application Support/Sublime Text 3/Packages/RuleEditor/RulePack.tmLanguage

The file exists and is readable. It used to work with ST2.
Also setting the RulePack syntax manually through the command palette works.

Any idea?

Thanks,
A.

0 Likes

#4

From the docs set_syntax_file method.

I know the ST2 api docs say the same thing, so maybe jps corrected this so it behaves as it is described. In any case, you don’t want to use absolute paths when referencing resources in ST3. Unlike ST2, ST3 packages do not need to be extracted to the packages folder to run. Using an absolute path will cause things to break if the plugin is installed as a sublime-package. Of course, this may be another issue, but probably worth fixing that first, then seeing if the error still exists.

0 Likes