1. 程式人生 > >Sublime text 3搭建Python開發環境及常用外掛安裝

Sublime text 3搭建Python開發環境及常用外掛安裝

一、環境準備

1、官方網站地址

2、Windows 10

3、Sublime Text 3 + 官網購買license(Just a suggestion,$80)

  購買連結,Sublime Text may be downloaded and evaluated for free, however a license must be purchased for continued use。如果資金緊張,可以搜尋破解版License(建議支援正版,本文不再提供破解版License)。

二、安裝Sublime Text 3

1、雙擊下載的.exe檔案安裝,安裝路徑不要有中文目錄

2、安裝Sublime Text 3時,勾選 “Add to explorer context menu”
,可以在檔案右鍵選單新增“Open with Sublime Text”,方便使用Sublime Text開啟檔案。

三、配置Python環境

執行環境

1、開啟Tools > Build System > New Build System..

2、點選New Build System後,會生成一個空配置檔案,在這個配置檔案內覆蓋配置資訊,本文python安裝路徑為“D:/Anaconda3”,(注意區分正反斜槓,請將路徑換成python實際安裝路徑),然後按ctrl+s,將檔案儲存在預設路徑,檔名命名為“Python3”

{
    "cmd": ["D:/Anaconda3/python.exe","-u","$file"],
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python",
}

如圖:

3、開啟Tools > Build System,選擇新建好的Python3即可

 

測試

1、新建test.py檔案,輸入簡單python語句,按Ctrl+B執行

字型及字號

1、開啟Preferences –>>Settings(Settings User),在右側新增如下程式碼(font_face及font_size可根據個人喜好更改)

複製程式碼
{
    "color_scheme": "Packages/Color Scheme - Default/Monokai.sublime-color-scheme",
    "font_face": "Consolas",
    "font_size": 14,
    "ignored_packages":
    [
        "Vintage"
    ]
}
複製程式碼

 2、效果如下

 四、安裝Package Control(官網教程)

第一種方法:線上安裝

