In Sublime Text, How to change the default Python build system?

2.1k Views Asked by At

I have .py script open in sublime text that I would like to run using ctrl+B.

The script to test is simply printing the python version and location:

import sys
print("Current Python version", sys.version)
print("Current Python folder:", sys.prefix)

Automatically Sublime Text selects the "Python" build system, which is good. To make sure I selected it manually:

enter image description here

However that build system doesn't refer to the right Python exe. because the result of this is

Can't find a default Python. [Finished in 62ms]

I verified that Python is well installed and available in the cmd.
I can run those same commands and I get:

Current Python folder: C:\Users\mat\scoop\apps\python\current

enter image description here

Note that the script worked previously and printed v 3.9.x which was in a separate folder, but was uninstalled recently.

So my question is:
How can I change the default Python build system to use either a specific folder or the default python path.

(I already tried to restart sublime-text & windows, with no success)

2

There are 2 best solutions below

0
On

See my answer here for how to make a new build system generally - the specific case there is C++. For your installation of Python, the contents should be:

{
    "cmd": ["c:/Users/mat/scoop/apps/python/current/python", "-u", "$file"],
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python",
    "env": {"PYTHONIOENCODING": "utf-8"}
}

When you go to save the build system, it will automatically put it in your Packages/User directory. Name it something like Python (scoop).sublime-build, then make sure you choose it in the Build System menu before building.

2
On

For Windows OS:

  1. Find out Python versions available by using following command in command prompt without double quotes: "py --list". ( Output of my system:

C:\Users\trira>py --list

Installed Pythons found by py Launcher for Windows

-3.10-64 *

-3.8-64

)

  1. Note down the name of version you would like to use (let's assume it's "-3.10-64").
  1. In your Sublime Build file, kindly copy & paste the following build (replace highlighted version with version you want):

{ "shell_cmd": "start cmd /k py -3.10-64 $file_name" }

  1. (Optional step) Give the build a meaningful name containing version info.

  2. Now select & use this build to run your python script.