⇦ Back

Choose your OS:

macOS

Tested on macOS 10.12 (Sierra) to macOS 12 (Monterey).

Running Python

As macOS comes with Python pre-installed, Sublime Text can usually run it out-of-the-box:

  • Using the Sublime Text Build System:
    • Open a Python script (a file with the “.py” extension) in Sublime Text then try to build it using either the menu (Tools > Build) or the keyboard shortcut (Cmd+B)
    • If a box appears in the lower part of the screen with text detailing information about your build, then Sublime Text has successfully run Python on your script
    • If no box appears and you see the words “No Build System” in the very bottom-left of the screen (in the bottom grey bar) then it hasn’t worked and you need to follow the “Creating a Python Build System” instructions below
  • Using SublimeREPL:
    • A Sublime Text package called SublimeREPL can be installed and used to run Python. You may prefer it to the Build System or you may not, it’s up to you.
    • This can be installed through Sublime Text’s Package Control:
      • If you do not have Package Control yet, first install it either from the menu (Tools > Install Package Control…) or from the Command Palette (Tools > Command Palette… [or use the keyboard shortcut Shift+Cmd+P], search for “Install Package Control” and press Enter)
      • Once you have Package Control installed, open the Command Palette again, search for “Package Control: Install Package”, hit Enter, search for “SublimeREPL” and hit Enter again
  • Using Terminus
    • Terminus is another Sublime Text package. It opens a terminal as a window in Sublime Text which, just like a regular terminal, can be used to run Python on a file.
    • Install Terminus from Package Control as detailed above: open the Command Palette either from the menu (Tools > Command Palette…) or via the keyboard shortcut (Shift+Cmd+P), search for “Package Control: Install Package”, Enter, search for “Terminus”, Enter again
    • It’s useful to create a custom key binding for Terminus: from the menu go Sublime Text > Preferences > Package Settings > Terminus > Key Bindings and on the right-hand side (Default (OSX).sublime-keymap) add the following to the JSON file (leave the final comma out if it’s the last key binding in the file):
    // Termius key binding
    { "keys": ["shift+alt+t"], "command": "terminus_open", "args": {"cwd": "${file_path:${folder}}"} },
    • Now, you can press Shift+Alt+T to get a terminal window which you can use to run Python like so:
    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:

Troubleshooting

When changing settings, always close-and-open Sublime Text and/or your terminal to let the changes take effect.

  • What version of Python is being used? And is it Python 2 or Python 3?
    • Check by running the following script:
    import platform
    print(platform.python_version())
    • If it’s a version of Python 2 you should definitely change it as that version is no longer supported
    • If you are using the build system it will be whatever Python version is specified in the relevant 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 system
    • If you are using SublimeREPL you will have to fiddle around with that package’s settings to change the Python version
    • If you are using Terminus it will be whatever version corresponds to the terminal command you are using, eg python3.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).
  • Which copy of Python?
    • If you have multiple copies of Python installed - eg one in /Library/Frameworks/ and another in /usr/local/bin/ - it will probably not be clear which one Sublime Text is using
    • Check by running this script:
    import sys
    print(sys.executable)
    • If you are using the build system then it will be using a terminal command in the background to run Python and the copy being used will be that associated with the command. Ensure you know which terminal command is being used by the build system (eg by creating your own build system) then you can edit which copy of Python this command is aliased to in a terminal settings file (~/.bash_profile, ~/.zprofile or ~/.zshrc)
    • If you are using SublimeREPL you will have to fiddle around with that package’s settings to change it
    • If you are using Terminus then, like the build system, you will be using a terminal command. Edit a terminal settings file to change which copy of Python your command is aliased to.
  • Bash or Z Shell?
    • From macOS 10.15 Catalina onwards, the default shell is Zsh (as opposed to Bash, which is what it was before). This is relevant because it will determine what your terminal settings file is (~/.zprofile or ~/.zshrc for Zsh, ~/.bash_profile for Bash).

Creating a Python Build System

To create a custom build file for Python 3.11:

  • Tools > Build System > New Build System…
  • Replace everything with the following:
{
    "cmd": ["python3.11", "-i", "-u", "$file"],
    "file_regex": "^[ ]File \"(...?)\", line ([0-9]*)",
    "selector": "source.python"
}
  • Save it as 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)
  • Open a Python file in Sublime Text and go Tools > Build With… (keyboard shortcut: Shift+Cmd+B) > Python 3.11
  • You should only have to do this once as the ‘build with’ command sets the default build system for a file type. From now on you should be able to just do Tools > Build (Ctrl+B)

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"
}

⇦ Back

Ubuntu

Ubuntu 22.04

Setup

  • Running Python files in Sublime Text usually works out-of-the-box:
    • Open a Python file (a file with a .py extension) in Sublime Text
    • From the menu bar, go to Tools > Build (or press Ctrl+B). This should run the Python script.
  • If Sublime Text has tried to run the script using a different language, go to Tools > Build System or Tools > Build With… and choose Python

Changing the Version of Python used by Sublime Text

  • The Sublime Text Python Build System works by running the 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:
    • Go to Tools > Build System > New Build System…
    • Replace all text with:
    {
        "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}\"",
            }
        ]
    }
    • Save it to the default location (which is ~/.config/sublime-text/Packages/User) with a meaningful name like “Python 3.11.sublime-build”
  • Now, go to Tools > Build With.. and select your new Build System (eg Python 3.11)

