Sublime Text 3 作为 Python 的开发环境,需要提供两个功能:代码提示和 REPL,这两个功能可以通过安装 Package 来实现。安装过程如下:
- 首选需要安装包管理工具
Package Control
,按ctrl+\
` 打开 Sublime 控制台,粘贴下面代码后按回车,安装完成后可能需要重新启动 Sublime。
import urllib.request,os,hashlib; h = 'df21e130d211cfc94d9b0905775a7c0f' + '1e3d39e33b79698005270310898eea76'; pf = 'Package Control.sublime-package'; ipp = sublime.installed_packages_path(); urllib.request.install_opener( urllib.request.build_opener( urllib.request.ProxyHandler()) ); by = urllib.request.urlopen( 'http://packagecontrol.io/' + pf.replace(' ', '%20')).read(); dh = hashlib.sha256(by).hexdigest(); print('Error validating download (got %s instead of %s), please try manual install' % (dh, h)) if dh != h else open(os.path.join( ipp, pf), 'wb' ).write(by)
- 安装代码提示工具
SublimeCodeIntel
,菜单:Preferences -> Package Control
,输入Install Package
,等待一会儿后,输入SublimeCodeIntel
后安装即可。安装完成后,菜单:Preferences -> Package Settings -> SublimeCodeIntel -> Settings Default
,找到codeintel_language_settings
项,Windows 平台下,添加如下内容:
"Python": {
"python": "C:/Python27/python.exe",
"codeintel_scan_extra_dir": [
"C:/Python27/Lib",
"C:/Python27/Lib/site-packages"
],
"codeintel_scan_files_in_project": true,
"codeintel_selected_catalogs": []
},
Mac 平台下,添加如下内容:
"Python": {
"python": "C:/Python27/python.exe",
"codeintel_scan_extra_dir": [
"/Applications/Sublime Text.app/Contents/MacOS",
"~/Library/Application Support/Sublime Text 3/Packages/SublimeCodeIntel/arch",
"~/Library/Application Support/Sublime Text 3/Packages/SublimeCodeIntel/libs",
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7",
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages"
],
"codeintel_scan_files_in_project": true,
"codeintel_selected_catalogs": []
},
这个工具初次使用,不是很稳定,状态栏经常会有些错误提示,但写代码时还是可以正常工作的。
- 安装 REPL 工具
SublimeREPL
,安装方法同上,添加F5
键在 REPL 中运行代码的功能,可以在菜单:Preferences -> Key Settings
中添加如下代码:
[
{ "keys": ["f5"], "command": "repl_transfer_current", "args": {"scope": "file"}}
]
SublimeREPL
在使用上有一些技巧,首选通过 View -> Layout -> Columns: 2
把 Sublime 设置成两列模式,然后 Tools -> SublimeREPL -> Python ->Python
在右列显示 Python REPL,这时在左列中打开的 Python 脚本,按 F5
即可在右列显示运行结果,并可进行交互编程。