將Emacs打造成強(qiáng)大的Python代碼編輯工具
基本配置
Emacs本身提供了python-mode,輸入M-x python-mode,就可以進(jìn)入python模式。相應(yīng)地,會在菜單欄出現(xiàn)Python菜單。當(dāng)然,一般來講,如果是.py文件打開的話,也會自動進(jìn)入該模式。
不過,默認(rèn)的python模式功能上面用起來還是有點(diǎn)弱,而且許多地方做的并不好,最好下載第三方的python模式。python-mode是一個開源項(xiàng)目,可以在https://launchpad.net/python-mode進(jìn)行下載。
1.安裝
1).安裝prog-modes:
aptitude install prolog-el
2).下載python-mode.el文件在項(xiàng)目主頁上面。
3).編譯:
C-x C-f /path/to/python-mode.el RET M-x byte-compile-file RET
4).在.emacs中加入python-mode.el路徑:
(setq load-path (cons "/dir/of/python-mode/" load-path))
檢測擴(kuò)展是否加載路徑,測試方法:M-x locate-library RET python-mode RET
2.配置.emacs文件
(setq auto-mode-alist (cons '("http://.py$" . python-mode) auto-mode-alist)) (setq interpreter-mode-alist (cons '("python" . python-mode) interpreter-mode-alist)) (autoload 'python-mode "python-mode" "Python editing mode." t) ;;; add these lines if you like color-based syntax highlighting (global-font-lock-mode t) (setq font-lock-maximum-decoration t) (set-language-environment 'Chinese-GB) (set-keyboard-coding-system 'euc-cn) (set-clipboard-coding-system 'euc-cn) (set-terminal-coding-system 'euc-cn) (set-buffer-file-coding-system 'euc-cn) (set-selection-coding-system 'euc-cn) (modify-coding-system-alist 'process "*" 'euc-cn) (setq default-process-coding-system '(euc-cn . euc-cn)) (setq-default pathname-coding-system 'euc-cn)
3.操作
1).執(zhí)行:C-c C-c,這樣會在新的窗口及緩沖區(qū)執(zhí)行腳本;
2).C-j:以相同的縮進(jìn)插入新的一行;
3).C-M-a:跳至函數(shù)或類首;
4).C-M-e:跳至函數(shù)或類尾;
5).C-c C-w:運(yùn)行PyChecker進(jìn)行代碼檢測;
大體的使用方式就是這樣的了,另外,還有許多類或函數(shù)的模板可以通過快捷鍵進(jìn)行,在今后常用的時候會加強(qiáng)了解的。感謝你能看到這里!
安裝擴(kuò)展
在Emacs中,通過各種擴(kuò)展,打造強(qiáng)大的Python IDE環(huán)境,包括Snippet工具,智能提示,自動補(bǔ)全,重構(gòu)工具,調(diào)試以及GAE的調(diào)試,等等。以下各工具的安裝前提是你對Emacs的配置文件有一定的了解,所有相關(guān)的el文件都必須放在load_path能夠加載的地方。
1. YASnippet
snippet工具,可自定義一些模板,必不可少的好東西!看了下面這個很酷的演示動畫就明白了:
http://yasnippet.googlecode.com/files/yasnippet.avi
安裝方法:
(require 'yasnippet) (yas/initialize) (yas/load-directory "~/.emacs.d/plugins/yasnippet-0.6.1c/snippets")
2. AutoComplete
自動完成工具,會像VS里一樣,彈出一個列表框讓你去選擇。
安裝方法:
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->(require 'auto-complete) (require 'auto-complete-config) (global-auto-complete-mode t) (setq-default ac-sources '(ac-source-words-in-same-mode-buffers)) (add-hook 'emacs-lisp-mode-hook (lambda () (add-to-list 'ac-sources 'ac-source-symbols))) (add-hook 'auto-complete-mode-hook (lambda () (add-to-list 'ac-sources 'ac-source-filename))) (set-face-background 'ac-candidate-face "lightgray") (set-face-underline 'ac-candidate-face "darkgray") (set-face-background 'ac-selection-face "steelblue") ;;; 設(shè)置比上面截圖中更好看的背景顏色 (define-key ac-completing-map "\M-n" 'ac-next) ;;; 列表中通過按M-n來向下移動 (define-key ac-completing-map "\M-p" 'ac-previous) (setq ac-auto-start 2) (setq ac-dwim t) (define-key ac-mode-map (kbd "M-TAB") 'auto-complete)
3. Rope and Ropemacs
非常棒的重構(gòu)工具,比如rename,move,extract method等等。還有非常好用的goto difinition(跳到定義),show documents(顯示文檔)等等。安裝Ropemacs前,必須先安裝rope和pymacs 。
rope的安裝方法:
python setup.py install
pymacs的安裝方法:
python setup.py install
.emacs中:
(autoload 'pymacs-apply "pymacs") (autoload 'pymacs-call "pymacs") (autoload 'pymacs-eval "pymacs" nil t) (autoload 'pymacs-exec "pymacs" nil t) (autoload 'pymacs-load "pymacs" nil t)
Ropmacs的安裝方法:
python setup.py install
.emacs中:
(pymacs-load "ropemacs" "rope-") (setq ropemacs-enable-autoimport t)
4. pycomplete
一個更加強(qiáng)大的智能提示工具,比如,輸入time.cl 然后按TAB鍵,會列出time模塊所有cl開頭的函數(shù)名。在調(diào)用函數(shù)時,還會在mini buffer中提示函數(shù)的參數(shù)類型。這個東西需要先安裝pymacs。
安裝方法:
1. 拷貝 python-mode.el and pycomplete.el 到Emacs的load_path中。
2. 拷貝 pycomplete.py 到PYTHONPATH (比如: c:/python25/Lib/site-packages)
3. .emacs中添加:
(require 'pycomplete) (setq auto-mode-alist (cons '("\\.py$" . python-mode) auto-mode-alist)) (autoload 'python-mode "python-mode" "Python editing mode." t) (setq interpreter-mode-alist(cons '("python" . python-mode) interpreter-mode-alist))
5. pdb調(diào)試
在Emacs中,通過M-x pdb可調(diào)出pdb對python代碼進(jìn)行調(diào)試。但是發(fā)現(xiàn)在Windows系統(tǒng)中,總進(jìn)入不了調(diào)試模式。主要原因有:
(1). windows中,找不到pdb.py位置。需自己制定pdb的路徑??梢酝ㄟ^下面的方法設(shè)置pdb的路徑:
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->;; pdb setup, note the python version (setq pdb-path 'c:/python25/Lib/pdb.py gud-pdb-command-name (symbol-name pdb-path)) (defadvice pdb (before gud-query-cmdline activate) "Provide a better default command line when called interactively." (interactive (list (gud-query-cmdline pdb-path (file-name-nondirectory buffer-file-name)))))
(2). windows中,調(diào)用pdb時,未使用python -i 參數(shù)。
針對上面兩個問題,我的解決辦法是,不設(shè)置pdb具體路徑,M-x pdb 回車后,出現(xiàn)下面命令:
Run pdb (like this): pdb
然后手動修改一下:
Run pdb (like this): python -i -m pdb test.py
這樣就搞定了。
6. 如何調(diào)試GAE程序
GAE是一個Web應(yīng)用,需要跨線程進(jìn)行調(diào)試,而pdb本身對線程調(diào)試支持不好。使用pdb進(jìn)行線程調(diào)試時,只有在需要調(diào)試的地方插入下面代碼:
import pdb pdb.set_trace()
然后直接運(yùn)行被調(diào)試代碼,而不是通過python pdb來執(zhí)行,就可以多線程代碼進(jìn)行調(diào)試了。
但是Google App Engine這樣的Web應(yīng)用,使用這個方法還是不能調(diào)試,和stdin和stdout有關(guān),最后找到一個很好的解決方法:
def set_trace(): import pdb, sys debugger = pdb.Pdb(stdin=sys.__stdin__, stdout=sys.__stdout__) debugger.set_trace(sys._getframe().f_back)
在任何需要調(diào)試的地方,調(diào)用上面的set_trace()函數(shù)。
相關(guān)文章
深度學(xué)習(xí)tensorflow基礎(chǔ)mnist
mnist作為深度學(xué)習(xí)中的HelloWorld,該小工程詳細(xì)描述了如何從零開始深度學(xué)習(xí),代碼詳細(xì),解釋全面,需要的朋友可以參考下2021-04-04Python函數(shù)使用的相關(guān)練習(xí)題分享
這篇文章主要介紹了Python函數(shù)使用的相關(guān)練習(xí)題分享,文章基于python函數(shù)內(nèi)容展開其相關(guān)例題,具有一定的參考價值,需要的小伙伴可以參考一下2022-05-05python list等分并從等分的子集中隨機(jī)選取一個數(shù)
這篇文章主要介紹了python list等分并從等分的子集中隨機(jī)選取一個數(shù),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11pytorch實(shí)現(xiàn)加載保存查看checkpoint文件
這篇文章主要介紹了pytorch實(shí)現(xiàn)加載保存查看checkpoint文件方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07Python OpenCV使用dlib進(jìn)行多目標(biāo)跟蹤詳解
這篇文章主要為大家介紹了如何使用 dlib 庫在實(shí)時視頻中有效地跟蹤多個對象,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)OpenCV有一定幫助,需要的可以參考一下2022-03-03Django中常用的查詢數(shù)據(jù)方法及查詢對象的條件詳解
在web 開發(fā)過程中,Django 與后臺數(shù)據(jù)庫的交互是必不可少的一項(xiàng),也是實(shí)現(xiàn)業(yè)務(wù)邏輯所需數(shù)據(jù)的重要方式,這篇文章主要給大家介紹了關(guān)于Django中常用的查詢數(shù)據(jù)方法及查詢對象條件的相關(guān)資料,需要的朋友可以參考下2021-09-09python神經(jīng)網(wǎng)絡(luò)Xception模型復(fù)現(xiàn)詳解
這篇文章主要為大家介紹了python神經(jīng)網(wǎng)絡(luò)Xception模型復(fù)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05