Win平台下Python+Emacs搭建Python编程环境

所需工具

安装工具

安装 Git

默认安装,把 bin 文件夹加入系统环境变量 path。

安装 Gnutls

默认安装,同样把 bin 文件夹加入系统环境变量 path。

安装 Python

我把 Python 安装在 C 盘根目录下,把 bin 文件夹加入系统环境变量 path(3.0 以上的 Python 有加入的选项,注意勾选)。

安装 Python 辅助工具

  • Setuptools

    setuptools 是 Python distutils 增强版的集合,它可以帮助我们更简单的创建和分发 Python 包,尤其是拥有依赖关系的。

  • ipython

    一个增强版的Shell。

  • pip

    安装和管理 Python 包的工具。

  • virtualenv

一个 python 工具. 它可以创建一个独立的 python 环境. 这样做的好处是你的 python 程序运行在这个环境里, 不受其它的 python library 的版本问题影响。

Setuptools、pip

3.0 以上版本的 Python,Setuptools、pip 已自带,无需单独安装。

安装 ipython
  1. 下载 IPython 的二进制安装包,地址在这里。注意选择对应的 Python 版本和平台。

  2. 因为是 Windows 下的,为了方便起见,直接下载二进制包,双击安装后,IPython 的安装软件会自动选择在注册表中注册的 Python 路径,并进行安装。

  3. 将 Python 目录下的 Scripts 目录添加到环境变量中,如..\Python3.x\Scripts 这样的。这样就能够快速在命令行启动 ipython 了。

  4. 启动 ipython,你会注意到警告信息,大概的意思是,windows 下没有 readline 模块可用,所以 tab 的自动补全以及 console 的颜色样式是“NoColor“。在这里可以下载到 pyreadline 包。同样,注意安装包对应的平台,安装方式与 IPython 相同。

PS: 这里我安装的是3.5版本的 Python,readline 还不支持此版本,可以不安装。

安装 virtualenv

打开 Console,进入 Python 安装目录的 Scripts 文件夹,输入以下命令:

1
2
lenovo@LENOVO-PC C:\Python35-32\Scripts
$ pip install virtualenv==1.11.1

出现以下提示,说明安装成功:

1
2
3
4
5
Collecting virtualenv==1.11.1
  Using cached virtualenv-1.11.1.tar.gz
Installing collected packages: virtualenv
  Running setup.py install for virtualenv
Successfully installed virtualenv-1.11.1

PS: 安装的时候如果不设定==1.11.1,默认会安装最新版本,而新版本的 virtualenv,在创建环境的时候会失败,用1.11.1版本,没有这个问题,所以我们安装的时候需要明确版本1.11.1。

安装完毕 virtualenv 之后,就可以创建虚拟环境了:

1
2
lenovo@LENOVO-PC C:\Python35-32\Scripts
$ virtualenv.exe vProject

出现以下提示,说明安装成功:

1
2
3
Using base prefix 'c:\\python35-32'
New python executable in vProject\Scripts\python.exe
Installing setuptools, pip...done.

安装 Emacs

解压即可,一般解压到非系统盘的根目录下。我的是 F:\Emacs

配置 Emacs 的 Home 路径

因为 Windows 没有 Home 目录的概念,所以,Emacs 就按如下方式来查找配置文件:

  1. 如果设置了 Home 环境变量,那么就用它的值作为 home 目录;

  2. 如果存在注册表键值 HKCU\SOFTWARE\GNU\Emacs\HOME,就用它的值作为 Home 目录;

  3. 如果存在注册表键值 HKLM\SOFTWARE\GNU\Emacs\HOME,就用它的值作为 Home 目录(和2的区别是,2只是针对当前用户的注册表路径,3则是针对所有用户);

  4. 如果存在 C:\.emacs,就用 C:\ 作为home目录;

  5. 如果以上都不存在的话,就使用 \Users\\AppData\Roaming 作为 Home 目录(对于XP和较早windows用户,需要到 Documents and Settings 目录下去找)。

