Sublime text python modules
Plugins in Sublime Text are Python files located in the root of a package . The following document describes the Python environment the plugins are run in.
Overview🔗
Sublime Text runs plugins in a separate process from the main editor UI. This process runs an executable named plugin_host .
Running plugins in a separate process ensures the entire editor will not crash due to a poorly written plugin. If a plugin does cause the plugin_host to crash, a user may still save their work before re-starting Sublime Text.
All plugins are run in a single plugin_host process, and share a single Python environment. Each plugin is loaded as a sub-module of a module named after the package. For example, a plugin in the file MyPackage/my_plugin.py will be loaded as the Python module MyPackage.my_plugin .
The plugin_host process contains an embedded version of the Python programming language, and exposes an API to plugins. The plugin_host executable always uses its own embedded version of Python, even if the end-user has Python installed on their machine.
Python Version🔗
By default all plugins are run using Python 3.3.6, except inside the User package which always uses the latest python. Sublime Text‘s build of Python 3.3.6 includes a handful of patches backported from Python 3.4 to fix issues with unicode paths and crashes with the ctypes module on 64bit versions of Windows.
Starting in build 4050, plugins may also be run using Python 3.8. Python 3.8 features many improvements to the language, better performance and continued support and bug fixes from the Python Software Foundtion.
Selecting the Python Version🔗
To provide for backward compatibility, Sublime Text 4050 will continue to run all plugins using Python 3.3.
Any package that wishes to use Python 3.8 must create a file named .python-version in the root of the packages. This file should contain either the text 3.3 or 3.8 to select the version of Python to use. If a file named .python-version is not present, or it contains any value other than 3.8 , then Python 3.3 will be used.
All plugins in a package will use the same version of Python. Any package with a .python-version file containing 3.8 loaded in older builds of Sublime Text will try to run the plugins using Python 3.3.
Modules🔗
The Python environment within plugin_host contains all of the modules in The Python Standard Library, except for:
Pre-Installed Packages🔗
The following the packages are pre-installed in both the Python 3.3 and 3.8 environments:
How to Install Python Package in Sublime Editor
Learn to install python package in sublime editor for features like autocomplete and run build within sublime editor itself.
Table of Contents Install Sublime Package Control Install Package Python 3 Verify Python Autocomplete and Build
Install Sublime Package Control
First download the package control for sublime editor.
- Go to URL: https://packagecontrol.io/installation#st3
- Now note down the location of folder where packages are installed in sublime editor. You can find the location by clicking on Preferences > Browse Packages.
- Save the downloaded file from package control website on clicking “Package Control.sublime-package” link, and place it inside folder located at second step.
- Now restart the sublime editor by closing it and then open it again.
- To verify the package control is installed correctly, click on Preferences > Package Control menu item. It should open up the package control window.
- Now to install any package support, including Python package, go to Preferences > Package Control and choose Install Package.
- In opened window, type ‘python’ to filter the list of packages related to python only.
Wait for few seconds and python package will be installed into editor.
Verify Python Autocomplete and Build
To verify python support, again restart the IDE. Create a file with name demo.py . Type few simple commands like print . It should open the autocomplete window.
Now type simple hello world code, and enter CTRL + B in keyborad. It will open the output output window in bottom pane and will show the build output of the commands in the demo.py file.
Now you are ready to create and build python programs using sublime editor.
How can I import a Python module in a Sublime Text plugin?
I’m having trouble making a Sublime Text 3 plugin. I just installed wxPython with Python 2.7 on my Macintosh. In the terminal, my Mac can find wxPython ( import wx ). But the source code of a Sublime Text plugin cannot import wxPython. You can check out the screen capture below. How can I fix this problem?
1 Answer 1
Plugins are executed using Sublime’s internal Python interpreter, not any version of Python installed on your computer. Nearly all of the standard library is included, but a few packages (including Tkinter, among others) are not. To my knowledge it is not possible to use pip , for example, to install 3rd-party modules into Sublime Text.
However, if you would like to include some 3rd-party code, just put it in your plugin’s directory. For example, if you store your plugin code in Packages/MyPlugin (where Packages is the directory opened by selecting Preferences -> Browse Packages. ), and you want to include the 3rd-party library foobar , just copy the foobar directory into Packages/MyPlugin . Then, in your plugin code, use the following template, assuming you’re trying to code for both ST3 (Python 3.3) and ST2 (Python 2.6):
try: #ST3 from .foobar import mymodule except ImportError: #ST2 from foobar import mymodule
Obviously, if you’re just planning on supporting ST3 (there are enough differences in the API to make programming for both versions annoying), you won’t need the try / except clause. Also, if you are going to be distributing your plugin via Package Control or some other method, make sure you can redistribute the 3rd-party code, and that your license is compatible with its license.
Import Python Module into Sublime Text 3
I am trying to import Python modules (e.g. pandas or openpyxl) into Sublime Text 3. I installed the modules on the command line using pip3 install. If I try to import the modules into the command line, they work. For instance,
ModuleNotFoundError: No module named 'openpyxl'
It seems as if some work has to be done for sublime to respect PATH: stackoverflow.com/questions/8574919/…
As a first step I would verify that the build system you’re using to run your Python programs is using the correct version of Python (the default runs python and not python3 , for example).
3 Answers 3
It has been over a year since I asked this question myself. A few classes later into Command Line and Python I know ‘somewhat’ better what I am doing, and now came back to share the answer to this:
The problem here is that the versions of python that Sublime are using are not the same ones that my Command Line or my Jupyter Notebook are using. These 2 are using anaconda3/bin/python3. So the solution to this problem is to have Sublime use a Build that is pointing to this python (where the modules like openpyxl are installed).
On Sublime Text > Tools > Build System > New Build System.
and change «path_to_your_desired_python_version» to point to your desired version of python.
Now save the build you just created, for example «Python3.7X.sublime-build».
From now on, you can select that build in Tool > Build Systems.