How do I run Python code from Sublime Text 2?
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.
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.
-
-
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).
neo
Updated on July 15, 2020Comments
-
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 over 12 yearsbut 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 over 12 yearsPlease give more details (by expanding the question) on what you are doing.
-
neo over 12 yearsi just type in python code like print 'hello' and then build it and open the console to see this error.
-
Alexis about 12 yearssublime text can also "auto detect" the language. So it worked for me to just CTRL + B
-
SeanJA almost 12 yearsMake sure
python
is in yourPATH
... the windows installer doesn't seem to do this automagically -
Ib33X almost 12 yearshow do you stop running program (python)?
-
Enrico almost 12 yearsPay attention to the double slash in the path, Sublime won't recognize it otherwise!
-
Dylan Hogg almost 12 yearsSingle forward slash also works in place of double backslash on Windows: "C:/Python27/python.exe" (à la Linux and OSX)
-
mkenyon about 11 yearsUsing 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 almost 11 yearswhat does it do actually? How does it help?
-
icy over 10 yearsMy keyboard's break key looks like "Pause/Break", so to stop process I also added
{"keys": ["pause"], "command": "exec", "args": {"kill": true} }
-
aultimus over 10 yearsFor me the path was %APPDATA%\Sublime Text 2\Packages\Python\Python.sublime-build
-
CHM over 10 years@Qwerty He thought the program should handle exceptions so he put the code in a
try
block. -
Devi over 10 yearsHaving the line
{"keys": ["ctrl+shift+c"], "command": "exec", "args": {"kill": true} }
in User key bindings and usingctrl+shift+c
prints that the build is[Cancelled]
while the program still being run. -
pqn about 10 yearsIn 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 about 10 yearsSorry 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 about 10 years@JimRaynor it's unbuffered streams stackoverflow.com/questions/14258500/significance-of-u-option
-
Jonathan Lidbeck over 9 yearsSolved 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 over 9 yearsI tried this. It didn't fix the problem, and it broke the existing behavior.
-
Matt Bannert over 9 yearsCould you elaborate on how to ship data from a script window to SublimeREPL, cause it does work for python, while R does work...
-
HoKy22 almost 9 yearsI don't seem to see the Python folder in Sublime Text 3
-
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.