Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show result of python script in a console when running from ST editor?

I'm new to Sublime Text so am unfamiliar with its internals so far. From what I could tell the problem could be something related to this.

I have a python script

var = raw_input("Enter something: ")
print "You entered ", var

which asks for input, waits for it, then prints it out in windows console prompt.

How do I make ST3 upon "building" to show the results in a console window?

like image 593
Rook Avatar asked Sep 01 '13 10:09

Rook


People also ask

How do I run a Python .PY file in console?

To run Python scripts with the python command, you need to open a command-line and type in the word python , or python3 if you have both versions, followed by the path to your script, just like this: $ python3 hello.py Hello World!

How do I open an output window in Python?

cmd /k is the typical way to open any console application (not only Python) with a console window that will remain after the application closes. The easiest way I can think to do that, is to press Win+R, type cmd /k and then drag&drop the script you want to the Run dialog. Fantastic answer. You should have got this.


1 Answers

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.

like image 129
MattDMo Avatar answered Oct 10 '22 13:10

MattDMo