最近玩 python ,一般用的編譯器是 pycharm ,功能強大,但是苦於啟動速度遂准備換坑,瞄上了 sublime 。這里記錄一下 sublime 的設置以及坑爹項,需要注意的是我用的是 sublime text 3 做的例子。
在安裝numpy等庫函數時,通過“命令提示符”操作顯示庫函數已經安裝完畢,在pycharm中可是依然顯示引用失敗,嘗試使用sublime,顯示可用,遂好好使用sublime,現配置成想用的模式。
1 解決Tab轉換成空格
首先點擊Preferences --> Settings
顯示對話框,在右側 Preferences.sublime-settings-User,補充上一段代碼:
// The number of spaces a tab is considered equal to "tab_size": 4, // Set to true to insert spaces when tab is pressed "translate_tabs_to_spaces": true, //設置保存時自動轉換 "expand_tabs_on_save": true
2 、Package Control 安裝
package control 可以方便地安裝其他插件,實際上package control就是插件管理器(安裝、升級、移除)。
Tools -- > Install Package Control (注:一般該按鈕位於最后一行,次行為Developer)
如果發現Tools的最后一行為Developer,說明Package Control已經安裝過;
若Preferences - > Package Settings -> Package Control ,則說明安裝成功,如下圖。
3 用Package Control 安裝Python相關的插件
安裝Package Control 后需要重啟sublime。
打開install的命令對話框;
1)sublime軟件界面 Ctrl+Shift+P,打開下圖對話框,
2)輸入install,打開插件安裝對話框,
3)輸入想要安裝軟件的名字進行搜索並點擊安裝
- List Packages 顯示所有已安裝的插件
- Remove Packages 移除一個指定的插件
- Upgrade Package 更新一個指定的插件
- Upgrade/Overwrite All Packages 更新所有已安裝的插件
4 安裝代碼檢查工具 Flake8
Flake8包裝了下列工具:
- PyFlakes:靜態檢查Python代碼邏輯錯誤的工具。
- pep8: 靜態檢查PEP 8編碼風格的工具。
- Ned Batchelder’s McCabe script:靜態分析Python代碼復雜度的工具。
它綜合上述三者的功能,在簡化操作的同時,還提供了擴展開發接口。Flake8 工具非常適合編寫代碼時的檢查分析。
插件安裝:pip install flake8
查看幫助:在命令提示符中輸入flake8 --help 可以查看幫助。
插件升級:pip install --upgrade flake8
官網 https://pypi.org/project/flake8/
Flake8簡介 這里面介紹了 安裝、使用、配置、與Git整合等知識點
5 代碼校驗工具SublimeLinter
sublime的Sublime插件是檢查代碼語法是否錯誤,並對錯誤處進行提示。
如同其他插件一樣使用 Package Control 來安裝。
- 按下
Ctrl+Shift+p
進入 Command Control - 輸入
install
進入 Package Control: Install Package,注意 : 輸入install可能會等待時間較長, - 輸入
SublimeLinter
。點擊即可開始安裝.
當出現下圖(messages文檔)時表面安裝完畢
在
6 Install SublimeLinter-flake8
這個插件為sublimeLinter 提供了flake8接口。
如同其他插件一樣使用 Package Control 來安裝。
- 按下
Ctrl+Shift+p
進入 Command Control - 輸入
install
進入 Package Control: Install Package,注意 : 輸入install可能會等待時間較長, - 輸入
SublimeLinter-Flake8
。點擊即可開始安裝.
安裝完畢后,顯示下圖形式,但重啟sublime后插件才有效。
重啟后隨便打開一段代碼,如下圖
方括號里面即為不符合Flake8規定的語法,將光標放在方括號處可以在狀態欄處看到錯誤原因。
配置優化
在 pycharm 中顯示一般都是下划線之類的,優化修改顯示形式
打開preferes --> package settings --> sublimelinter --> settings
將sublimelinter.sublime-settings--sublimelinter(default)文件中的代碼全部復制——粘貼到sublimelinter.sublime-settings--user文件中;同時將user文件中(約205行) "mark_style": "outline" 更變為 "mark_style":“squiggly underline”。修改后將方括號提示轉變成下划線提示。
不過我覺得方括號錯誤提示挺好的,可以培養自己良好的代碼習慣。
======2019年01月01日 更新 =======
修改的原因,我將主題修改成淺綠色后,錯誤提示不明顯,所以將其錯誤提示修改有其他顏色。
將default中的內容全部復制粘貼在user中
修改 styles 中的內容
1 "styles": [ 2 { 3 // 用於確定顏色,例如 region.<colorish> 4 // colorish彩色可以是redish紅色、orangish橙色、yellowish淡黃色、 5 // greenish綠色、bluish藍色、purplish紫色、pinkish粉紅色 6 // Used to determine the color. E.g. region.<colorish>, with one of 7 // redish, orangish, yellowish, greenish, bluish, purplish, pinkish. 8 "scope": "region.yellowish markup.warning.sublime_linter", 9 10 // The error type this style definition will match for. 11 // An array which can contain "warning" and/or "error". 12 // If omitted will match both. 13 "types": ["warning"] 14 }, 15 ],
將第 8 行 修改為
"scope": "region.purplish markup.warning.sublime_linter",
此時就將錯誤提示顏色修改為紫色
7 代碼自動補全功能
----------- 我安裝后出現問題,此步驟跳過,以后不安裝該功能----------------------------
安裝之后嘗試,其與Flake8功能有重疊,本打算關閉anaconda的代碼檢查功能只保留代碼自動補全功能,結果報錯竟然該一個再報錯下一個。
卸載之后依然沒有改變形式,只有卸載sublime重新安裝了。誰要是知道的話,給我說一下呀!謝謝。
再次追新,后來發現沒有代碼自動補全確實不好用,又安裝了,只是本次安裝后,不再配置代碼檢驗 false 項。后來想想還是取消了anacoda的檢查,修改方式如下藍字所示
SublimeCodeIntel 插件(有的說這個插件依賴於另外安裝的 CodeIntel 來索引第三方包)可以實現自動補全,但是看里面存在有bug,詳細參看——>修復在 ST3 下 SublimeCodeIntel 對 Python 無法自動補全 import 語句里的模塊名的問題。就直接換Anaconda插件了。
1、Ctrl+shift+p 打開 Package Control
2、輸入install,進入安裝對話框
3、輸入Anaconda,點擊安裝
4、安裝成功后出現Messages文檔信息
5、配置:preferences --> package settings --> anaconda --> settings - default
(實際上,我安裝並重啟后,已有代碼提示信息,可網上搜到資料說“因Anaconda插件本身無法知道Python安裝路徑,故需要設置Python主程序的實際位置”)
打開后約在100行處可以看到如下內容,或者直接搜索 python_interpreter
/* Default python interpreter This can (and should) be overridden by project settings. NOTE: if you want anaconda to lint and complete using a remote python interpreter that runs the anaconda's minserver.py script in a remote machine just use it's address:port as interpreter for example: "python_interpreter": "tcp://my_remote.machine.com:19360" */ "python_interpreter": "python",
看過文件中的還是重新配置一下 ,鍵入Python.exe程序所在的路徑,注意將 \ 轉換成 / ,
"python_interpreter": "C:/.../Programs/Python/Python37/python.exe",
同時在setting user文件(Preferences->Package Settings->Anaconda->Settings User)中添加如下內容
-----------------------------------忽略----------------------------------------------------
{ "python_interpreter": "C:/Users/zyong/AppData/Local/Programs/Python/Python37/python.exe", "suppress_word_completions":true, "suppress_explicit_completions":true, "comlete_parameters":true, "swallow_startup_errors":true, "anaconda_linting":false, // anaconda 的規范代碼與Flake8 在功能上重疊, // 這個使用Flake8,將anaconda規范代碼功能false掉 "anaconda_linting": false, "pep8": false }
若沒有
"anaconda_linting": false, "pep8": false
------------------------------忽略-----------------------------------------------------
-----------------------------2018.09.05更新第三次更新start-------------------------------
1)打開兩個文件settings - user 和 settings - default。
Preferences->Package Settings->Anaconda->Settings - user
Preferences->Package Settings->Anaconda->Settings - default
2)將setting - default 中的代碼粘貼到 setting - user中
3)約在300行處,進行兩處修改
# sublime版本 Sublime Text Build 3176 x64 "anaconda_linter_mark_style": "none", #300行 "anaconda_linter_underlines": false, #305行
相關說明及后記
其實當時看到是滿屏幕的白色方框,記得在前面 SublimeLinter-flake8 優化配置中有 "mark_style": "outline" 更變為 "mark_style":“squiggly underline”
所以就打開anacoda的配置文件,搜索outline,發現確實有相關信息,翻譯和修改值嘗試,正好解決,為了規范合理,將setting - default中的代碼信息粘貼到setting - user中進行修改,保存。
原文信息如下:
/* If 'outline' (default), anaconda will outline error lines. 如果'outline'(默認),anaconda將概述錯誤行。 If 'fill', anaconda will fill the lines. 如果'填充',anaconda將填補線。 If 'solid_underline', anaconda will draw a solid underline below regions. 如果'solid_underline',anaconda將在區域下方繪制一個堅實的下划線。 If 'stippled_underline', anaconda will draw a stippled underline below regions. 如果'stippled_underline',anaconda將在區域下方繪制一個點狀下划線。 If 'squiggly_underline', anaconda will draw a squiggly underline below regions. 如果'squiggly_underline',anaconda將在地區下面繪制一個波浪形的下划線。 If 'none', anaconda will not draw anything on error lines. 如果'none',anaconda將不會在錯誤行上繪制任何內容。 */ "anaconda_linter_mark_style": "outline", /* If this is set to false, anaconda will not underline errors. 如果將其設置為false,則anaconda不會強調錯誤。 */ "anaconda_linter_underlines": true,
備注,如果該段代碼中沒有{ } 時,需將最外層添加 { } ,否則會報錯
error trying to parse settings unexpected trailing characters in packages/user/
Unexpected trailing characters?
-----------------------------2018.09.05更新第三次更新end-------------------------------
-----------------------------2018.12.23更新第三次更新start-------------------------------
由於想用ubuntu系統(重新安裝了win10 & Ubuntu雙系統),結果一直FQ不成功,后來還只能用Win10繼續學習Tensorflow。
在配置sublime時發現,在安裝PEP8后,將下端代碼放在了
preferences --> package settings --> anaconda --> settings - User 中
/* If this is set to true, anaconda will show errors inline. */ "anaconda_linter_phantoms": true,
運行代碼時出現如下情況
滿屏都是 E501報錯
遂打開:preferences --> package settings --> anaconda --> settings - Default 文件
檢索關鍵詞“ E501” ,
顯示 E501 - Try to make lines fit within --max-line-length characters.
然后再檢索 “ max-line-length ” 無果,再繼續檢索 “ max ” ,得到了 ‘ 4 matches ’
// Maximum line length for pep8
"pep8_max_line_length": 79,
同時偶然看見了下段代碼
"pep8_ignore":
[
"E309"
],
我就將 pep8_ignore 粘貼到Anaconda.sublime-setting-User中
"pep8_ignore": [ "E309", "E501" ],
保存后發現該E501報錯消失。
-----------------------------2018.12.23更新第三次更新start-------------------------------
這段代碼,可能會出現一個警告或語法錯誤處出現了多個方括號,這是由於Flake8 和 anaconda 均報錯造成的。
anaconda插件的主要功能:
1、代碼自動補全
2、顯示python類,方法或函數的使用方法
3、檢查導入模塊是否有效
4、按照PEP8規范自動化格式我們的代碼
5、可以跳轉到函數的定義或者類的定義
6、Goto Definitions 能夠在你的整個工程中查找並且顯示任意一個變量,函數或者類的定義。
7、Find Usage 能夠快速的查找某個變量,函數或者類在某個特定文件中的什么地方被使用了。
8、Show Documentation: 能夠顯示一個函數或者類的說明性字符串(當然,是在定義了字符串的情況下)
=============2019年01月01日 更新 ==============
preferences --> package settings --> anaconda --> settings - User 中添加
"anaconda_linter_mark_style": "none",
忽略anaconda錯誤提示
8 關於卸載sublime
由於安裝 anacoda 插件時出現問題,我卸載sublime又重新安裝,發現以前安裝的插件依然存在。
看到將插件直接刪除
打開對話框
將Packages中所有文件刪除。
也可以采用 “Remove Packages 移除一個指定的插件 ” 來完成
為了保證一次性成功,將注冊表中的sublime信息、C盤全部的sublime信息通過檢索全部刪除。再安裝后,達到預期效果。
Win+R 運行中輸入 “RegEdit”
9 自動保存
1)Ctrl+shift+p調出命令面板
2)在搜索框中輸入Preferences 找到 Preferences:Settings - User 點擊
3)打開package control.sublime-settings 文件如下圖
或者打開preferences --> package settings -->package control --> settings - user ,點擊settings - user,也出現類似的對話框
在package control.sublime-settings文件中末尾追加一行代碼
"save_on_focus_lost": true,
設置后每次文件失去焦點后就會自動保存,就不用手動保存了。
參考:讓你用sublime寫出最完美的python代碼--windows環境
10 打開左側邊
view --> side bar --> show open files
打開后就可以顯示當前打開了那些文件
11 創建project
打開左側邊可以更好地查看 project 的樹形結構:
project --> add folder to project 就可以創建一個project,在右側邊欄打開的情況下可以看到project的樹形結構。
12 代碼格式化工具
一直沒有找到相關更好的工具,網上找的沒有效果,等發現插件繼續補充
13 快捷方式
參考 前端神器Sublime Text下載、使用教程、插件推薦說明、全套快捷鍵 中的第三項 主要快捷鍵列表:
-----------------------2018年12月23日更新------------------------------
14 一些快捷鍵運行
ctrl + ` 在sublime中打開sublme執行命令控制面板Terminal,重復則為關閉
ctrl + b 在sublime中直接運行文件,並在sublme下方輸出運行結果。
ctrl + shift + b 與 ctrl + b 的效用類似,只是多了選擇的功能。
15 sublime字體設置
由於sublime默認的字體看着不太好,所以就想配置 楷體 或者 微軟雅黑
楷體 顯示漢字看着舒服但是英文字母顯示不好。
最后改成 Times New Roman
也即在Preferences -> Settings User文件中添加
"font_face": "Times New Roman",
16 sublime 主題顏色設置
黑色的主題色感覺不爽,就想換成護眼色。
在Preferences中使用Color Scheme 以及 Theme中的背景顏色沒有合適的,就在網上找了一下,有個淺綠色的
下圖是安裝后的顏色,感覺還挺好的。
1 下載文件 https://github.com/waer24/sublimeTheme
2 配置文件
將下載后的壓縮包解壓到一個文件中,我將其解壓到sublime的Package -> User中了
(打開方式 preferences --> Browse Package -> User )
同樣在Preferences ——> settings 文件右邊的User文件代碼中修改
"color_scheme": "Packages/User/sublimeTheme-master/Darcy.tmTheme",
注意,我填入的是文件的絕對路徑,保存后,其自動修改為相對路徑。
3 保存即可使用
===2019.05.20更新=====
a、常見插件
ConvertToUTF8 :解決中文問題
Colorsublime - Themes:Colorsublime 的主題集合
SublimeREPL:Python運行
SideBarEnhancements:側欄增強功能
Alignment:自動對齊,ctrl+alt+a
Anaconda:python插件集合,禁用PEP8提示:Sublime > Preferences > Package Settings > Anaconda > Settings – User: {"anaconda_linting": false}
SublimeLinter-pep8
b、主題配置:打開 Preferences > Settings (若報錯,注意下圖紅色標記內容color_scheme整行替換)
{ "auto_indent": true, "bold_folder_labels": true, "color_scheme": "Packages/Color Scheme - Default/Monokai.tmTheme", "draw_white_space": "all", "font_size": 20, "font_face": "Consolas", "highlight_line": true, "ignored_packages": [ ], "preview_on_click": false, "rulers": [ 100 ], "save_on_focus_lost": true, "scroll_past_end": true, "show_encoding": true, "show_full_path": true, "show_line_endings": true, "smart_indent": true, "tab_size": 4, "translate_tabs_to_spaces": true, "trim_automatic_white_space": true, "trim_trailing_white_space_on_save": true, "use_tab_stops": true, "word_wrap": true, "wrap_width": 80 }
c、自定義快捷鍵盤配置:打開Preferences > Key Bindings輸入如下代碼,F5運行程序,F6切換IDEL工具,ctrl+d自定義刪除行,其他快捷鍵是通用的,網上很多快捷鍵的資料,這里不贅述。
[ { "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": ["ctrl+d"], "command":"run_macro_file", "args": {"file":"res://Packages/Default/Delete Line.sublime-macro"} } ]
d、分屏:
ALT+shift+1單屏,ALT+shift+2左右分屏,ALT+shift+8上下分屏,
e、如果沒有自動提示,可以如下配置,打開Preferences >Package Settings >Anaconda >Setting Default輸入:Administrator改為你計算機的名字
"python_interpreter": "C:/Users/Administrator/Anaconda3/python.exe"
打開Preferences >Package Settings >Anaconda >Setting Users輸入:Administrator改為你計算機的名字
{ "python_interpreter":"C:/Users/Administrator/Anaconda3/python.exe", "suppress_word_completions":true, "suppress_explicit_completions":true, "complete_parameter":true, }
到此,完成全部配置工作
摘自:白寧超的博客《【Machine Learning】Python開發工具:Anaconda+Sublime 》