emacs中集成Python

来源:互联网 发布:桂林力港公司 知乎 编辑:程序博客网 时间:2024/04/27 22:02

1. 安装YASnippet

YASnippet是一个模板自动生成工具。安装YASnippet只需M-x list-package,之后选择YASnippet并且安装即可。之后在.emacs文件中写入

===========================================================

(add-to-list 'load-path "~/.emacs.d/elpa/yasnippet/")

(require 'yasnippet)

(yas-global-mode 1)

============================================================


2. 安装auto-completion.el

首先进入auto-completion.el的官网(自己搜),把auto-complete的安装包下下来,然后根据安装文档做就行了,之后在.emacs中添加(很多网上把(add-to-list 'ac-dictionary-directories "~/.emacs.d/ac-dict") 放到上面去,结果报错。解决方案就是放到最后一行,让(require 'auto-complete-config)先执行。)

=========================================================================

(add-to-list 'load-path "~/.emacs.d")
(require 'auto-complete-config)
(ac-config-default)
(add-to-list 'ac-dictionary-directories "~/.emacs.d/ac-dict") 

==============================================================================


3. 安装python-mode。安装同auto-completion.el,把包下下来放到~/.emacs.d/plugins中。然后在~/.emacs文件中添加如下内容

==============================================

(add-to-list 'load-path "~/.emacs.d/plugins/python-mode.el-6.1.3")
(require 'python-mode)
(add-to-list 'auto-mode-alist '("\.py\'" . python-mode))

==============================================


4. 安装ipython。

首先安装ipython:

# yum install ipython

emac24 以上在Python.el中带有ipython,直接激活就行。在~/.emacs中添加:

============================

require 'python)
(setq python-shell-interpreter "ipython")
(setq python-shell-interpreter-args "--pylab")

============================


5. 安装python的帮助文档

首先,下载python的HTML帮助文档http://docs.python.org/download.html(可能被墙);之后,从https://github.com/tsgates/pylookup下载pylookup,参考README文档(注意可以更新或者添加帮助文档);生成.db帮助文档,$./pylookup.py -u python-2.7.1-docs-html;之后,将pylookup.py、pylookup.db和pylookup.el放入~/.emacs.d的文件夹下,比如放入“~/emacs.d/plugins/pylookup”;修改.emacs文档,设定查找快捷键C-c h

==============================================================

(setq pylookup-dir "~/.emacs.d/plugins/pylookup")

(add-to-list 'load-path pylookup-dir)

(require 'pylookup)


;; load pylookup when compile time

(eval-when-compile (require 'pylookup))


;; set executable file and db file

(setq pylookup-program (concat pylookup-dir "/pylookup.py"))

(setq pylookup-db-file (concat pylookup-dir "/pylookup.db"))


;; set search option if you want

;; (setq pylookup-search-options '("--insensitive" "0" "--desc" "0"))


;; to speedup, just load it on demand

(autoload 'pylookup-lookup "pylookup"

  "Lookup SEARCH-TERM in the Python HTML indexes." t)


(autoload 'pylookup-update "pylookup" 

  "Run pylookup-update and create the database at `pylookup-db-file'." t)

(global-set-key "\C-ch" 'pylookup-lookup)

============================================================              


6. 安装其他自动补全

首先,安装pylint和pep8

# yum install pylint

# easy_install pep8

安装之后,下载pylint和pep8的源码包,放到plugins中,之后在~/.emacs中添加:pep8只能下载到python-pep8.el,直接扔到plugins中

=========================================================

(add-to-list 'load-path "~/.emacs.d/plugins/pylint-1.3.0/elisp")
(require 'pylint)

(add-to-list 'load-path "~/.emacs.d/plugins/")
(require 'python-pep8)

==========================================================


7. 安装anything

根据以下网站安装anything: http://www.emacswiki.org/emacs/Anything#toc5。一种方法是将anything-config包整体下载在~/.emacs.d/plugins/文件夹下。之后,下载anything-ipython.el文件http://www.emacswiki.org/emacs-en/download/anything-ipython.el,存放至anything-config文件夹下。在.emacs文件夹下添加:

================================================

(add-to-list 'load-path "~/.emacs.d/plugins/anything-config")

(require 'anything-config)

(require 'anything-ipython)

(when (require 'anything-show-completion nil t)

   (use-anything-show-completion 'anything-ipython-complete

                                 '(length initial-pattern)))

================================================


解决bug:

1. 在运行ipython时候,可能会出现“File mode specification error: (file-error "Cannot open load file" "~/.emacs.d/abbrev_defs")”的错误,这是因为缺少一个名为“abbrev_defs”的文件。相关Bug讨论https://bugs.launchpad.net/python-mode/+bug/1015713解决办法:M-x edit-abbrevs,之后保存,就会出现

“abbrev_defs”文件。

2. 运行pep8时候,可能会出现“Symbol's function definition is void: tramp-tramp-file-p”的提示,bug讨论http://x.thexs.us/2011/06/17/pep8-error-tramp-tramp-file-p/解决办法:在.emacs文件中添加(require 'tramp)即可。

3. 使用C-c C-c启动ipython时,会出现“Symbol's value as variable is void: py-mode-map”的报错。但是,再次执行以下C-c C-c之后,就可以启动ipython。不知道这个bug产生的原因和解决办法


使用方法:

1. C-c C-c:将整个buffer送入ipython执行;

2. 选定要运行的区域,之后C-c |:ipython中执行选定的区域;

3. M-p/M-n:自动补全时上下选择;

4. TAB:补全;

5. M-x pep8:运行检查pep8代码风格;M-x ':跳到下一个代码错误处;M-x pylint:作用类似pep8;

6. C-c h:查询帮助文档HTML格式;



参考网址:

1. http://ranjiao.com/blog/index.php/2009/配置emacs下的python开发环境/

2. http://hide1713.wordpress.com/2009/01/30/setup-perfect-python-environment-in-emacs/

3. http://pedrokroger.com/2010/07/configuring-emacs-as-a-python-ide-2/

4. 关于emacs设置ipythonhttp://ipython.org/ipython-doc/stable/config/editors.html

5. http://www.cnblogs.com/bamanzi/archive/2011/07/10/emacs-for-python-wishlist.html

6. 视频介绍 https://www.youtube.com/watch?v=6BlTGPsjGJk

0 0
原创粉丝点击