国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

將Emacs打造成強(qiáng)大的Python代碼編輯工具

2019-11-25 17:01:34
字體:
供稿:網(wǎng)友

基本配置

Emacs本身提供了python-mode,輸入M-x python-mode,就可以進(jìn)入python模式。相應(yīng)地,會(huì)在菜單欄出現(xiàn)Python菜單。當(dāng)然,一般來講,如果是.py文件打開的話,也會(huì)自動(dòng)進(jìn)入該模式。
        不過,默認(rèn)的python模式功能上面用起來還是有點(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)目主頁上面。
        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,這樣會(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)行代碼檢測;
大體的使用方式就是這樣的了,另外,還有許多類或函數(shù)的模板可以通過快捷鍵進(jìn)行,在今后常用的時(shí)候會(huì)加強(qiáng)了解的。感謝你能看到這里!

安裝擴(kuò)展
在Emacs中,通過各種擴(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è)列表框讓你去選擇。

20151120142324649.png (414×309)

安裝方法:

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來向下移動(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 '("http://.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對(duì)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時(shí),未使用python -i 參數(shù)。

針對(duì)上面兩個(gè)問題,我的解決辦法是,不設(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 pdbpdb.set_trace()

然后直接運(yùn)行被調(diào)試代碼,而不是通過python pdb來執(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ù)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 紫金县| 麻江县| 黎平县| 长顺县| 屏山县| 德安县| 浮山县| 称多县| 安平县| 邵阳县| 平潭县| 纳雍县| 丹巴县| 柳江县| 泰宁县| 东方市| 江津市| 丰宁| 丰台区| 安国市| 治县。| 朔州市| 如皋市| 大冶市| 五华县| 闽侯县| 丹寨县| 蒙自县| 肃北| 会昌县| 思茅市| 聂拉木县| 虎林市| 鸡泽县| 隆林| 金秀| 余庆县| 同德县| 泰来县| 驻马店市| 巩留县|