Home Download Buy Blog Forum Support

Module loading

Module loading

Postby alvms on Sun May 26, 2013 9:24 pm

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 "<frozen importlib._bootstrap>", line 1577, in _gcd_import
File "<frozen importlib._bootstrap>", line 1558, in _find_and_load
File "<frozen importlib._bootstrap>", line 1525, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 586, in _check_name_wrapper
File "<frozen importlib._bootstrap>", line 1023, in load_module
File "<frozen importlib._bootstrap>", line 1004, in load_module
File "<frozen importlib._bootstrap>", line 562, in module_for_loader_wrapper
File "<frozen importlib._bootstrap>", line 869, in _load_module
File "<frozen importlib._bootstrap>", line 313, in _call_with_frames_removed
File "/Users/alvaro/Library/Application Support/Sublime Text 3/Packages/Rule Editor/unittests.py", line 35, in <module>
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
alvms
 
Posts: 26
Joined: Sun May 26, 2013 9:15 pm

Re: Module loading

Postby skuroda on Sun May 26, 2013 10:08 pm

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

Specifically,

Importing other Plugins is simpler and more robust in Sublime Text 3, and can be done with a regular import statement, e.g., import Default.comment will import Packages/Default/Comment.py


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.
skuroda
 
Posts: 676
Joined: Fri Oct 05, 2012 8:11 am

Re: Module loading

Postby alvms on Mon May 27, 2013 4:20 pm

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.
alvms
 
Posts: 26
Joined: Sun May 26, 2013 9:15 pm

Re: Module loading

Postby skuroda on Mon May 27, 2013 8:20 pm

From the docs set_syntax_file method.

Changes the syntax used by the view. syntax_file should be a name along the lines of Packages/Python/Python.tmLanguage.


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.
skuroda
 
Posts: 676
Joined: Fri Oct 05, 2012 8:11 am


Return to Plugin Development

Who is online

Users browsing this forum: No registered users and 6 guests