按方法 1 设置 Home 环境变量后,很多从 Linux 移植到 Win 平台下的软件都会使用这个环境变量,到时这个目录里面就会开杂货铺子,乱哄哄一片。按方法 5,里面更乱。最理想的办法就是注册表面摆一道,操作简单,而且这个目录里包括了 Emacs 所有文件。操作步骤为:

  1. Win 键 + r 打开运行对话框,输入 regedit,打开注册表;

  2. 找到 HKCU\SOFTWARE\GNU\,如果没有 GNU 项,就新建一个;

  3. 新建子项 Emacs,在该项下新建字符值 HOME,其数值数据为 F:\Emacs\(这是我的 Emacs 安装路径),导出该项后是这样的:

1
2
[HKEY_LOCAL_MACHINE\SOFTWARE\GNU\Emacs]
"Home"="F:\\Emacs\\"

配置 Emacs 及安装插件

Emacs 中的插件管理我使用的是 ELPA ,所以只要把 .emacs 文件复制到 Emacs 文件夹中,然后启动 Emacs 就自动安装所有插件了。我的 .emacs 配置内容为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
(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-safe-themes (quote ("26614652a4b3515b4bbbb9828d71e206cc249b67c9142c06239ed3418eff95e2" default)))
'(scroll-bar-mode nil)
'(tool-bar-mode nil))

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



;;;; < ==================== package system,ELPA ==================== >

;;;; Usage -------------------------------------------------+
;;;; |
;;;; Update the package list,and show it. |
;;;; M-x package-list-packages or list-packages |
;;;; |
;;;; Save the package list in local. |
;;;; M-x package-refresh-contents |
;;;; |
;;;; Read the local package list. |
;;;; package-list-packages-no-fetch |
;;;; |
;;;; Search the package list. |
;;;; 'C-s' or 'C-r' |
;;;; |
;;;; Mark to install: 'i'; Mark to uninstall: 'd'. |
;;;; Execute the mark: 'x'; Cancel the mark: 'u'. |
;;;; |
;;;; Install a package. |
;;;; M-x package-install RET |
;;;; xxxxx (name of package) RET |
;;;; |
;;;; Update all packages. |
;;;; M-x package-refresh-contents(first update list) |
;;;; M-x package-menu-mark-upgrades |
;;;; |
;;;; -------------------------------------------------------+

