作为一个挨踢老兵,最近终于有点闲暇时间,突然想重温一下emacs,玩玩python和人工智能。
实话说,听说归听说,自己真的开始配置emacs作为python的集成开发环境,还真是费了不少周折,通过姑娘找到的帮助基本上都没通,倒是最终通过google收到的文章靠谱。
参考资料:https://realpython.com/blog/python/emacs-the-best-python-editor/
一、准备工作
1、下载python
我下载的是2.7.13,貌似3以上的现在好多工具支持还不到位。
下载地址:https://www.python.org/downloads/release/python-2713/
我下载的是:Windows x86 MSI installer
2、下载emacs
我下载的emacs版本是24.3,下载地址:https://www.python.org/downloads/release/python-2713/
我选择的是:emacs-24.3-bin-i386.zip
二、emacs基础
刚接触不多,也就说说基础啦:)
emacs有很多命令,关于使用的最好的文档实际上应该是它自带的,打开emacs后,欢迎页就列出了各种帮助,文档质量很高,我觉得没必要导出去网上搜。
这些命令中,最重要的掌握俩事儿:
C-<chr>,表示按住Ctrl键同时再按住另一个键;M-<chr>,表示按住Alt键同时再按住另一个键
常见命令可参考:http://blog.chinaunix.net/uid-26263325-id-2998663.html
三、emacs基本配置
1、配置字体和字号
emacs默认用的不是中文字体,可能导致中文字体不显示,翻页的时候特别慢。
解决的办法是:
1)、打开菜单项:Options->set default font...,在对话框中选择字体、字号就行了;
2)、执行菜单项:Options->save options。执行完毕后,emacs会生成一个配置文件,并且把这些配置信息写到个人用户文件夹里面一个叫做.emacs文件,比如,在我的电脑上,配置文件的位置是:C:\Users\roy\AppData\Roaming\.emacs
用notepad++打开这个配置文件后,发现字体设置family文字后面是乱码,八成是编码问题,查阅资料后,发现修改一下emacs的默认编码比较好,我把emacs的默认编码修改成了UTF-8,此时.emacs的内容如下(据说这是一种叫做Lisp的语言):
(set-language-environment "UTF-8") (set-terminal-coding-system 'utf-8) (set-keyboard-coding-system 'utf-8) (set-clipboard-coding-system 'utf-8) (set-buffer-file-coding-system 'utf-8) (set-selection-coding-system 'utf-8) (modify-coding-system-alist 'process "*" 'utf-8) (custom-set-variables ;; custom-set-variables was added by Custom. ;; If you edit it by hand, you could mess it up, so be careful. ;; Your init file should contain only one such instance. ;; If there is more than one, they won't work right. ) (custom-set-faces ;; custom-set-faces was added by Custom. ;; If you edit it by hand, you could mess it up, so be careful. ;; Your init file should contain only one such instance. ;; If there is more than one, they won't work right. '(default ((t (:family "宋体" :foundry "outline" :slant normal :weight normal :height 120 :width normal)))))
修改完配置文件后,可以把乱码处直接改成中文“宋体“,再后面这个配置文件的中文就不会乱码啦。
四、配置emacs的样式和主题
默认的emacs编辑器的样式不太酷,我们可以把它搞得高大上一些,具体方法是:
打开.emacs文件,在后面添加下面的配置信息:
;; INSTALL PACKAGES ;; -------------------------------------- (require 'package) (add-to-list 'package-archives '("melpa" . "http://melpa.org/packages/") t) (package-initialize) (when (not package-archive-contents) (package-refresh-contents)) (defvar myPackages '(better-defaults material-theme)) (mapc #'(lambda (package) (unless (package-installed-p package) (package-install package))) myPackages) ;; BASIC CUSTOMIZATION ;; -------------------------------------- (setq inhibit-startup-message t) ;; hide the startup message (load-theme 'material t) ;; load material theme (global-linum-mode t) ;; enable line numbers globally
为emacs安装插件时,需要有安装包源地址,在安装elpy的过程中,找了好些地址都不好用,目前找到的MELPA,地址是:http://melpa.org/packages/,这个挺好用。
上述配置为emacs安装了包better-defaults和material-theme,他们改变了emacs默认的布局和样式。
以上的配置项修改完毕后,重启emacs,此时应该变样了,参见下图:
感觉是酷了一点点。
五、Elpy-Python开发环境,支持代码自动补全...
据python高手说,elpy这东西非常好,对python开发帮助很大。
为了安装elpy,需要先安装它所依赖的库,打开cmd窗口,在里面执行下面的安装命令:
# Either of these pip install rope pip install jedi # flake8 for code checks pip install flake8 # importmagic for automatic imports pip install importmagic # and autopep8 for automatic PEP8 formatting pip install autopep8 # and yapf for code formatting pip install yapf
这些命令也可以一句话搞定:
pip install jedi flake8 importmagic autopep8
安装成功后,打开.emacs配置文件,增加elpy的安装配置:
(defvar myPackages '(better-defaults elpy ;; add the elpy package material-theme))
配置修改完毕后,重新启动emacs。
本来以为elpy已经装上了,可是实际上好像不行。为了以防万一,我们再装一下。
在emacs中输入 M-x,即按住alt键同时按下x,然后输入:
list-package
再按下回车后,发现很多melpa的包,林林总总。
再按下 M-x,输入:
pacakge-install
再提示后输入:elpy,等一会眼花缭乱的下载和安装后,一般能装完,我自己试验的时候,第一次网络出错了,第二次再次安装时,成功了。
六、验证elpy是否安装成功
修改.emacs,增加自动加载elpy的配置:
(elpy-enable)
重启emacs。
打开菜单:File->Visit New File...,在弹出的窗口中选择一个文件位置,输入python文件名,比如:test.py,点击打开,此时如果不存在test.py时,会新建一个文件。
输入下图所示的代码,如果发现小框弹出来,恭喜我们终于把elpy装好了,一个比较好的python的IDE搞出来了:)
七、我的emacs for python配置
(set-language-environment "UTF-8") (set-terminal-coding-system 'utf-8) (set-keyboard-coding-system 'utf-8) (set-clipboard-coding-system 'utf-8) (set-buffer-file-coding-system 'utf-8) (set-selection-coding-system 'utf-8) (modify-coding-system-alist 'process "*" 'utf-8) (custom-set-variables ;; custom-set-variables was added by Custom. ;; If you edit it by hand, you could mess it up, so be careful. ;; Your init file should contain only one such instance. ;; If there is more than one, they won't work right. ) (custom-set-faces ;; custom-set-faces was added by Custom. ;; If you edit it by hand, you could mess it up, so be careful. ;; Your init file should contain only one such instance. ;; If there is more than one, they won't work right. '(default ((t (:family "宋体" :foundry "outline" :slant normal :weight normal :height 120 :width normal))))) ;; INSTALL PACKAGES ;; -------------------------------------- (require 'package) (add-to-list 'package-archives '("melpa" . "http://melpa.org/packages/") t) (package-initialize) (when (not package-archive-contents) (package-refresh-contents)) (defvar myPackages '(better-defaults material-theme)) (mapc #'(lambda (package) (unless (package-installed-p package) (package-install package))) myPackages) ;; BASIC CUSTOMIZATION ;; -------------------------------------- (setq inhibit-startup-message t) ;; hide the startup message (load-theme 'material t) ;; load material theme (global-linum-mode t) ;; enable line numbers globally (defvar myPackages '(better-defaults elpy ;; add the elpy package material-theme)) (elpy-enable)
新手上路,有不妥的地方,欢迎不吝赐教。