7

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?

MattDMo
  • 100,794
  • 21
  • 241
  • 231
Rook
  • 60,248
  • 49
  • 165
  • 242
  • Is it all right if you just `run` it from the console, _within_ sublime text? – Games Brainiac Oct 08 '13 at 19:52
  • @GamesBrainiac - I'm "aiming" for windows cmd (whenever I write "console", I mean that one) because I'm writing programs with some properties that ST's console really can't handle. Encoding and color-output-wise, not to mention graphics like. But having gone past reputation-hunting on these sites, I'll award it to anyone who can get me a step closer to having a proper build&run system which outputs to cmd. – Rook Oct 08 '13 at 20:19
  • Take my advice, just use PyCharm. I _could_ build this, but it would take ages, and it would not be a permanent solution to the problem, because ST3 is still in beta, and so the APIs keep changing. – Games Brainiac Oct 08 '13 at 20:20
  • @GamesBrainiac - You're saying ST cannot execute a simple command on a cmd prompt and show its results. How do people build and run their programs from it then? – Rook Oct 08 '13 at 20:28
  • They use CMD, in their current directories. And ST can, but its a lot of config, and its different for windows and linux, and the makes can't decide on what formats they want to keep. – Games Brainiac Oct 08 '13 at 20:29
  • I will research this and try to get back to you with a good solution. But, I doubt it will be fruity. – Games Brainiac Oct 08 '13 at 20:30

5 Answers5

7

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.

Community
  • 1
  • 1
MattDMo
  • 100,794
  • 21
  • 241
  • 231
  • Basically your `easy way` is my the same as my answer, @MattDMo in my eyes that doesn't look to good. But I can understand that you'd think that my answers just builds upon your first version, even though that's not the case. – Daniel Figueroa Oct 11 '13 at 05:47
  • 1
    @DanielFigueroa I see that our answers are similar, but they were arrived at independently. I gathered bits and pieces from around the web to generate the first version, and formulated my answer. I wasn't completely satisfied with it, came back to it after a few hours and came up with the "Easy Way", and amended my answer. It wasn't until afterward that I saw you had answered after me, and apparently we were both thinking along the same lines. – MattDMo Oct 11 '13 at 13:43
  • I prefer `"cmd": ["start", "cmd", "/c", "python -u $file && pause || pause"]`. This does not require typing `exit` or clicking the close button after running the script. – utapyngo Oct 11 '13 at 15:00
  • The easy was works fine, apart from one single thing. When I have unsaved files (unsaved to a file, i.e. not given names) it gives an error. Do you have any idea how that could be avoided? – Rook Oct 16 '13 at 16:40
  • @Idigas as far as I know Sublime won't build unsaved files - this is because it needs to pass the file's name to whatever interpreter/compiler you're using. You can set the `Save All on Build` option in the `Tools` menu, but that saves **all** open files. – MattDMo Oct 16 '13 at 17:14
2

It's really simple just issue a command to start a new cmd.exe.

start cmd /K python main.py

See here for more(ancient)

The /k flag "pipes" the other commands to cmd.exe and runs until you exit the shell yourself, useful for when you want to see traces.

The /C flag "pipes" the other commands to cmd.exe and runs until the python program has finished.

So in your project-file you should have something like this:

"build_systems": [
    {
        "name": "RunPY",
        "cmd": ["start", "cmd", "/K", "python", "main.py"],
        "shell": true,
        "working_dir": "<project_path>"
    },

]

While we're on the topic of build_systems in Sublime I would like to recommend the plugin BuildSwitcher, it works okay on ST3, it just doesn't always notice when you've added a new build in your project file. This also means that you have to install the plugin manually for ST3.

With that plugin you can easily define and run different builds with just a few key-strokes. A real timesaver.

Daniel Figueroa
  • 10,348
  • 5
  • 44
  • 66
  • Daniel, you also seem knowledgeable (not getting into who's answer was first) about ST's build systems. Could you please look at my last comment in the answer above, and tell if there's anything that could be done about that? – Rook Oct 16 '13 at 16:42
  • @Idigas Sure, just set the 'save_on_focus_lost' setting To true. That should solve it. You can take a look here for more fun settings: http://docs.sublimetext.info/en/latest/reference/settings.html – Daniel Figueroa Oct 16 '13 at 16:51
0

SublimeREPL for is a plugin for SublimeText

It does not open a cmd instance but can provide you a REPL, where You can provide input, which you usually cannot give on default console.

Installation

Install Package Control. http://wbond.net/sublime_packages/package_control
Install SublimeREPL
    Preferences | Package Control | Package Control: Install Package
    Choose SublimeREPL
Restart SublimeText2
Configure SublimeREPL (default settings in Preferences | Package Settings |
SublimeREPL | Settings - Default should be modified in Preferences |
Package Settings |SublimeREPL | Settings - User,
this way they will survive package upgrades!

Note from the documentation:

ctrl+, , f means: press Ctrl and Comma, release all, press F.

The keybinding above is used to Evaluate in REPL

If SublimeText prompts for Cannot find REPL for "Language", You need to open a REPL by

Tools | SublimeREPL | Language

I hope this helps

Arya-2790306
  • 115
  • 1
  • 1
  • While S.REPL is indeed a wonderful plugin, it doesn't suit me in this case, at all. I'm trying to solve this problem with ST's build systems partly because I'm trying to make them work for two of my other languages as well ... fortran (with ifort) and a certain special kind of Basic. I just used python as exemplary language because most people are familiar with it. – Rook Oct 09 '13 at 07:24
0

Here's a variant for OSX, just in case it's useful to anyone:

Go to Tools->Build System->New Build System...

{
    "cmd": ["/opt/X11/bin/xterm", "-e", "python -u \"$file\""],
    "selector": "source.python"
}

You can find more details about the syntax here

Save it (e.g. with the name mytestbuild.sublime-build in the directory that SublimeText gives you), switch to your Python file, select the build system from Tools->Build Systems->mytestbuild, and hit cmdB.

That will open up an xterm, and run your Python code inside the xterm window. If you want to use Terminal.app, you may need to use one of the tricks from this question.

Community
  • 1
  • 1
m01
  • 9,033
  • 6
  • 32
  • 58
0

[ This solution shows the output within Sublime, but as an auto-updating text file(s) shown in another pane ]

To have the output visible in Sublime as another file(s), do this:

  1. Create a new build system: Tools > Build Systems > New Build System...
  2. Use the following configuration:

    {
        "cmd": ["python.exe", "$file", "1>", "$file_name.__STDOUT__.txt", "2>", "$file_name.__STDERR__.txt"],
        "selector": "source.python",
        "shell": true,
        "working_dir": "$file_dir"
    }
  1. For your Python file select the above build system configuration file: Tools > Build Systems > {your_new_build_system_filename}
  2. ctrl + b
  3. Now, next to your file, e.g. "file.py" you'll have "file.__STDOUT__.py" and "file.__STDERR__.py" (for errors, if any)
  4. If you split your window into 3 columns, or a grid, you'll see the result immediately, without a need to switch panels / windows
ellockie
  • 3,730
  • 6
  • 42
  • 44