从Sublime Text 2中交互式运行Python [英] Running Python interactively from within Sublime Text 2
问题描述
我已经查看了该论坛上的所有答案,但我错过了一些东西. 我希望能够在Sublime Text 2中编辑Python文件"myfile.py"时点击 Cmd + B .
I have looked at all the answers on this forum but I'm missing something. I want to be able to hit Cmd+B while editing a Python file "myfile.py" in Sublime Text 2.
这应该打开一个Python外壳程序,该外壳程序会加载我的文件并返回到交互式提示符,以便我的Python脚本中的名称空间可用.
This should open up a Python shell that loads my file and returns me to the interactive prompt so the namespace in my Python script is available.
在构建设置中设置-i
选项仍会关闭解释器(见下文)
Setting the -i
option in the build setting still closes the interpreter (see below)
> 81
> >>> [Finished in 0.1s]
我下载了 sublimeREPL ,但不确定如何设置-i
选项.
感谢您的帮助
I downloaded sublimeREPL but I'm not sure how to set the -i
option.
Any help is appreciated
推荐答案
好的,感谢sneawo的提示!这是我这样做的第一个切入点.
ok, thanks to sneawo for the hints! Here's my first cut at doing this.
第1步.创建一个插件pydev(来自Tools-> New Plugin),该插件创建命令"pydev"
Step 1. Create a plugin pydev, (from Tools->New Plugin) which creates a command 'pydev'
import sublime, sublime_plugin
class PydevCommand(sublime_plugin.WindowCommand):
def run(self):
self.window.run_command('set_layout', {"cols":[0.0, 1.0], "rows":[0.0, 0.5, 1.0], "cells":[[0, 0, 1, 1], [0, 1, 1, 2]]})
self.window.run_command('repl_open',{"type": "subprocess",
"encoding": "utf8",
"cmd": ["python2.7", "-i", "-u", "$file"],
"cwd": "$file_path",
"syntax": "Packages/Python/Python.tmLanguage",
"external_id": "python2.7"
})
self.window.run_command('move_to_group', { "group": 1 })
第2步.在Preferences-> Key-Bindings-user
Step 2. Create a new key binding in the Preferences->Key-Bindings-user
{"keys": ["f5"], "command": "pydev"}
现在按 f5 (在Mac上默认为 fn + f5 )会完成窍门,它将启动python解释器在一个repl标签中,将布局设置为水平两个窗口,然后将repl标签移至下部窗口.
Now pressing f5 (on the Mac it will be fn+f5 by default) does the trick-it will start the python interpreter in a repl tab, set the layout to two-window horizontal and move the repl tab to the lower window.
这是非常基本的,因为它不会检查当前布局是什么,而只是将布局设置为2水平.可能会整理代码以进行一些检查,并只需在现有布局中添加一个水平窗口即可.关闭repl标签时删除水平缓冲区也将是一件好事.
This is pretty basic in that it doesn't check to see what the current layout is and simply sets the layout to 2-horizontal. Will probably spruce up the code to do some checking and simply add a horizontal window to the existing layout. Also would be good to remove the horizontal buffer when the repl tab is closed.
这篇关于从Sublime Text 2中交互式运行Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!