日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

emacs python开发环境_配置Emacs下的Python开发环境

發(fā)布時(shí)間:2025/3/21 python 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 emacs python开发环境_配置Emacs下的Python开发环境 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

特性

在Linux論壇上總有人問Python用什么IDE比較好,然后總會(huì)有人回答說Emacs。最近開始學(xué)Python,也花了點(diǎn)時(shí)間研究怎么配置Emacs,發(fā)現(xiàn)沒有想象中的那么麻煩。這篇文章大致上來自于Lei

Chen博客文章的翻譯,完成以后的Emacs具有以下特性:

自動(dòng)完成同一個(gè)文件內(nèi)的變量、函數(shù)

自動(dòng)完成python庫中的名稱

代碼重構(gòu)

模板展開功能

在線幫助系統(tǒng)

即時(shí)語法檢測

其他特性還包括自動(dòng)縮進(jìn),括號(hào)匹配,語法高亮,代碼折疊等等。其中最有用的莫過于自動(dòng)完成了,貌似很少有python編輯器可以做到這一點(diǎn)。而即時(shí)語法檢測讓emacs下的python代碼書寫變得像Eclipse一樣,一旦有錯(cuò)誤立刻就會(huì)高亮標(biāo)記出來。

如何安裝

首先你得在home目錄下有一個(gè).emacs配置文件,并且有一個(gè)用來放插件的文件夾(比如說~/.emacs.d/)

下載auto-completion.el到.emacs.d,并且在.emacs中添加如下幾行:?(require ‘a(chǎn)uto-complete)

(global-auto-complete-mode t)

下載yasnippet到.emacs.d并且編輯.emacs:

(require ‘yasnippet) (yas/initialize) (yas/load-directory “~/.emacs.d/snippets”)

下載python-mode.el并且放到.emacs.d中。我們會(huì)在之后的配置中用到它

設(shè)置Rope, Ropemacs

我們需要使用最新的development版的rope和ropemacs,否則在emacs中不能找到rope-completion函數(shù)。通過如下步驟安裝:

sudo apt-get install mercurial python-setuptools

mkdir /tmp/rope && cd /tmp/rope

hg clone http://bitbucket.org/agr/rope

hg clone http://bitbucket.org/agr/ropemacs

hg clone http://bitbucket.org/agr/ropemode

sudo easy_install rope

ln -s ../ropemode/ropemode ropemacs/

sudo easy_install ropemacs

http://pymacs.progiciels-bpi.ca/archives/ 下載 pymacs并安裝

安裝pyflacks以進(jìn)行自動(dòng)語法檢查:

sudo apt-get install pyflakes

把所有東西放到一塊兒

在你的.emacs.d中創(chuàng)建一個(gè)init_python.el,并且加入以下內(nèi)容:

