4

So I'm trying to run python code from Sublime Text 3, but I'm not sure how. Even if it was only from the console, that would be fine. Anybody know how???

Jonah Johnson
  • 77
  • 1
  • 1
  • 4
  • 1
    Possible duplicate of [Python3.4 on Sublime Text 3](https://stackoverflow.com/questions/23257984/python3-4-on-sublime-text-3) – Brian J Sep 19 '17 at 20:35

8 Answers8

14

Tools->Build System->Python or Ctrl+B

  • At the start of a new session: Tools->Build System->Python Every time you want to run the code after that: Ctrl + B – grego Feb 03 '21 at 22:14
1

Need to install package to run python from sublime Python + Sublime

Dharmesh Fumakiya
  • 2,276
  • 2
  • 11
  • 17
1

Try Anaconda plugin which will help you to run python on sublime

Setup Sublime for python

napster
  • 384
  • 3
  • 13
1

You can use this package in sublime text: https://packagecontrol.io/packages/Terminal to open a terminal at the specific file or folder.

sikkis
  • 11
  • 3
1
  • Sublime Text 3 will run your python code inside the integrated console when you use Ctrl + B

  • if you want to run your code at own terminal, but still get some error information inside to integrated console, you need to build your own builder, or use plugins.

  • One way: https://github.com/Wilhox/Integrated-builder.git

Ville
  • 119
  • 7
1

This answer is for fellow googlers who want to run python scripts within their sublime. As other answers explains, all you need is a sublime build system, here after a bit of struggle I got it worked for Linux Systems.

{
  "cmd": ["gnome-terminal", "--", "/bin/bash", "-c", "python3 -u \"$file\" echo;echo;echo Press Enter to exit...;read"],
  "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
  "selector": "source.python"
}

This is by far the simplest I believe. Hope it helps.

Manu S Pillai
  • 899
  • 8
  • 13
1

If you need non-interactive Build System, just follow the Official Guide.

If you plan to run code that contains something like input(), or you have any other way of interacting with your program you would need additional setup - Plugin + simple config.

The steps of having proper/full build system are:

  1. Install "Package Control":

Win/Linux: ctrl+shift+p, Mac: cmd+shift+p ▶ Type: Install Package Control ▶ ENTER

  1. Install Terminus via Package Control:

Win/Linux: ctrl+shift+p, Mac: cmd+shift+p ▶ Type: Package Control: Install Package ▶ ENTER ▶ Type: Terminus ▶ ENTER

  1. Create Build System for Python

Tools ▶ Build System ▶ New Build System… menu item or the Build: New Build System ▶ Paste one of the following sections and edit respectively.

For Windows, obviously you should change the path to your Python:

{
    "target": "terminus_exec",
    "cancel": "terminus_cancel_build",
    
    "shell_cmd": "D:\\.python_venvs\\general_python\\Scripts\\python.exe -u \"$file\"",
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python",

    "env": {"PYTHONIOENCODING": "utf-8"},

    "variants":
    [
        {
            "name": "Syntax Check",
            "shell_cmd": "D:\\.python_venvs\\general_python\\Scripts\\python.exe -m py_compile \"${file}\"",
        }
    ]
}

For Mac/Linux, do not forget to change the path to your Python.:

{
    "target": "terminus_exec",
    "cancel": "terminus_cancel_build",
    
    "shell_cmd": "/home/<user>/.python_venvs/general_python/Scripts/python -u \"$file\"",
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python",

    "env": {"PYTHONIOENCODING": "utf-8"},

    "variants":
    [
        {
            "name": "Syntax Check",
            "shell_cmd": "/home/<user>/.python_venvs/general_python/Scripts/python -m py_compile \"${file}\"",
        }
    ]
}
  1. Name the file e.g: Python Custom.sublime-build
  2. Select the newly created Build System:

Tools ▶ Build System ▶ Python Custom 6. Execute your code: Ctrl/CMD + B

SOURCE

Martin Nikolov
  • 146
  • 1
  • 4
0

@Thayakorn Rakwetpakorn's answer is correct

Ctrl+ B, and also make sure to have saved the file as hello.py or something before trying to run it

If that doesnt work Tools->Build system -> New build system

Delete the existing code and paste the code I have shown below

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

Then change the file location to where your python.exe file location is, in the above code

"C:\\Users\\Programs\\Python\\Python37\\python.exe"

Instead of the code lines of my file location path

Dulangi_Kanchana
  • 1,135
  • 10
  • 21