How to Setup Python 3 on Sublime Text

Step 1: Download and install Python3 and Sublime text

You can download Python3 from this page https://www.python.org/downloads/ and install it by following simple wizard steps.

The latest version of Sublime Text can be downloaded from this page https://www.sublimetext.com/download. Install it on your computer and follow the next step.

Note: At the time of writing this post, the Sublime Text 3 was the latest version available.

Step 2: Find the Python3 path

For Windows, open cmd and type the following command.

python

The output will show you the version of the Python installed on your computer. In my case, it was Python 3.7.4

Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 19:29:22) [MSC v.1916 32 bit (Intel)] on win32

This will also start the Python console which help us to find the path of the python installed. Now, type the following commands.

>> import os
>>> import sys
>>> os.path.dirname(sys.executable)

The output will give the desired path. Save this somewhere on notepad.

'C:\\Users\\usr_name\\AppData\\Local\\Programs\\Python\\Python37-32'

For Unix or Mac, open terminal and type the following command.

which python3

The output will give the path that you are looking for. Save this somewhere to use in next step.

/usr/local/bin/python3

Step 3: Setup Build System for Python 3 on Sublime Text

Sublime ships with some pre-defined build systems, but in our case, we are going to build a custom build system to run Python3 programs.

  1. Open Sublime, go to Tools > Build System > New Build System.
  2. A new file will open with some default code in it. You can delete everything on this file and copy-paste this code.
    For Windows,

    {
    "cmd": ["C:/Users/usr_name/AppData/Local/Programs/Python/Python37-32/python.exe", "-u", "$file"],
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python"
    }

    Note: Get the path you saved and replace the double backward slashes with forward slash and place it in above location. This is important because the Sublime will not understand the path with backward slashes therefore it is necessary to make this change. Most importantly, also add /python.exe at the end of the path.

    For Mac, use the path you saved.

    {
    "cmd": ["/usr/local/bin/python3", "-u", "$file"],
    "file_regex": "^[ ]File \"(...?)\", line ([0-9]*)",
    "selector": "source.python"
    }

    Now, save the above file with name like Python-37.sublime-build.

Step 4: Select new Build System and run the code

  1. Select your new build system by going to Tools > Build System > Python-37.
  2. Create a new file, add the following code to test and save the file with .py extension
    import sys
    print("hello world")
    print(sys.version)
  3. Run the code by Ctrl + B for Window users and Command + B for Mac users. You will get the following output.
    hello world
    3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 19:29:22) [MSC v.1916 32 bit (Intel)]

You are now all setup to run Python3 code on Sublime Text

Happy Coding 🙂

Maninder Singh