Run Python 3 on Sublime Text (Mac)

It Only Takes a Minute

Wafiq Syed
Towards Data Science

--

Background Photo by Florian Olivo on Unsplash

Sublime Text is one of the most widely used lightweight text editors when it comes to programming. If you’re a Python programmer, you may not be running your preferred version of Python. This tutorial explains how to get Sublime Text running Python 3.7.

If you prefer a video tutorial, here’s my accompanying YouTube video:

Check Which Version Your Sublime Text is Using

Create a new Python file. I called mine scratch.py. It’s important that you save your file as a .py extension before running Python code. Otherwise, you won’t be able to execute Python code. Also, it’s always nice to have a scratch Python file to run quick code.

Now type the following code:

import sys
print(sys.version)

To run the code, press Command B or go to Tools -> Build.

As you can see, my Sublime Text is running Python 2.7. To change this to Python 3.7, we have to add a “Build System.

Add Python 3 as a Build System

Go to Tools -> Build System -> New Build System..

Notice, in my list of Build Systems, I have both Python and Python 3. Python comes automatically and runs 2.7. Python3 is what we’re going to add.

Once you click New Build System… you’ll be brought to a new window called untitled.sublime-build.

We need to change the text between the curly brackets. First delete the code between the brackets. Next copy and paste the following code:

"cmd": ["python3", "-i", "-u", "$file"],     
"file_regex": "^[ ]File \"(...?)\", line ([0-9]*)",
"selector": "source.python"

You might have to press Tab to indent the second and third lines. Now save the file by pressing Command S. Rename the file to what you’d like to call your Build System. Something short and clear to understand. I named mine Python3. Note, don’t change the extension, it must be .sublime-build. And don’t change the path either. Sublime Text will put it in the right path by default.

Once you click save, close the file so that you’re back on your scratch.py file. Now go to Tools -> Build System, and select Python3 (or whatever you named your Build System). If you don’t see your new build system, you may have to quit Sublime Text and reopen it.

Now run the same code to test which version of Python you’re using.

There you go, Python 3.7 up and running. Happy coding!

If you’d like to see my video tutorial on running python 3 in Sublime Text, click this link: https://youtu.be/IprbE2C_rsEv

--

--