Sublime Forum

Invoking PILLOW (and other 3rd party libraries) from within a ST plugin

#1

I am writing a plugin to (amongst other things) convert MD to HTML, break this HTML up into separate files based on heading structures, and download any linked images.

This is all working.

I know that ST plugins use an older Python kernel, but I have successfully imported BeautifulSoup into my plugin, for HTML traversing and DOM manipulation.
This I achieved using a dependencies.json file:
{ "*": { "*": [ "bs4"] } }

I now want to extend this to dynamically resize any overly large image files (eg anything beyond 1024px wide), using the PILLOW library.

from PIL import Image

Despite my best efforts, I cannot get Pillow to become available to my plugin.
I continually get the error:
ImportError: No module named 'PIL'

This is despite extending the dependencies.json file, like so:
{ "*": { "*": [ "bs4","PIL"] } }

Any help gratefully received

0 Likes

#2

Sublime Text 4 ships python 3.3 plugin_host for backward compatibility, only. New plugins should however use python 3.8 plugin_host by adding a .python-version file with content 3.8 to their root directory.

In order to handle python 3.8 dependencies/libraries Package Control 4 is required.

It can install ordinary python WHEELs, if they are registered to an available “channel”. Currently, https://github.com/packagecontrol/channel is the only semi-official channel, supporting python 3.8 libraries.

Pillow was added to that channel via https://github.com/packagecontrol/channel/pull/22 for python 3.8 plugins.

Dependencies are still specified via dependencies.json.

{ "*": { "*": [ "bs4", "pillow", "soupsieve"] } }

Note, Package Control can’t yet recursively resolve dependencies. Any dependency required (e.g. “soupsieve” for “bs4”) must also be registered to the channel and explicitly specified via dependencies.json.


Alternatively, for private plugins you can also use pip to install any python 3.8 package to ST’s $data/Lib/python38/ folder via

pip install <name> --python 3.8 --upgrade --isolated --disable-pip-version-check --only-binary :all: --no-compile --target $data\Lib\python38
  • <name>: The package to install
  • $data: The folder of ST’s profile folder
    • Windows: %APPDATA%\Sublime Text
    • Linux: ~/.config/sublime-text
    • MacOS: ~/Library/Application\ Support/Sublime\ Text
2 Likes

#3

Wow. Thankyou @deathaxe for that super helpful and speedy response. I am a bit of a newbie to Python and so spend hours trying and failing. This has really helped me to understand things better.

I will implement your suggestions and get back to let you know how I go.

Thanks again - you’re a lifesaver

1 Like