[英]Running Python interactively from within Sublime Text 2
我已經看過這個論壇上的所有答案,但我遺漏了一些東西。 我希望能夠在Sublime Text 2中編輯Python文件“myfile.py”時點擊Cmd + B.
這應該打開一個Python shell,加載我的文件並返回到交互式提示符,以便我的Python腳本中的命名空間可用。
在構建設置中設置-i
選項仍會關閉解釋器(參見下文)
> 81
> >>> [Finished in 0.1s]
我下載了sublimeREPL,但我不知道如何設置-i
選項。
任何幫助表示贊賞
好的,感謝sneawo提示! 這是我第一次做這件事。
步驟1.創建一個插件pydev,(從Tools-> New Plugin)創建一個命令'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中創建一個新的密鑰綁定
{"keys": ["f5"], "command": "pydev"}
現在按f5 (在Mac上它默認為fn + f5 )就可以了 - 它將在repl選項卡中啟動python解釋器,將布局設置為雙窗口水平並將repl選項卡移動到下窗口。
這是非常基本的,因為它不會檢查當前布局是什么,只是將布局設置為2水平。 可能會修改代碼來進行一些檢查,只需在現有布局中添加一個水平窗口即可。 當repl選項卡關閉時,也可以刪除水平緩沖區。
嘗試更新用戶鍵綁定:
[
{ "keys": ["super+shift+r"], "command": "repl_open",
"caption": "Python",
"mnemonic": "p",
"args": {
"type": "subprocess",
"encoding": "utf8",
"cmd": ["python", "-i", "-u", "$file"],
"cwd": "$file_path",
"syntax": "Packages/Python/Python.tmLanguage",
"external_id": "python"
}
}
]
答案比你的方法簡單得多。 只需定義一個新的構建“profile”(構建系統),在其中你准確捕獲默認的Python構建,除了將選項-u
更改為-ui
{ "cmd": ["C:\\\\python33\\\\python.exe", "-ui", "$file"], "file_regex": "^[ ]*File \\"(...*?)\\", line ([0-9]*)", "selector": "source.python" }
我想通過@ user1936097為答案添加快速編輯。
我復制了這個想法但是想要加載IPython(代碼可以正常加載標准Python)。 我換了......
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('repl_open', {
"type": "subprocess",
"encoding": "utf8",
"autocomplete_server": true,
"cmd": ["python","-u","${packages}/SublimeREPL/config/Python/ipy_repl.py"],
"cwd": "$file_path",
"syntax": "Packages/Python/Python.tmLanguage",
"external_id": "python",
"extend_env": {
"PYTHONIOENCODING": "utf-8",
"SUBLIMEREPL_EDITOR": "$editor"}
})
但它沒有用。
行"autocomplete_server": true
似乎是問題所在。 如果我刪除它,代碼運行,但IPython打開關閉。 如果我離開它,什么都沒發生。
我終於借用了文件/SublimeREPL/config/Python/Default.sublime-commands
找到的命令,並提出了......
self.window.run_command('run_existing_window_command', {
"id": "repl_python_ipython",
"file": "config/Python/Main.sublime-menu"
})
這制作了最終的插件代碼:
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('run_existing_window_command', {
"id": "repl_python_ipython",
"file": "config/Python/Main.sublime-menu"
})
self.window.run_command('move_to_group', { "group": 1 })
將其分配給keybind,如原始帖子所示,您現在將加載IPython而不是標准Python!
這是一種簡單的方法,只需創建一個新的構建系統。
{
"cmd": ["C:\\python33\\python.exe", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
然后將構建系統保存為Python3或Python27並將其選為默認值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.