(require 'cl)
(require 'package)
(add-to-list 'package-archives '("marmalade" . "http://marmalade-repo.org/packages/") t)
(setq package-archives '(("gnu" . "http://elpa.gnu.org/packages/")
("marmalade" . "http://marmalade-repo.org/packages/")
("melpa" . "http://melpa.milkbox.net/packages/")))

(package-initialize)

;;;; < --------------- 更新包后,自动更新加载路径 --------------- >
(defun package-update-load-path ()
"Update the load path for newly installed packages."
(interactive)
(let ((package-dir (expand-file-name package-user-dir)))
(mapc (lambda (pkg)
(let ((stem (symbol-name (car pkg)))
(version "")
(first t)
path)

(mapc (lambda (num)
(if first
(setq first nil)
(setq version (format "%s." version)))

(setq version (format "%s%s" version num)))

(aref (cdr pkg) 0))

(setq path (format "%s/%s-%s" package-dir stem version))
(add-to-list 'load-path path)))

package-alist))
)


;;;; < ==================== auto installed package ==================== >

;;;; << evil >>----------------------------------------------+
;;;; Evil is an extensible vi layer for Emacs. |
;;;; It emulates the main features of Vim, |
;;;; and provides facilities for writing custom extensions. |
;;;; --------------------------------------------------------+

;;;; << autopair >>------------------------------------------+
;;;; It can be useful to insert parentheses, braces, quotes |
;;;; and the like in matching pairs - e.g., |
;;;; pressing "(" inserts '()', with the cursor in between. |
;;;; --------------------------------------------------------+

;;;; << pangu-spacing >>-------------------------------------+
;;;; pangu-spacing-mode is an minor-mode to auto add space |
;;;; between Chinese and English characters. |
;;;; --------------------------------------------------------+

;;;; << jedi >>----------------------------------------------+
;;;; Jedi.el is a Python auto-completion package for Emacs. |
;;;; It aims at helping your Python coding in a non-destruc- |
;;;; tive way. It also helps you to find information about |
;;;; Python objects, such as docstring, function arguments |
;;;; and code location. |
;;;; --------------------------------------------------------+

;;;; << neotree >>-------------------------------------------+
;;;; A Emacs tree plugin like NerdTree for Vim. |
;;;; --------------------------------------------------------+

;;;; << color-theme >>---------------------------------------+
;;;; EmacsLisp library color-theme.el provides many prede- |
;;;; fined color themes for your use, and you can easily |
;;;; define additional themes. |
;;;; --------------------------------------------------------+

;;;; << auto-complete >>-------------------------------------+
;;;; This extension provides a way to complete with popup |
;;;; menu like: |
;;;; |
;;;; def-!- |
;;;; +-----------------+ |
;;;; |defun::::::::::::| |
;;;; |defvar | |
;;;; |defmacro | |
;;;; | ... | |
;;;; +-----------------+ |
;;;; --------------------------------------------------------+

;;;; << yasnippet >>-----------------------------------------+
;;;; YASnippet is a template system for Emacs. It allows you |
;;;; to type an abbreviation and automatically expand it in- |
;;;; to function templates. |
;;;; --------------------------------------------------------+

;;;; << markdown-mode >>-------------------------------------+
;;;; markdown-mode is a major mode for GNU Emacs which pro- |
;;;; vides syntax highlighting and supporting commands for |
;;;; editing Markdown files. |
;;;; --------------------------------------------------------+

;;;; << flycheck >>------------------------------------------+
;;;; Flycheck is a modern on-the-fly syntax checking exten- |
;;;; sion for GNU Emacs 24, intended as replacement for the |
;;;; older Flymake extension which is part of GNU Emacs. |
;;;; Before use flycheck, Must install pylint like: |
;;;; |
;;;; > sudo pip install pylint |
;;;; |
;;;; --------------------------------------------------------+

;;;; << switch-window >>-------------------------------------+
;;;; Just switch window! 'C-x o', and press the number. |
;;;; --------------------------------------------------------+


(when (not (package-installed-p 'evil))
(package-refresh-contents)
(package-install 'evil))


(when (not (package-installed-p 'autopair))
(package-refresh-contents)
(package-install 'autopair))


(when (not (package-installed-p 'pangu-spacing))
(package-refresh-contents)
(package-install 'pangu-spacing))


(when (not (package-installed-p 'jedi))
(package-refresh-contents)
(package-install 'jedi))


(when (not (package-installed-p 'neotree))
(package-refresh-contents)
(package-install 'neotree))


(when (not (package-installed-p 'color-theme))
(package-refresh-contents)
(package-install 'color-theme))


(when (not (package-installed-p 'auto-complete))
(package-refresh-contents)
(package-install 'auto-complete))


(when (not (package-installed-p 'yasnippet))
(package-refresh-contents)
(package-install 'yasnippet))


(when (not (package-installed-p 'markdown-mode))
(package-refresh-contents)
(package-install 'markdown-mode))


(when (not (package-installed-p 'flycheck))
(package-refresh-contents)
(package-install 'flycheck))



(when (not (package-installed-p 'switch-window))
(package-refresh-contents)
(package-install 'switch-window))



;;;; < ==================== 非内嵌包配置 ==================== >

;;;; < --------------- evil --------------- >
(require 'evil)
(evil-mode 1)
(provide 'init-evil)

;;;; < --------------- autopair --------------- >
(require 'autopair)
(autopair-global-mode 1)

;;;; < --------------- auto-complete --------------- >
;;;; 貌似 auto-complete 配置需放置到 jedi 前面
(require 'auto-complete-config)
(ac-config-default)
(define-key ac-completing-map (kbd "C-n") 'ac-next)
(define-key ac-completing-map (kbd "C-p") 'ac-previous)

;;;; < --------------- jedi --------------- >
(add-hook 'python-mode-hook 'jedi:setup)
(setq jedi:setup-keys t)
(setq jedi:complete-on-dot t)
(when (fboundp 'jedi:setup) (jedi:setup))

;;;; < --------------- neotree --------------- >
;;;; if install evil, must write the following lines in .emacs.
(require 'neotree)
(global-set-key [f8] 'neotree-toggle)
(add-hook 'neotree-mode-hook
(lambda ()
(define-key evil-normal-state-local-map (kbd "TAB") 'neotree-enter)
(define-key evil-normal-state-local-map (kbd "SPC") 'neotree-enter)
(define-key evil-normal-state-local-map (kbd "q") 'neotree-hide)
(define-key evil-normal-state-local-map (kbd "RET") 'neotree-enter)))


;;;; < --------------- color-theme --------------- >
;;;; 如果加载 theme 失败,更换正确路径后,去掉下面代码前面注释符。
;;;; (add-to-list 'custom-theme-load-path "~/.emacs.d/elpa/color-theme-20080305.34")
(require 'color-theme)
(color-theme-initialize)
;;;; If color-theme doesn’t seem to work, try adding one more line.
;;;; (setq color-theme-is-global t)
(color-theme-subtle-hacker)

;;;; < --------------- yasnippet --------------- >
(require 'yasnippet)
(yas-global-mode 1)

;;;; < --------------- markdown-mode --------------- >
;;;; 识别 .markdown .md 扩展名的文件
(autoload 'markdown-mode "markdown-mode"
"Major mode for editing Markdown files" t)

(add-to-list 'auto-mode-alist '("\\.markdown\\'". markdown-mode))
(add-to-list 'auto-mode-alist '("\\.md\\'". markdown-mode))

;;;; < --------------- pangu-spacing -------------->
(require 'pangu-spacing)
(global-pangu-spacing-mode 1)
(setq pangu-spacing-real-insert-separtor t)

;;;; < --------------- flycheck --------------->
(add-hook 'after-init-hook #'global-flycheck-mode)

;;;; < --------------- switch-window --------------- >
;;;;Rebind zC-x o key:
(global-set-key (kbd "C-x o") 'switch-window)
(require 'switch-window)


;;;; < ==================== 内嵌包配置 ==================== >

;;;; 把这几行加入 .emacs 就可以把 C-x C-b 那个普通的 buffer menu
;;;; 换成非常方便的 ibuffer。
(require 'ibuffer)
(global-set-key (kbd "C-x C-b") 'ibuffer)

;;;; 打开 iswitchb 模式,C-x b 快捷键后,
;;;; 所有 buffer 名字排列在 minibuffer,
(iswitchb-mode 1)

;;;; ido 的配置,用 C-x C-f 打开文件时会有提示;
(ido-mode t)

;;;; eshell 清屏命令 cls
(defun eshell/cls()
; "to clear the eshell buffer."
(interactive)
(let ((inhibit-read-only t))
(erase-buffer)))



;;;; < ==================== GUI ==================== >

;;;; 若要将注释改为斜体,可采用以下代码:
(make-face-italic 'font-lock-comment-face)

;;;; Setting English Font(设置英文字体)
(set-face-attribute
'default nil :font "Yahei Consolas Hybrid 14")

;; Chinese Font(设置中文字体)
(dolist (charset '(kana han symbol cjk-misc bopomofo))
(set-fontset-font (frame-parameter nil 'font)
charset
(font-spec :family "微软雅黑" :size 14)))


;;;; 全局显示行号
(global-linum-mode 1)

;;;; 设置标题栏显示文件完整路径名
(setq frame-title-format
'("< GNU Emacs > SWBeta is modifying... %S" (buffer-file-name "%f"
(dired-directory dired-directory "%b"))))


;;;; 默认显示 80 列换行
(setq default-fill-column 80)

;;;; 时间使用 24 小时制
(setq display-time-24hr-format t)

;;;; 时间显示包括日期和时间
(setq display-time-day-and-date t)

;;;; 显示时间的格式
(setq display-time-format "%m 月%d 日%A%H:%M")

;;;; 60 秒更新一次时间
(setq display-time-interval 60)

;;;; 显示时间,最后写这句,
;;;; 否则 emacs 会先显示默认时间格式,然后显示自定义格式
(display-time)

;;;; 显示光标当前所在的行号和列号
(column-number-mode t)

;;;; 高亮当前行并设置背景色(没找到合适对比色,高亮无意义)
;;;;(global-hl-line-mode t)
;;;; 设置高亮行的背景颜色
;;;;(set-face-background hl-line-face "#F08080")

;;;; 不显示工具栏
;;;;(tool-bar-mode -1)
;;;; 不显示滚动条
;;;;(scroll-bar-mode 0)
;;;; 以上 2 行设置,最好在位于 .emacs 最开始
;;;; 否则,工具栏、scrollbar 会闪现。
;;;; 已通过 menu 设为 Emacs 默认,这里注释掉

;;;; 启动 Emacs 后,指定窗口(frame)大小
;;;; arrange-frame (w h x y) 函数的w h x y分别是frame的宽、高、左上角x坐标
(defun arrange-frame (w h x y)
"Set the width, height, and x/y position of the current frame"
(let ((frame (selected-frame)))
(delete-other-windows)
(set-frame-position frame x y)
(set-frame-size frame w h)))

(arrange-frame 120 30 100 50)

;;;; 设置函数,Ctr+x Ctr+a 最大化窗口,Ctr+x a 最小化窗口
(defun emacs-maximize ()
"Maximize emacs window in windows os"
(interactive)
(w32-send-sys-command 61488)) ; WM_SYSCOMMAND #xf030 maximize

(defun emacs-minimize ()
"Minimize emacs window in windows os"
(interactive)
(w32-send-sys-command #xf020)) ; #xf020 minimize

(defun emacs-normal ()
"Normal emacs window in windows os"
(interactive)
(w32-send-sys-command #xf120)) ; #xf120 normalimize

;; (emacs-maximize)
(global-set-key (kbd "C-x C-a") 'emacs-maximize)
(global-set-key (kbd "C-x a") 'emacs-minimize)


;;;; < ==================== 功能类 ==================== >

;;;; 设置 Emacs 默认工作目录
(setq command-line-default-directory "F:\code" )

;;;; 括号匹配时显示另外一边的括号,而不是跳到另一个括号
(show-paren-mode t)
(setq show-paren-style 'parentheses)

;;;; 不生成备份文件
(setq make-backup-files nil)

;;;;不生成#F#文件
(setq auto-save-default nil)

;;;; 光标靠近鼠标指针时,鼠标指针自动避让。
(mouse-avoidance-mode 'animate)

;;;; 消除 ring-bell 的效果。
(setq ring-bell-function 'ignore)

;;;; 关出错时的提示声
(setq visible-bell t)

;;;; 改变 Emacs 要求回答 yes 的行为。按 y 或空格键表示 yes,n 表示 no。
(fset 'yes-or-no-p 'y-or-n-p)

;;;; 较大的 kill ring
(setq kill-ring-max 200)

;;;; 支持 emacs 和外部程序的粘贴
(setq x-select-enable-clipboard t)

;;;; 用 M-x 执行某个命令的时候,在输入的同时给出可选的命令名提示
(icomplete-mode 1)

;;;; 页面平滑滚动,scroll-margin 3 靠近屏幕边沿 3 行时开始滚动,可以很好的看到上下文。
(setq scroll-margin 3
scroll-conservatively 10000)



;;;; < ==================== 语言类 ==================== >

;;;; 只渲染当前屏幕语法高亮,加快显示速度
;;;;(setq font-lock-maximum-decoration t)

;;;; delete trailing space,保存时删除行尾空白,python 对空格敏感
(add-hook 'before-save-hook 'delete-trailing-whitespace)

安装 jedi server

因为 jedi 依赖 Python 的 virtualenv,所以 ELPA 是无法完成这个任务。这里需要手动安装 jedi server。

  1. 打开 Emacs

  2. M-x,输入 jedi:install-server 回车

  3. minibuffer 里出现 done,就 OK 了。
     
     
     
     
    一个基础的 Win 平台下 Python + Emacs 编程环境就成形了。