How do I run Python code from Sublime Text 2?

581,623

Solution 1

Tools -> Build System -> (choose) Python then:

To Run:

      Tools -> Build

      -or-

      Ctrl + B

      CMD + B  (OSX)

This would start your file in the console which should be at the bottom of the editor.

To Stop:

       Ctrl + Break or Tools -> Cancel Build

       Fn + C (OSX)

You can find out where your Break key is here: http://en.wikipedia.org/wiki/Break_key.

Note: CTRL + C will NOT work.

What to do when Ctrl + Break does not work:

Go to:

Preferences -> Key Bindings - User

and paste the line below:

{"keys": ["ctrl+shift+c"], "command": "exec", "args": {"kill": true} } 

Now, you can use ctrl+shift+c instead of CTRL+BREAK

Solution 2

Edit %APPDATA%\Sublime Text 2\Python\Python.sublime-build

Change content to:

{
    "cmd": ["C:\\python27\\python.exe", "-u", "$file"],
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python"
}

change the "c:\python27" part to any version of python you have in your system.

Solution 3

On Mac OS X, save your file with a .py extension. Press + B. It runs in a window below.

enter image description here

Solution 4

To RUN press CtrlB (answer by matiit)

But when CtrlB does not work, Sublime Text probably can't find the Python Interpreter. When trying to run your program, see the log and find the reference to Python in path.

[cmd:  [u'python', u'-u', u'C:\\scripts\\test.py']]
[path: ...;C:\Python27 32bit;...]

The point is that it tries to run python via command line, the cmd looks like:

python -u C:\scripts\test.py

If you can't run python from cmd, Sublime Text can't too.
(Try it yourself in cmd, type python in it and run it, python commandline should appear)

SOLUTION

You can either change the Sublime Text build formula or the System %PATH%.

  • To set your %PATH%:
    *You will need to restart your editor to load new %PATH%

    • Run Command Line* and enter this command: *needs to be run as administrator
      SETX /M PATH "%PATH%;<python_folder>"
      for example: SETX /M PATH "%PATH%;C:\Python27;C:\Python27\Scripts"

    • OR manually: (preferable)
      Add ;C:\Python27;C:\Python27\Scripts at the end of the string. Setting Path in Win7

  • To set the interpreter's path without messing with System %PATH% see this answer by ppy.

Solution 5

You can use SublimeREPL (you need to have Package Control installed first).

Share:
581,623
neo
Author by

neo

Updated on July 15, 2020

Comments

  • neo
    neo almost 4 years

    I want to set up a complete Python IDE in Sublime Text 2.

    I want to know how to run the Python code from within the editor. Is it done using build system? How do I do it ?

  • neo
    neo over 12 years
    but it shows the following error : File ".\exec.py", line 109, in run File ".\ntpath.py", line 205, in dirname File ".\ntpath.py", line 170, in split File ".\ntpath.py", line 125, in splitdrive TypeError: 'NoneType' object is unsubscriptable
  • poke
    poke over 12 years
    Please give more details (by expanding the question) on what you are doing.
  • neo
    neo over 12 years
    i just type in python code like print 'hello' and then build it and open the console to see this error.
  • Alexis
    Alexis about 12 years
    sublime text can also "auto detect" the language. So it worked for me to just CTRL + B
  • SeanJA
    SeanJA almost 12 years
    Make sure python is in your PATH... the windows installer doesn't seem to do this automagically
  • Ib33X
    Ib33X almost 12 years
    how do you stop running program (python)?
  • Enrico
    Enrico almost 12 years
    Pay attention to the double slash in the path, Sublime won't recognize it otherwise!
  • Dylan Hogg
    Dylan Hogg almost 12 years
    Single forward slash also works in place of double backslash on Windows: "C:/Python27/python.exe" (à la Linux and OSX)
  • mkenyon
    mkenyon about 11 years
    Using the Python console does not answer the question. The embedded interpreter is intended only to interact with the plugin API, not for general development.
  • Qwerty
    Qwerty almost 11 years
    what does it do actually? How does it help?
  • icy
    icy over 10 years
    My keyboard's break key looks like "Pause/Break", so to stop process I also added {"keys": ["pause"], "command": "exec", "args": {"kill": true} }
  • aultimus
    aultimus over 10 years
    For me the path was %APPDATA%\Sublime Text 2\Packages\Python\Python.sublime-build
  • CHM
    CHM over 10 years
    @Qwerty He thought the program should handle exceptions so he put the code in a try block.
  • Devi
    Devi over 10 years
    Having the line {"keys": ["ctrl+shift+c"], "command": "exec", "args": {"kill": true} } in User key bindings and using ctrl+shift+c prints that the build is [Cancelled] while the program still being run.
  • pqn
    pqn about 10 years
    In IDLE you can continue to use the shell your code runs in (to inspect objects, try more code, etc.). How do we do this in Sublime?
  • Jim Raynor
    Jim Raynor about 10 years
    Sorry but can you explain what is the meaning of option "-u"? I cannot find the list option of command line params to pass together with python.exe.
  • Qwerty
    Qwerty about 10 years
  • Jonathan Lidbeck
    Jonathan Lidbeck over 9 years
    Solved my problem too. Console output was appearing within Sublime Text 2, showing that my code was executing, but my GUI never appeared. I commented that line, and now there it is. Console output still goes to Sublime Text. Perfect!
  • Jonathan Lidbeck
    Jonathan Lidbeck over 9 years
    I tried this. It didn't fix the problem, and it broke the existing behavior.
  • Matt Bannert
    Matt Bannert over 9 years
    Could you elaborate on how to ship data from a script window to SublimeREPL, cause it does work for python, while R does work...
  • HoKy22
    HoKy22 almost 9 years
    I don't seem to see the Python folder in Sublime Text 3
  • Houy Narun
    Houy Narun about 6 years
    @matiit, it's cool, but how can I process user's input? something like guessNumber = input if guessNumber > 10: do_something()? after I input number, and press enter, sublime console start new line instead of processing `if statement.