Tested on macOS 10.12 (Sierra) to macOS 12 (Monterey).
As macOS comes with Python pre-installed, Sublime Text can usually run it out-of-the-box:
// Termius key binding
{ "keys": ["shift+alt+t"], "command": "terminus_open", "args": {"cwd": "${file_path:${folder}}"} },
python3.11 <path to file>/<file name>.py
However, even though Python can now be used from within Sublime Text there are a couple of things to be aware of:
When changing settings, always close-and-open Sublime Text and/or your terminal to let the changes take effect.
import platform
print(platform.python_version())
sublime-build
file (which you may or may not have access to) so, if it isn’t using the version you want, see below about how to create a new build systempython3.11
will use 3.11 but python3
and python
will either use the computer’s default version or whatever version is set as an alias in your terminal’s settings file (eg ~/.bash_profile
, ~/.zprofile
or ~/.zshrc
). Either explicitly use the command that corresponds to the Python version you want to use or change the alias(es) in the settings file(s)./Library/Frameworks/
and another in /usr/local/bin/
- it will probably not be clear which one Sublime Text is usingimport sys
print(sys.executable)
~/.bash_profile
, ~/.zprofile
or ~/.zshrc
)~/.zprofile
or ~/.zshrc
for Zsh, ~/.bash_profile
for Bash).To create a custom build file for Python 3.11:
{
"cmd": ["python3.11", "-i", "-u", "$file"],
"file_regex": "^[ ]File \"(...?)\", line ([0-9]*)",
"selector": "source.python"
}
Python 3.11.sublime-build
in the default location (which will be /Users/<username>/Library/Application Support/Sublime Text 3/Packages/User
if you’re using Sublime Text 3 or initially installed Sublime Text 3 and have since upgraded to 4)For the more generic ‘Python 3’ (which will use a version of Python 3, not necessarily the latest one you have installed) you can use the following (for any other version of Python, just replace the python3
with the version you want - provided you have it installed, of course):
{
"cmd": ["python3", "-i", "-u", "$file"],
"file_regex": "^[ ]File \"(...?)\", line ([0-9]*)",
"selector": "source.python"
}
Setup
.py
extension) in Sublime TextChanging the Version of Python used by Sublime Text
python3
command on the file that is currently open. By default, this will be Python 3.10.6 because that is the version Ubuntu 22.04 comes pre-installed with. If you want to change that (eg if you have Python 3.11 installed and working and want Sublime Text to use that) you will need to create a new build system:
{
"shell_cmd": "python3.11 -u \"$file\"",
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
"env": {
"PYTHONIOENCODING": "utf-8",
// Manually set where a Python module looks when it uses shared
// object files or static libraries
"LD_LIBRARY_PATH": "/usr/local/lib"
},
"variants":
[
{
"name": "Syntax Check",
"shell_cmd": "python3.11 -m py_compile \"${file}\"",
}
]
}
~/.config/sublime-text/Packages/User
) with a meaningful name like “Python 3.11.sublime-build”Using SublimeREPL
SublimeREPL: obtaining sane environment failed in getenv()
Check console and 'getenv_command' setting
WARN: Falling back to SublimeText environment
{
"getenv_command": false
}
Using Terminus
Python files can also be run ‘manually’ from a Terminus terminal, look here for more info on installing that.
Troubleshooting: Use LaTeX format with Matplotlib
This is for when you are using the code plt.rc('text', usetex=True)
in an attempt to use LaTeX in Matplotlib plots but it’s causing them to not render or save. You will be seeing a very long error and/or an error saying that ‘dvipng’ could not be found.
sudo apt-get install dvipng
matplotlib.cbook.Locked.TimeoutError: LOCKERROR
, delete your tex.cache
folder before trying again:rm /home/<username>/.cache/matplotlib/tex.cache
Setup
python --version
.bashrc
file:subl ~/.bashrc
# the terminal command "python" should open python3, not python2
alias python=python3
python --version
again. The Python version should now be 3.x.x.{
"shell_cmd": "python3 -u \"$file\"",
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
"env": {
"PYTHONIOENCODING": "utf-8",
// Manually set where a Python module looks when it uses shared
// object files or static libraries
"LD_LIBRARY_PATH": "/usr/local/lib"
},
"variants":
[
{
"name": "Syntax Check",
"shell_cmd": "python3 -m py_compile \"${file}\"",
}
]
}
~/.config/sublime-text/Packages/User
) with a meaningful name like “python3.sublime-build”SublimeREPL: obtaining sane environment failed in getenv()
Check console and 'getenv_command' setting
WARN: Falling back to SublimeText environment
{
"getenv_command": false
}
Troubleshooting: Use LaTeX format with Matplotlib
This is for when you are using the code plt.rc('text', usetex=True)
in an attempt to use LaTeX in Matplotlib plots but it’s causing them to not render or save. You will be seeing a very long error and/or an error saying that ‘dvipng’ could not be found.
sudo apt-get install dvipng
matplotlib.cbook.Locked.TimeoutError: LOCKERROR
, delete your tex.cache
folder before trying again:rm /home/<username>/.cache/matplotlib/tex.cache
On Windows 7, 8 and 10:
python --version
from the Command Prompt; it should return the Python version you have as opposed to giving an error{
"cmd": ["C:/Users/<username>/AppData/Local/Programs/Python/Python36-32/python.exe", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
;C:/Users/<username>/AppData/Local/Programs/Python/Python36-32/
Troubleshooting: Use LaTeX format with Matplotlib
This is for when you are using the code plt.rc('text', usetex=True)
in an attempt to use LaTeX in Matplotlib plots but it’s causing them to not render or save. You will be seeing a very long error and/or an error saying that ‘dvipng’ could not be found.
import matplotlib as mpl
print(mpl.get_configdir())
'latex' is not recognized as an internal or external command, operable program or batch file.
If this is the case:
"default_extend_env": {"PATH":"C\\Users\\<username>\\AppData\\Local\\Programs\\Python\\Python36-32\\;C:\\Program Files (x86)\\MiKTeX 2.9\\miktex\\bin"},