Sublime中運行基於控制檯的Python腳本程序

首先須要安裝Package Control,若是沒有安裝過的話。python

使用Ctrl+`(不是單引號哈,在鍵盤左上角位置)快捷鍵或者經過View->Show Console菜單打開命令行。shell

使用Sublime Text 3的,粘貼以下代碼:
ui

import urllib.request,os; pf = 'Package Control.sublime-package'; ipp = sublime.installed_packages_path(); urllib.request.install_opener( urllib.request.build_opener( urllib.request.ProxyHandler()) ); open(os.path.join(ipp, pf), 'wb').write(urllib.request.urlopen( 'http://sublime.wbond.net/' + pf.replace(' ','%20')).read())url

若是是 Sublime Text 2,使用代碼以下:
spa

import urllib2,os; pf='Package Control.sublime-package'; ipp = sublime.installed_packages_path(); os.makedirs( ipp ) if not os.path.exists(ipp) else None; urllib2.install_opener( urllib2.build_opener( urllib2.ProxyHandler( ))); open( os.path.join( ipp, pf), 'wb').write( urllib2.urlopen( 'http://sublime.wbond.net/' +pf.replace( ' ','%20' )).read()); print( 'Please restart Sublime Text to finish installation').net

順利的話,此時就能夠在Preferences菜單下看到Package Settings和Package Control兩個菜單了。
命令行

1、安裝SublimeREPLrest

一、調用ctrl+shift+p,輸入:sublimerepl選擇並安裝;(若是找不到這個sublimerepl,後來輸入install package才調出來
二、而後tools --sublimeREPL--Python你會發現打開了一個python的shell窗口,顯然這個並非咱們想要的;
三、繼續往下走,選擇好咱們要執行的py文件,選擇
Tools -> SublimeREPL -> Python -> RUN current file,這時候已經支持輸入了數據了。
code

2、設置快捷鍵orm

最後,因爲每次運行程序都要執行:Tools -> SublimeREPL -> Python -> RUN current file 菜單有點麻煩,因此如今能夠考慮給他建立個快捷鍵。方法以下:

選擇主菜單上perferences -- key bindings user 中粘貼以下代碼(快捷鍵紅色標示的自定義就能夠,我的喜歡F5):

[ {"keys":["f5"],
"caption": "SublimeREPL: Python - RUN current file",
"command": "run_existing_window_command", "args":
{
"id": "repl_python_run",
"file": "config/Python/Main.sublime-menu"
}}
]


這樣之後你打開你要運行的Py程序,按下f5(Mac OS X下須要同時按下fn鍵才能激活功能鍵選擇)就能夠直接運行了。

下面的配置將F5設爲運行python文件,F6爲顯示Python控制檯,F7爲顯示Shell控制檯,直接加入「Preference-key-binding-user」便可。

[ {"keys":["f5"],
    "caption": "SublimeREPL: Python - RUN current file",
    "command": "run_existing_window_command", "args":
    {
    "id": "repl_python_run",
    "file": "config/Python/Main.sublime-menu"
    }
},{"keys":["f6"],
    "caption": "SublimeREPL: Python",
    "command": "run_existing_window_command", "args":
    {
    "id": "repl_python",
    "file": "config/Python/Main.sublime-menu"
    }
},{"keys":["f7"],
    "caption": "SublimeREPL: Shell",
    "command": "run_existing_window_command", "args":
    {
    "id": "repl_shell",
    "file": "config/Shell/Main.sublime-menu"
    }
}
]
相關文章
相關標籤/搜索