通過快捷鍵[ ctrl+`]或“ View > Show Console”選單開啟控制檯,將下面的Python程式碼貼上到控制檯裡
import urllib.request,os,hashlib; h = '6f4c264a24d933ce70df5dedcf1dcaee' + 'ebe013ee18cced0ef93d5f746d80ef60'; 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)

注:上面程式碼僅適用於Sublime Text 3,如果是Sublime Text2請使用如下程式碼:

import urllib2,os,hashlib; h = '6f4c264a24d933ce70df5dedcf1dcaee' + 'ebe013ee18cced0ef93d5f746d80ef60'; 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()) ); by = urllib2.urlopen( 'http://packagecontrol.io/' + pf.replace(' ', '%20')).read(); dh = hashlib.sha256(by).hexdigest(); open( os.path.join( ipp, pf), 'wb' ).write(by) if dh == h else None; print('Error validating download (got %s instead of %s), please try manual install' % (dh, h) if dh != h else 'Please restart Sublime Text to finish installation')

關掉並重新開啟Sublime Text 3,如果在Perferences->package settings中能看到package control這一項,則表示安裝成功。

第二種方法:手動安裝

1、點選 Preferences > Browse Packages… 選單
2、Browse up a folder and then into the Installed Packages/ folder
3、Download Package Control.sublime-package and copy it into the Installed Packages/ directory
4、Restart Sublime Text

1、通過Package Control安裝其他外掛

1.按下Ctrl+Shift+P調出命令面板

2.輸入install 調出 Install Package

3.在列表中選中要安裝的外掛,或者輸入外掛名,根據命令面板中的過濾結果,選擇要安裝的外掛

2、通過Package Control檢視已安裝的外掛

1. 按下Ctrl+Shift+P調出命令面板

2. 輸入"package",在下拉列表找到"Package Control: list packages",選中後回車,可以顯示全部外掛列表。

五、常用外掛介紹及安裝

SublimeCodeIntel

介紹

Full-featured code intelligence and smart autocomplete engine

  • Jump to Symbol Definition - Jump to the file and line of the definition of a symbol.

  • Imports autocomplete - Shows autocomplete with the available modules/symbols in real time.

  • Function Call tooltips - Displays information in the status bar about the working function.

支援語言

JavaScript, ES6, Mason, XBL, XUL, RHTML, SCSS, Python, HTML, Ruby, Python, XML, XSLT, Django, HTML5, Perl, CSS, Twig, Less, Smarty, Node.js, Tcl, TemplateToolkit, PHP.

Installation

1.Control+Shift+P開啟Package Control控制檯

2.輸入install,選擇關聯出來的install package

3.輸入sublimecodeintel,然後點選列表提示的sublimecodeintel安裝

4.安裝完成之後,文字框會出現如下顯示,或者可以通過【Preferences>Package Settings】中檢視到已安裝的sublimecodeintel外掛

5.開啟preferences->packages settings ->Package Control ->Settings-User,檢查是否有如下紅框程式碼,如果沒有得手動新增

6.點選preferences中的browse Packages,進入SublimeCodeIntel資料夾,在當前的路徑下新建.codeintel資料夾(windows中檔案命名的時候為 .codeintel. ),之後進入到 .codeintel資料夾中,新建檔案“config.log”檔案,開啟輸入(下文以路徑“D:\Anaconda3”為例,實際配置時請根據具體安裝路徑修改):

複製程式碼
"python3":{
    "python":"D:/Anaconda3/python.exe",
    "pythonExtraPaths":[
         "D:/Anaconda3/DLLs",
         "D:/Anaconda3/Lib",
         "D:/Anaconda3/Lib/lib-tk",
         "D:/Anaconda3/Lib/site-packages",
    ]
}
複製程式碼

如圖:

7.儲存,重啟Sublime Text 3

測試

新建檔案並儲存為.py檔案,輸入程式碼測試

 

SublimeREPL

介紹

新增快捷鍵後,可直接運行當前檔案,非常方便

  • Launch python in local or remote(1) virtualenv.

  • Quickly run selected script or launch PDB.

  • Use SublimeText Python console with history and multiline input.

使用方法

1、安裝SublimeREPL外掛後,開啟Preferences->Key Bindings,新增快捷鍵:

程式碼貼上:

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

 測試

1、新建test.py檔案,例如:

2、上文中SublimeREPL外掛設定的快捷鍵是F5,所以按F5執行成功如下

 

 SublimeTmpl

介紹

安裝外掛後,可以通過快捷鍵按照模板快速新建檔案

使用方法

1、安裝SublimeTmpl外掛後,開啟Preferences->Package Settings->SublimeTmpl->Settings User,新增以下內容

複製程式碼
{  
    "disable_keymap_actions": false, // "all"; "html,css"  
    "date_format" : "%Y-%m-%d %H:%M:%S",  
    "attr": {  
        "author": "Yong Lee",  
        "email": "[email protected]",  
        "link": "http://www.cnblogs.com/honkly/"  
    }  
}
複製程式碼

如圖:

 2、新增快捷鍵,開啟Preferences->Key Bindings,新增紅框中的快捷鍵程式碼

複製程式碼
[
    {
        "caption": "Tmpl: Create python", "command": "sublime_tmpl",  
        "keys": ["ctrl+alt+n"], "args": {"type": "python"}  
    },
    {
        "keys": ["f5"],
        "caption": "SublimeREPL: Python - RUN current file",
        "command": "run_existing_window_command",
        "args": {
            "id": "repl_python_run",
            "file": "config/Python/Main.sublime-menu"
        }
    }
]
複製程式碼

 如圖:

 

測試

開啟Sublime Text 3,上文中SublimeTmpl外掛設定的快捷鍵是"Ctrl+alt+n",按快捷鍵後成功新建檔案如下

 

ColorSublime

介紹

提供了主題更換功能,可根據個人喜好選擇

使用方法

1、安裝ColorSublime外掛完成後,開啟Preferences->Color Scheme...,選擇主題

 

 Anaconda

介紹

程式碼提示等許多功能,必備

安裝方法

1、Preferences->Package Settings->Anaconda->Settings Default,修改"python_interpreter"為實際Python安裝路徑

2、Preferences->Package Settings->Anaconda->Settings User,新增如下內容

複製程式碼
{
    "python_interpreter":"D:/Anaconda3/python.exe",
    "suppress_word_completions":true,
    "suppress_explicit_completions":true,
    "comlete_parameters":true,
    "swallow_startup_errors":true,
    "anaconda_linting":false
}
複製程式碼

如圖:

測試

1、開啟Sublime Text3,新建任意*.py檔案,輸入語句測試,如圖