0

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)

stallingOne
  • 3,633
  • 3
  • 41
  • 63
  • Have you added `C:\Users\mat\scoop\apps\python\current` to your system's `PATH`? That would be the most straightforward option. – MattDMo Mar 25 '22 at 16:53
  • Another question - what is the `Python3` build system that appears in the Build menu? Can you edit that to put the correct path instead? – MattDMo Mar 25 '22 at 16:53
  • @MattDMo 1) I believe PATH is fine because I typed ´python´ in cmd and it worked. And 2) the reason Python3 in that list is because I installed this package: https://github.com/petervaro/python (I was trying things, didn't really use this package) – stallingOne Mar 25 '22 at 19:50
  • I wouldn't use Peter's syntax definition any more - it was good (and needed!) when it first came out, but really the only one you need now is the built-in syntax. If you do a lot of Cython work it might be helpful, but for regular Python work it's grown stale, just like my own Python Improved. – MattDMo Mar 25 '22 at 20:06
  • How did you install Python? – MattDMo Mar 25 '22 at 20:07
  • @MattDMo I installed python via scoop (cf. https://scoop.sh/) . When I type "py" in command it says "Can't find a default Python." so definitely it's not correctly installed... Maybe I can just add my "...apps\python\current" to the PATH? – stallingOne Mar 26 '22 at 12:51
  • `C:\Users\mat\scoop\apps\python\current` is probably already on your `PATH`, because running `python` from CMD works fine. The trouble is that the default Python build system for Windows calls `py`, not `python`. I'll write a quick answer showing you how to make a new build system. – MattDMo Mar 26 '22 at 17:24

2 Answers2

1

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.

MattDMo
  • 100,794
  • 21
  • 241
  • 231
0

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.

Rahul
  • 139
  • 3
  • "No Installed Pythons Found!" (I installed python via scoop) – stallingOne Mar 26 '22 at 12:49
  • 1
    I tried by installing with scoop also. The command "py --list" still showed all the versions. I think you haven't set the environment variables or registry key for PEP correctly after using scoop. So, please check once as that registry key is required so third party applications can detect installed pythons. – Rahul Mar 27 '22 at 14:46