Using SublimeREPL

  • Install SublimeREPL via Package Control (Ctrl+Shift+P > search for “install package” > search for “SublimeREPL”)
  • Tools > SublimeREPL > Python > Python - RUN current file (you can optionally create a user key binding to do this via a keyboard shortcut)
  • Shift+Alt+8 creates a lower window which you can drag the Python output console to
  • Troubleshooting:
    • If the code runs fine but you get the following window:
    SublimeREPL: obtaining sane environment failed in getenv()
    Check console and 'getenv_command' setting
    WARN: Falling back to SublimeText environment
    • Open Preferences > Package Settings > SublimeREPL > Settings - User
    • Enter the following:
    {
      "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.

  • Firstly, make sure that LaTeX is installed and working outside of Sublime Text (eg by rendering a .tex file from the terminal or in a LaTeX editor like TeXstudio)
  • Secondly, check that the Sublime Text package “LaTeXTools” is installed and working by running a LaTeX file in Sublime Text
  • If that works, everything should be sorted by installing dvipng from the terminal with:
sudo apt-get install dvipng
  • If you get an error saying matplotlib.cbook.Locked.TimeoutError: LOCKERROR, delete your tex.cache folder before trying again:
rm /home/<username>/.cache/matplotlib/tex.cache

⇦ Back

Ubuntu 16.04, 18.04 and 20.04

Setup

  • Double check that your default Python is 3.x.x, not 2.x.x:
    • Check your Python version with:
    python --version
    • If it is Python 2, create an alias to overwrite this. First, open your .bashrc file:
    subl ~/.bashrc
    • Then add the following to the bottom:
    # the terminal command "python" should open python3, not python2
    alias python=python3
    • Save, close and restart the terminal before running python --version again. The Python version should now be 3.x.x.
  • Using the default Sublime Text build functionality:
    • This usually doesn’t work out-of-the-box
    • Go to Tools > Build System > New Build System…
    • Replace all text with:
    {
        "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}\"",
            }
        ]
    }
    • Save it to the default location (which is ~/.config/sublime-text/Packages/User) with a meaningful name like “python3.sublime-build”
  • Using SublimeREPL:
    • Install SublimeREPL via Package Control (Ctrl+Shift+P > search for “install package” > search for “SublimeREPL”)
    • Tools > SublimeREPL > Python > Python - RUN current file (you can optionally create a user key binding to do this via a keyboard shortcut)
    • Shift+Alt+8 creates a lower window which you can drag the Python output console to
    • Troubleshooting:
      • If the code runs fine but you get the following window:
      SublimeREPL: obtaining sane environment failed in getenv()
      Check console and 'getenv_command' setting
      WARN: Falling back to SublimeText environment
      • Open Preferences > Package Settings > SublimeREPL > Settings - User
      • Enter the following:
          {
            "getenv_command": false
          }
  • Python files can also be run ‘manually’ from a Terminus terminal, look in 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.

  • Firstly, make sure that LaTeX is installed and working outside of Sublime Text (eg by rendering a .tex file from the terminal or in a LaTeX editor like TeXstudio)
  • Secondly, check that the Sublime Text package “LaTeXTools” is installed and working by running a LaTeX file in Sublime Text
  • If that works, everything should be sorted by installing dvipng from the terminal with:
sudo apt-get install dvipng
  • If you get an error saying matplotlib.cbook.Locked.TimeoutError: LOCKERROR, delete your tex.cache folder before trying again:
rm /home/<username>/.cache/matplotlib/tex.cache

⇦ Back

Windows

On Windows 7, 8 and 10:

  • Make sure you have Python 3 installed on your computer (Sublime Text is just a front end; it doesn’t do the compiling itself!)
    • IDLE is ok, not Canopy
    • Check by running python --version from the Command Prompt; it should return the Python version you have as opposed to giving an error
  • Test that SublimeREPL is working:
    • Open a .py file or create a new one and save it
    • In Sublime Text: Tools > SublimeREPL > Python > Python - RUN current file
    • This should (a) run without a problem and (b) show that you are running Python 3, not Python 2
  • If you ARE running Python 2, change it:
    • Tools > Build System > New Build System…
    • Replace all text with the following (but choose the right path to your Python executable!):
    {
        "cmd": ["C:/Users/<username>/AppData/Local/Programs/Python/Python36-32/python.exe", "-u", "$file"],
        "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
        "selector": "source.python"
    }
    • Save it to the default location with a meaningful name like “python3.sublime-build”
    • Test that it works with Tools > Build System > python3, then Tools > Build
  • You might need to change the Environment Variable Path:
    • Windows 7: Start > right-click on Computer > Properties > Advanced system settings > Environment Variables…
    • Windows 10: Search “environ” from the start button > Edit the system environment variables > Environment Variables…
    • Append the following to the top one (Path):
    ;C:/Users/<username>/AppData/Local/Programs/Python/Python36-32/
  • Shift+Alt+8 creates a lower window to which you can drag the Python output console

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.

  • Delete the matplotlib caches. Run the following in Python, see what folder path is outputted to the console, navigate there and delete everything that is inside it:
import matplotlib as mpl
print(mpl.get_configdir())
  • After this is done, try to run the code again
  • It might say 'latex' is not recognized as an internal or external command, operable program or batch file. If this is the case:
    • Open Preferences > Package Settings > SublimeREPL > Settings - User
    • Change the PATH to the following:
    "default_extend_env": {"PATH":"C\\Users\\<username>\\AppData\\Local\\Programs\\Python\\Python36-32\\;C:\\Program Files (x86)\\MiKTeX 2.9\\miktex\\bin"},

⇦ Back