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

送分小仙女□ 提交于 2019-12-09 06:05:36

问题


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?


回答1:


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.




回答2:


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.




回答3:


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




回答4:


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.




回答5:


[ 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


来源:https://stackoverflow.com/questions/18557668/how-to-show-result-of-python-script-in-a-console-when-running-from-st-editor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!