Search code examples
pythonsublimetextsublime-text-plugin

Python import module check needed; due to different Python versions or the way Sublime Text imports packages?


In a Sublime Text plugin I've had to use a version dependant import which looks something like this:

python_version_major = version_info[0]

if python_version_major == 3:
    from .matching import whatever...
    from .paths import whatever...
elif python_version_major == 2:
    from matching import whatever...
    from paths import whatever...

Note: The difference between them is simply whether matching and paths need to be prefixed by a . or not.

Sublime Text v3 uses Python v3.3 while Sublime Text v2 uses Python v2.6.

The code works fine, allowing the plugin to work with both versions of Sublime Text, but I'd like to know whether the check is necessary due to the differences in the way Sublime Text v2 and v3 load packages or due to the different Python versions?

Thanks.


Solution

  • The use of dots as a directory/module prefix establishes that you are importing relative to the package directory.   A single dot will import from the package directory; and each additional dot will seek further into parent directories.

    If no dots are present, the import will be absolute and seek from sys.path.

    Implementation of this feature started in Python 2.4.   Info at Python Enhancement Proposals mentions issues up to and including Python 2.6.

     

    Reference:

    Python PEP 0328 -- Imports: Multi-Line and Absolute/Relative