(autoload 'python-mode "python-mode" "Python Mode." t)

(add-to-list 'auto-mode-alist '("\\.py\\'" . python-mode))

(add-to-list 'interpreter-mode-alist '("python" . python-mode))

(require 'python-mode)

(add-hook 'python-mode-hook

(lambda ()

(set-variable 'py-indent-offset 4)

;(set-variable 'py-smart-indentation nil)

(set-variable 'indent-tabs-mode nil)

(define-key py-mode-map (kbd "RET") 'newline-and-indent)

;(define-key py-mode-map [tab] 'yas/expand)

;(setq yas/after-exit-snippet-hook 'indent-according-to-mode)

(smart-operator-mode-on)

))

;; pymacs

(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)

;;(eval-after-load "pymacs"

;; '(add-to-list 'pymacs-load-path YOUR-PYMACS-DIRECTORY"))

(pymacs-load "ropemacs" "rope-")

(setq ropemacs-enable-autoimport t)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; Auto-completion

;;; Integrates:

;;; 1) Rope

;;; 2) Yasnippet

;;; all with AutoComplete.el

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun prefix-list-elements (list prefix)

(let (value)

(nreverse

(dolist (element list value)

(setq value (cons (format "%s%s" prefix element) value))))))

(defvar ac-source-rope

'((candidates

. (lambda ()

(prefix-list-elements (rope-completions) ac-target))))

"Source for Rope")

(defun ac-python-find ()

"Python `ac-find-function'."

(require 'thingatpt)

(let ((symbol (car-safe (bounds-of-thing-at-point 'symbol))))

(if (null symbol)

(if (string= "." (buffer-substring (- (point) 1) (point)))

(point)

nil)

symbol)))

(defun ac-python-candidate ()

"Python `ac-candidates-function'"

(let (candidates)

(dolist (source ac-sources)

(if (symbolp source)

(setq source (symbol-value source)))

(let* ((ac-limit (or (cdr-safe (assq 'limit source)) ac-limit))

(requires (cdr-safe (assq 'requires source)))

cand)

(if (or (null requires)

(>= (length ac-target) requires))

(setq cand

(delq nil

(mapcar (lambda (candidate)

(propertize candidate 'source source))

(funcall (cdr (assq 'candidates source)))))))

(if (and (> ac-limit 1)

(> (length cand) ac-limit))

(setcdr (nthcdr (1- ac-limit) cand) nil))

(setq candidates (append candidates cand))))

(delete-dups candidates)))

(add-hook 'python-mode-hook

(lambda ()

(auto-complete-mode 1)

(set (make-local-variable 'ac-sources)

(append ac-sources '(ac-source-rope) '(ac-source-yasnippet)))

(set (make-local-variable 'ac-find-function) 'ac-python-find)

(set (make-local-variable 'ac-candidate-function) 'ac-python-candidate)

(set (make-local-variable 'ac-auto-start) nil)))

;;Ryan's python specific tab completion

(defun ryan-python-tab ()

; Try the following:

; 1) Do a yasnippet expansion

; 2) Do a Rope code completion

; 3) Do an indent

(interactive)

(if (eql (ac-start) 0)

(indent-for-tab-command)))

(defadvice ac-start (before advice-turn-on-auto-start activate)

(set (make-local-variable 'ac-auto-start) t))

(defadvice ac-cleanup (after advice-turn-off-auto-start activate)

(set (make-local-variable 'ac-auto-start) nil))

(define-key py-mode-map "\t" 'ryan-python-tab)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; End Auto Completion

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Auto Syntax Error Hightlight

(when (load "flymake" t)

(defun flymake-pyflakes-init ()

(let* ((temp-file (flymake-init-create-temp-buffer-copy

'flymake-create-temp-inplace))

(local-file (file-relative-name

temp-file

(file-name-directory buffer-file-name))))

(list "pyflakes" (list local-file))))

(add-to-list 'flymake-allowed-file-name-masks

'("\\.py\\'" flymake-pyflakes-init)))

(add-hook 'find-file-hook 'flymake-find-file-hook)

(provide 'init_python)

在你的.emacs文件中添加:(load-library

“init_python”)

使用說明

碼自動(dòng)展開。比如說在編輯C/C++頭文件的時(shí)候鍵入once然后按tab,則會(huì)自動(dòng)展開#ifndef

__FILE_H__這樣的宏。這個(gè)功能是YASnippet提供的,在emacs的菜單中可以看到它的所有宏。展開以后再按tab鍵可以在已展開的代碼

之間切換位置。

自動(dòng)完成代碼。函數(shù)名輸入到一半按tab鍵會(huì)調(diào)用Rope和Ropemacs的功能自動(dòng)完成庫文件中的函數(shù)、變量名

C-c d 顯示python的doc string

C-c C-c 運(yùn)行當(dāng)前的文件

C-c ! 打開python shell

實(shí)上init_python.el做的事情是通過使用auto_complete.el中的功能,把YASnippet和Rope的功能結(jié)合到了

一塊兒,實(shí)現(xiàn)了類似TexMate的自動(dòng)完成等功能。其中YASnippet可以自定義模板,很是實(shí)用。Rope的功能還在研究中。

總結(jié)

以上是生活随笔為你收集整理的emacs python开发环境_配置Emacs下的Python开发环境的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。