Home Download Buy Blog Forum Support

ST3: Loading plugins from "Installed Packages" may fail ...

Re: ST3: Loading plugins from "Installed Packages" may fail ...

Postby FichteFoll on Sat Jan 11, 2014 5:44 am

I decided to take back on this since I could not get relative importing to work on my packages when updating to ST3.

This post contains a lot of (necessary or not) information for reproduction, including many tracebacks. Please take a look at the critical example 3: ST swallows runtime exceptions from imported files when importing, so you never actually know where the error is!

-----------------------


Everything that is following has been run on Windows 7x64 with a fresh Sublime Text Portable x64 Build 3059.

Note that TestPackage.sublime-package (pretty much any file that fails to load) always seems to remain opened by ST because I have to close it to delete the file.

  1. Example 1

    File Structure:
    Code: Select all
    Data/
    + Packages/
    | + TestPackage/
    | | + subpackage/
    | | | + test4.py
    | | + testsub.py
    + Installed Packages/
    | + TestPackage.sublime-package
    | | + test.py


    test.py:
    Code: Select all
    print("loading test in sublime-package")


    testsub.py:
    Code: Select all
    from .subpackage import test4

    print(test4.variable)


    test4.py:
    Code: Select all
    print("we subtesting now")

    variable = "test4"



    Console: (when restarting)

    Code: Select all
    reloading plugin TestPackage.test
    loading test in sublime-package
    reloading plugin TestPackage.testsub
    Traceback (most recent call last):
      File "C:\Program Files\Sublime Text 3 Portable Beta TEST\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 "C:\Program Files\Sublime Text 3 Portable Beta TEST\sublime_plugin.py", line 671, in load_module
        exec(compile(source, source_path, 'exec'), mod.__dict__)
      File "C:\Program Files\Sublime Text 3 Portable Beta TEST\Data\Packages\TestPackage\testsub.py", line 1, in <module>
        from .subpackage import test4
      File "<frozen importlib._bootstrap>", line 1558, in _find_and_load
      File "<frozen importlib._bootstrap>", line 1525, in _find_and_load_unlocked
      File "C:\Program Files\Sublime Text 3 Portable Beta TEST\sublime_plugin.py", line 671, in load_module
        exec(compile(source, source_path, 'exec'), mod.__dict__)
    UnboundLocalError: local variable 'source' referenced before assignment


    Expected behavior: No exception and "we subtesting now\ntest4" in the console.

    \
  2. Example 2 (more advanced but worked on 3045)

    File Structure:
    Code: Select all
    Data/
    + Packages/
    | + TestPackage/
    | | + test.py
    + Installed Packages/
    | + TestPackage.sublime-package
    | | + test.py


    test.py (in sublime-package):
    Code: Select all
    print("loading test in sublime-package")

    variable = "test.sublime-package"


    testsub.py (in Packages/TestPackage):
    Code: Select all
    print("before import")

    from . import test

    print(dir(test))

    print("this module: " + __file__)
    print("installed module: " + test.__file__)

    print("variable before: " + test.variable)
    test.variable = "changed!"
    print("variable now: " + test.variable)



    Console: (when restarting)

    Code: Select all
    reloading plugin TestPackage.test
    before import
    Traceback (most recent call last):
      File "C:\Program Files\Sublime Text 3 Portable Beta TEST\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 "C:\Program Files\Sublime Text 3 Portable Beta TEST\sublime_plugin.py", line 671, in load_module
        exec(compile(source, source_path, 'exec'), mod.__dict__)
      File "C:\Program Files\Sublime Text 3 Portable Beta TEST\Data\Packages\TestPackage\test.py", line 3, in <module>
        from . import test
    ImportError: cannot import name test


    Expected behavior: Same as mentioned above: viewtopic.php?f=3&t=12564#p50283

    \
  3. Example 3 (THIS IS CRITICAL)

    File Structure:
    Code: Select all
    Data/
    Data/
    + Packages/
    | + TestPackage/
    | | + sub/
    | | | + other_module.py
    | | + testsub.py


    testsub.py:
    Code: Select all
    from .sub import other_module

    print("loading test in sublime-package")

    variable = "test.sublime-package"


    other_module.py:
    Code: Select all
    print("importing other module")

    from doesntexist import doesntexisteither # ImportError!
    variable = "testing~"

    # print "SyntaxError"



    ----------------------------------

    Console: (when restarting)

    Code: Select all
    reloading plugin TestPackage.testsub
    importing other module
    Traceback (most recent call last):
      File "C:\Program Files\Sublime Text 3 Portable Beta TEST\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 "C:\Program Files\Sublime Text 3 Portable Beta TEST\Data\Packages\TestPackage\testsub.py", line 1, in <module>
        from .sub import other_module
    ImportError: cannot import name other_module



    Expected behavior: Show the actual exception from other_module:
    Code: Select all
    File "C:\Program Files\Sublime Text 3 Portable Beta TEST\Data\Packages\TestPackage\testsub.py", line 1, in <module>
        from .sub import other_module
    ImportError: cannot import name other_module


    Note how the ImportError from within other_module is swallowed. It took me hours to try to debug why my import just would be found until I suspected the actual exception is swallowed. I eventually stopped debugging for half a yeah (basically since my last post in this thread) and discovered this only just now.

    ------------------------------

    other_module.py (with SyntaxError):

    Code: Select all
    print("importing other module")

    # from doesntexist import doesntexisteither # ImportError!
    variable = "testing~"

    print "SyntaxError"


    The console looks like this:

    Code: Select all
    reloading plugin TestPackage.testsub
    Traceback (most recent call last):
      File "C:\Program Files\Sublime Text 3 Portable Beta TEST\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 "C:\Program Files\Sublime Text 3 Portable Beta TEST\Data\Packages\TestPackage\testsub.py", line 1, in <module>
        from .sub import other_module
      File "C:\Program Files\Sublime Text 3 Portable Beta TEST\Data\Packages\TestPackage\sub\other_module.py", line 6
        print "SyntaxError"
                          ^
    SyntaxError: invalid syntax


    This is likely because Syntax errors are detected at parse-time, not at runtime. I have to wrap the entire imported file in a try-except in order to find this.

    --------------------------------------

    other_module.py wrapped in try-except:

    Code: Select all
    try:
        print("importing other module")

        from doesntexist import doesntexisteither # ImportError!
        variable = "testing~"

        # print "SyntaxError"
    except:
        import traceback
        print("=" * 30)
        traceback.print_exc()
        print("=" * 30)
        raise


    Console: (after restart)

    Code: Select all
    reloading plugin TestPackage.testsub
    importing other module
    ==============================
    Traceback (most recent call last):
      File "C:\Program Files\Sublime Text 3 Portable Beta TEST\Data\Packages\TestPackage\sub\other_module.py", line 4, in <module>
        from doesntexist import doesntexisteither # ImportError!
    ImportError: No module named 'doesntexist'
    ==============================
    Traceback (most recent call last):
      File "C:\Program Files\Sublime Text 3 Portable Beta TEST\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 "C:\Program Files\Sublime Text 3 Portable Beta TEST\Data\Packages\TestPackage\testsub.py", line 1, in <module>
        from .sub import other_module
    ImportError: cannot import name other_module
