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.
- Example 1
File Structure:
Data/
+ Packages/
| + TestPackage/
| | + subpackage/
| | | + test4.py
| | + testsub.py
+ Installed Packages/
| + TestPackage.sublime-package
| | + test.py[/code]
**test.py**
:
[code]print("loading test in sublime-package")
testsub.py:
[code]from .subpackage import test4
print(test4.variable)[/code]
test4.py:
[code]print(“we subtesting now”)
variable = “test4”
[/code]
Console: (when restarting)
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.
\
- Example 2 (more advanced but worked on 3045)
File Structure:
Data/
+ Packages/
| + TestPackage/
| | + test.py
+ Installed Packages/
| + TestPackage.sublime-package
| | + test.py[/code]
**test.py**
(in sublime-package):
[code]print("loading test in sublime-package")
variable = "test.sublime-package"
testsub.py (in Packages/TestPackage):
[code]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)
[/code]
Console: (when restarting)
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
\
- Example 3 (THIS IS CRITICAL)
File Structure:
Data/
Data/
+ Packages/
| + TestPackage/
| | + sub/
| | | + other_module.py
| | + testsub.py[/code]
**testsub.py**:
[code]from .sub import other_module
print("loading test in sublime-package")
variable = "test.sublime-package"
other_module.py:
[code]print(“importing other module”)
from doesntexist import doesntexisteither # ImportError!
variable = “testing~”
print “SyntaxError”
[/code]
Console: (when restarting)
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: 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]print(“importing other module”)
from doesntexist import doesntexisteither # ImportError!
variable = “testing~”
print “SyntaxError”
[/code]
The console looks like this:
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]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
[/code]
Console: (after restart)
[code]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
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 “”, 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 “C:\Program Files\Sublime Text 3 Portable Beta TEST\Data\Packages\TestPackage\testsub.py”, line 1, in
from .sub import other_module
ImportError: cannot import name other_module[/code]