將Emacs打造成強(qiáng)大的Python代碼編輯工具
基本配置
Emacs本身提供了python-mode,輸入M-x python-mode,就可以進(jìn)入python模式。相應(yīng)地,會(huì)在菜單欄出現(xiàn)Python菜單。當(dāng)然,一般來(lái)講,如果是.py文件打開的話,也會(huì)自動(dòng)進(jìn)入該模式。
不過(guò),默認(rèn)的python模式功能上面用起來(lái)還是有點(diǎn)弱,而且許多地方做的并不好,最好下載第三方的python模式。python-mode是一個(gè)開源項(xiàng)目,可以在https://launchpad.net/python-mode進(jìn)行下載。
1.安裝
1).安裝prog-modes:
aptitude install prolog-el
2).下載python-mode.el文件在項(xiàng)目主頁(yè)上面。
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))
檢測(cè)擴(kuò)展是否加載路徑,測(cè)試方法: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,這樣會(huì)在新的窗口及緩沖區(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)行代碼檢測(cè);
大體的使用方式就是這樣的了,另外,還有許多類或函數(shù)的模板可以通過(guò)快捷鍵進(jìn)行,在今后常用的時(shí)候會(huì)加強(qiáng)了解的。感謝你能看到這里!
安裝擴(kuò)展
在Emacs中,通過(guò)各種擴(kuò)展,打造強(qiáng)大的Python IDE環(huán)境,包括Snippet工具,智能提示,自動(dòng)補(bǔ)全,重構(gòu)工具,調(diào)試以及GAE的調(diào)試,等等。以下各工具的安裝前提是你對(duì)Emacs的配置文件有一定的了解,所有相關(guān)的el文件都必須放在load_path能夠加載的地方。
1. YASnippet
snippet工具,可自定義一些模板,必不可少的好東西!看了下面這個(gè)很酷的演示動(dòng)畫就明白了:
http://yasnippet.googlecode.com/files/yasnippet.avi
安裝方法:
(require 'yasnippet) (yas/initialize) (yas/load-directory "~/.emacs.d/plugins/yasnippet-0.6.1c/snippets")
2. AutoComplete
自動(dòng)完成工具,會(huì)像VS里一樣,彈出一個(gè)列表框讓你去選擇。

安裝方法:
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) ;;; 列表中通過(guò)按M-n來(lái)向下移動(dòng) (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
一個(gè)更加強(qiáng)大的智能提示工具,比如,輸入time.cl 然后按TAB鍵,會(huì)列出time模塊所有cl開頭的函數(shù)名。在調(diào)用函數(shù)時(shí),還會(huì)在mini buffer中提示函數(shù)的參數(shù)類型。這個(gè)東西需要先安裝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中,通過(guò)M-x pdb可調(diào)出pdb對(duì)python代碼進(jìn)行調(diào)試。但是發(fā)現(xiàn)在Windows系統(tǒng)中,總進(jìn)入不了調(diào)試模式。主要原因有:
(1). windows中,找不到pdb.py位置。需自己制定pdb的路徑??梢酝ㄟ^(guò)下面的方法設(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時(shí),未使用python -i 參數(shù)。
針對(duì)上面兩個(gè)問(wèn)題,我的解決辦法是,不設(shè)置pdb具體路徑,M-x pdb 回車后,出現(xiàn)下面命令:
Run pdb (like this): pdb
然后手動(dòng)修改一下:
Run pdb (like this): python -i -m pdb test.py
這樣就搞定了。
6. 如何調(diào)試GAE程序
GAE是一個(gè)Web應(yīng)用,需要跨線程進(jìn)行調(diào)試,而pdb本身對(duì)線程調(diào)試支持不好。使用pdb進(jìn)行線程調(diào)試時(shí),只有在需要調(diào)試的地方插入下面代碼:
import pdb pdb.set_trace()
然后直接運(yùn)行被調(diào)試代碼,而不是通過(guò)python pdb來(lái)執(zhí)行,就可以多線程代碼進(jìn)行調(diào)試了。
但是Google App Engine這樣的Web應(yīng)用,使用這個(gè)方法還是不能調(diào)試,和stdin和stdout有關(guān),最后找到一個(gè)很好的解決方法:
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)文章
利用Python第三方庫(kù)實(shí)現(xiàn)預(yù)測(cè)NBA比賽結(jié)果
今天給大家?guī)?lái)的是關(guān)于Python的相關(guān)知識(shí),文章圍繞著利用Python實(shí)現(xiàn)預(yù)測(cè)NBA比賽結(jié)果展開,文中有非常詳細(xì)的介紹,需要的朋友可以參考下2021-06-06
Python使用Pandas庫(kù)實(shí)現(xiàn)MySQL數(shù)據(jù)庫(kù)的讀寫
這篇文章主要介紹了Python使用Pandas庫(kù)實(shí)現(xiàn)MySQL數(shù)據(jù)庫(kù)的讀寫 ,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
詳解Python3.6安裝psutil模塊和功能簡(jiǎn)介
這篇文章主要介紹了詳解Python3.6安裝psutil模塊和功能簡(jiǎn)介,詳細(xì)的介紹了安裝psutil模塊和該模塊的使用,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05

