This is actually surprisingly easy, but it took a lot of digging to connect the pieces. I first came up with a more roundabout way using a batch file, but after some more thinking put it all together into a single Sublime build system.
The Easy Way
The following works just fine:
{
"cmd": ["start", "cmd", "/k", "c:/python27/python.exe", "-u", "$file"],
"selector": "source.python",
"shell": true,
"working_dir": "$file_dir"
}
Save it as Packages/User/Python_cmd.sublime-build
, select Tools -> Build System -> Python_cmd
, and build with CtrlB.
start
does what it says it does, start a new process independent of Sublime Text. cmd
is cmd.exe
, the Windows command-line interpreter. The /k
flag keeps the window open (at a new command prompt) after your program has run, allowing you to examine its output, look at tracebacks, run additional commands, etc. If you don't need that functionality, change it to /c
(like below), and the cmd
window will close when the program is done running.
I've tested it on XP and Win7, in both ST2 and ST3, and it works great on all of them with no changes needed.
My initial solution:
First, create run_python.bat
and store it in your Packages/User
directory (accessible from the Preferences -> Browse Packages...
menu option):
@echo off
c:\Python27\python.exe -u %1
pause
Obviously, adjust the path to python.exe
if it's different for you. Next, create Packages/User/Python_cmd.sublime-build
with the following contents:
{
"cmd": ["start", "cmd", "/c", "run_python.bat", "$file"],
"selector": "source.python",
"shell": true,
"working_dir": "$packages/User"
}
Save it, select Tools -> Build System -> Python_cmd
, switch over to your Python program above, hit CtrlB and you, my friend, are good to go. If you want the cmd.exe
window to stay open to enter more commands at the prompt, change the /c
to /k
. I haven't tested this with any GUIs, but assuming you can launch them now from the command line, they should work with this.
I've tested this on XP, so it should work with Win7 too. It should also work with Sublime Text 2.