Sublime Forum

Sublime Package with Dependencies

#1

Hi all,

I’m working on my own Sublime (v4) package. I’m new to creating my own package and Python. I was pretty excited to get my package working with some simple functionality but to reach my end goal I need to import an external Python library which has its own dependencies.

I’m storing those third party dependencies in a folder within my package, named ‘vendor’.

After a good deal of research, it seems like I need to instruct Python to search an additional directory (this ‘vendor’ folder) for these dependencies. I do this by adding a sys.path. But where?

So my question is where do I give this instruction. Do I just set the additional sys.path on my package .py file (the one on the root of my package) or do I have to set it on every file that imports the dependencies?

Thanks for your time.

0 Likes

#2

It turns out you only need to instruct Python once. In the case of a Sublime package/plugin, you can place that code in the .py file which lives on the root of your package directory. Assuming you have a “vendor” folder where you need Python to look for modules, etc. so you can import them, your code might look something like this…

import pathlib

# Get path to vendor directory
vendor_dir = str(pathlib.Path(__file__).parent.resolve()) + '\\vendor'

# Tell Python to search vendor directory when importing dependencies 
sys.path.insert(0, vendor_dir)

I hope this helps others which may have been wondering the same thing.

0 Likes

#3

While manipulating sys.path basically works to import vendored packages from non-standard paths, it comes with a risk of conflict.

If multiple packages vendor the same package but with different (incompatible) versions, the first imported one is used for all other packages.

If possible, use relative imports to access vendored packages.

Commonly used python packages can be registered at https://github.com/packagecontrol/channel and referenced by creating a dependency.json in package root.

0 Likes

#4

@deathaxe, I appreciate your insight. Thanks!

0 Likes