Last edited by FichteFoll on Wed Mar 19, 2014 3:41 pm, edited 1 time in total.
FichteFoll
 
Posts: 402
Joined: Fri Mar 16, 2012 11:49 pm
Location: Germany

Re: ST3: Loading plugins from "Installed Packages" may fail ...

Postby kblomqvist on Sat Jan 25, 2014 4:38 pm

I have the same problem and I keep getting ImportErrors for .py files imported from the plugin folder

Code: Select all
ImportError: No module named 'foo'
kblomqvist
 
Posts: 17
Joined: Fri Nov 16, 2012 4:14 pm

Re: ST3: Loading plugins from "Installed Packages" may fail ...

Postby FichteFoll on Sat Jan 25, 2014 7:10 pm

kblomqvist wrote:I have the same problem and I keep getting ImportErrors for .py files imported from the plugin folder

Code: Select all
ImportError: No module named 'foo'


Since you are getting a different error message I suppose you are having a different issue. How can I reproduce your problem? Especially your project setup with files and what the import statement is.
FichteFoll
 
Posts: 402
Joined: Fri Mar 16, 2012 11:49 pm
Location: Germany

Re: ST3: Loading plugins from "Installed Packages" may fail ...

Postby FichteFoll on Wed Mar 19, 2014 4:04 pm

Example 3 has already been fixed in 3061 where the Python API was updated to 3.3 and fixed the upstream bug http://bugs.python.org/issue15111.

See also: https://github.com/SublimeText/Issues/i ... t-38028215
FichteFoll
 
Posts: 402
Joined: Fri Mar 16, 2012 11:49 pm
Location: Germany

Previous

Return to Technical Support

Who is online

Users browsing this forum: Google [Bot] and 23 guests