Sublime Forum

Package Control: Trouble with internal imports in a dependency

#1

For a new version of my YAML Macros package, I’m looking to use ruamel.yaml instead of pyyaml (because pyyaml lacks ruamel’s round-trip capability). Rather than bundling ruamel.yaml with YAML Macros, I’d like to add it as a dependency.

My best understanding is that the structure of the ruamel.yaml dependency should look basically like this:

ruamel/
    st3/
        yaml/
            <ruamel.yaml contents here, including __init__.py>

However, the python files that comprise ruamel.yaml all import ruamel.yaml.<thing>. As a result, when using the above structure my own YAML Macros package can try to import ruamel.yaml.<thing>, but the ruamel.yaml package itself can’t resolve its own imports.

I realize that I could rectify this problem by rewriting the imports in the ruamel.yaml package, but I’m hoping that there is a less drastic solution that doesn’t complicate updating the ruamel.yaml version.

1 Like

#2

This is based on my (albeit limited) understanding of how dependencies work in PackageControl, but in general I think the reason your imports aren’t working is because the structure of the dependency should be something like ruamel/st3/ruamel/yaml.

Or if you will, you make a folder named after the ultimate dependency (just to make life on everyone easier, but it could be anything, I think), inside that you use all if it works for everything, st3 for Sublime 3 only and so on, and then inside of THAT directory the complete contents of the standard dependency.

Basically, if you open the Sublime console and do import sys; sys.path you see that the Python interpreter’s path for looking for modules contains a few core things, the overall Packages directory, and then your Packages/ruamel/st3 directory and similar directories for other dependencies that may be installed.

That means that if you do import ruamel.yaml it should be pulling it from the inner location and life is good.

3 Likes

#3

I’ve set up the ruamel directory as you indicate, placed it inside Packages/, and added ruamel to my package’s dependency list, but I get an ImportError: No module named ‘ruamel.yaml’. The ruamel/st3 directory is not in sys.path.

Does the dependency need to actually be added to Package Control to show up in sys.path?

0 Likes

#4

You can develop it locally without adding it to PC first, but PC needs to know that it’s a dependency for it to do the setup for you.

Do do that you add a .sublime-dependency file at the root of your package whose contents is a single line with a 2 digit number that specifies the relative load order (e.g. 01 to be first or 50 to be last, etc). You can choose a lower number if the dependency has no dependencies, or a higher one if you need to be sure something else is loaded first, etc.

Once that’s done, you select the Package Control: Install Local Dependency command from the palette and it should show you a list of local dependencies that aren’t installed yet, which should just be your own package. Select it there and it will update everything and you should be good to go.

If your dependency doesn’t show up in the list of local dependencies to install, you may need to restart Sublime after you add the file so that PC notices. In that case you should see information in the console at startup telling you that it’s installing a missing dependency, followed by saying it can’t find it.

Once you perform these steps you may need to restart Sublime one more time to get it fully set up.

What this is doing is that PC is adding a file named ##-yourdependency.py to the Installed Packages/0_package_control_loader.sublime-package file, where ## is the 2 digit number you selected above. That generated file is what sets up sys.path.

When Sublime loads packages at startup, it loads them in a definite order; always in sorted (lexical) order, first shipped packages, then installed packages, then unpacked packages. So 0_package_control_loader.sublime-package is the first non-shipped package that gets loaded, which sets everything up for other installed packages.

2 Likes

#5

That sorted it out — I wasn’t aware of the Package Control: Install Local Dependency command. Thanks.

1 Like