Sublime automatically loads all Python files that are loose in the Packages
directory and outside of a package (e.g. Packages\sample.py
) as well as all Python files that are directly inside of a package at the top level of the package.
That is to say, it would automatically load Packages\User\sample.py
but not Packages\User\something\sample.py
. Such files can still be loaded, but you need to do so explicitly with an import
statement in one of the files that Sublime automatically loads.
This is to allow you full control over what files get loaded, so that you could for example ship a set of files that are specific to certain platforms and then only load the ones that are currently relevant by detecting the platform and importing the one you require.
Generally speaking, this is due to the scoping rules of Python itself. If you have two files, a.py
and b.py
and both of them contain for example a class with the same name, the contents of that file will refer to the version of the class from the file that it’s in unless you tell python differently.
import sublime_plugin
class SampleClass():
value = "I am inside file A"
class TestOneCommand(sublime_plugin.TextCommand):
def run(self,edit):
self.view.insert(edit, 0, SampleClass.value)
import sublime_plugin
class SampleClass():
value = "I am inside file B"
class TestTwoCommand(sublime_plugin.TextCommand):
def run(self,edit):
self.view.insert(edit, 0, SampleClass.value)
Here even though both files contain a class named SampleClass
, the two commands defined (test_one
and test_two
) will refer to the version that’s in their own file.
In the context of Sublime, this does not carry through to classes that subclass from special Sublime classes such as ApplicationCommand
, WindowCommand
or TextCommand
. For those classes, Sublime is doing its own processing after all of the files are loaded: finding the classes, creating instances of them, and naming the resulting commands based on the class name.
This means that if both files above contained e.g. TestOneCommand
, then as Sublime is processing things the first class of this name becomes the command test_one
. As Sublime proceeds through the list, it finds the second class which also becomes the command test_one
, and now the first one is effectively hidden.
This is decidedly Bad Mojo and is why you should rename those sort of special classes when you’re duplicating code.