What is the proper way we can define the python execution environment (default path/ virtual env / pipenv) for a plugin?
The python interpreter that runs the plugin code is already fixed by Sublime. If you type the following in Sublime’s console (by pressing ctrl/cmd + `) :-
>>> import sys >>> sys.version
you get an output similar to (as of Sublime Text 3 build 3211) :-
'3.3.6 (default, Mar 29 2018, 23:30:25) [MSC v.1600 64 bit (AMD64)]'
which means that all your plugin code will use python 3.3.6 as a default interpreter (pip is not supported). You cannot use the global python interpreter you have installed in your machine to execute plugin code. That’s because Sublime ships with some python modules (like
sublime_plugin) that the global interpreter can’t understand (& you can’t install it either since they ship with Sublime). Hence, there is no concept of a virtual environment while developing a plugin (as far as I know).
Similarly , if you want to use third party packages with plugins, that’s also a headache because you would have to manually install it in the
Lib directory and hope that you can import the package relatively. There are a few dependencies that the community has prepared for use with Sublime but that’s about all (which you can see here https://raw.githubusercontent.com/wbond/package_control_channel/master/repository/dependencies.json). These dependencies are made such that they will automatically be added to
sys.path by package control & you can import it normally.
Thank you for your quick and comprehensive answer. I may haven’t specified the question enough.
I’m looking for a good solution to execute external code, which could be in an virtual environment, as well as in the default python path. An example could be sublack, which depends on an external installation of black. For my scenario this should be configurable to a user specific path.
Is there any good practice to implement such a behavior to a sublime plugin?
If your external code is something that contains only modules that comes with the standard library of python 3.3.6, then you can use it in a plugin.
Sure, but the code is not coming with the standard library of python. Also it depends on the libraries installed by the user. In short I’m talking about improving the Hermes Plug In , which launches a Jupyter Kernel and executes code there. Therefore the path of the python environment is needed.
In that case, I am not entirely sure if that’s possible (since as you say it depends on user installed libraries, which I assume you mean pip install able ones).