Create Emacs as a powerful Python code editing tool

Source: Internet
Author: User
Tags autoload
This article mainly introduces how to make Emacs a powerful Python code editing tool. It is often said that Vim is the god of the editor, and Emacs is the editor of the god. For more information, see Basic Configuration

Emacs provides python-mode. Input M-x python-mode to enter the python mode. Correspondingly, the Python menu appears on the menu bar. Of course, in general, if the. py file is opened, it will automatically enter this mode.
However, the default python Mode features are still a little weak in use, and it is not good in many places. It is best to download a third-party python mode. Python-mode is an open-source project that can be downloaded at https://launchpad.net/python-mode.
1. Install
1) install prog-modes:

aptitude install prolog-el

2). Download The python-mode.el file on the project home page.
3). Compile:

C-x C-f /path/to/python-mode.el RET        M-x byte-compile-file RET

4). Add the python-mode.el path to. emacs:

    (setq load-path (cons "/dir/of/python-mode/" load-path))

Check whether the extension is loaded. Test method: M-x locate-library RET python-mode RET
2. Configure the. emacs file

(setq auto-mode-alist (cons '("//.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. Operation
1) Run: C-c. The script will be executed in the new window and buffer zone;
2). C-j: Insert a new row with the same indentation;
3). C-M-a: Jump to the first function or class;
4). C-M-e: jump to the end of a function or class;
5). C-c C-w: Run PyChecker to check the code;
The general usage is like this. In addition, there are many templates for classes or functions that can be performed by using shortcut keys, which will be enhanced in the future. Thank you for seeing this!

Install Extension
Emacs creates a powerful Python IDE environment through various extensions, including Snippet tools, smart prompts, Automatic completion, refactoring tools, debugging, and GAE debugging. The prerequisites for installing the following tools are that you have a certain understanding of the Emacs configuration file. All the relevant el files must be placed where load_path can be loaded.

1. YASnippet
Snippet tool. You can customize some templates, which are essential! After reading the following cool demo animation, you will understand:
Http://yasnippet.googlecode.com/files/yasnippet.avi

Installation Method:

(require 'yasnippet)(yas/initialize)(yas/load-directory "~/.emacs.d/plugins/yasnippet-0.6.1c/snippets")

2. AutoComplete
The Automatic completion tool will pop up a list box for you to select, just like in.

Installation Method:

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 ");;; define-key ac-completing-map "\ M-n" 'ac-next );;; moving down by M-n in the list (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
Great refactoring tools, such as rename, move, and extract method. There are also good goto difinition (jump to definition), show documents (show document) and so on. You must install rope and pymacs before installing Ropemacs.

Rope installation method:

python setup.py install

Pymacs installation method:

python setup.py install

In. 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 installation method:

python setup.py install

In. emacs:

(pymacs-load "ropemacs" "rope-")(setq ropemacs-enable-autoimport t)

4. pycomplete
A more powerful smart reminder tool, such as inputting time. cl and then pressing the TAB key, lists the function names starting with all cl in the time module. When calling a function, the parameter type of the function is also prompted in mini buffer. Install pymacs first.

Installation Method:

1. Copy the python-mode.el and pycomplete. el to the load_path of Emacs.

2. Copy pycomplete. py to PYTHONPATH (for example, c:/python25/Lib/site-packages)

3. Add the following to emacs:

(require 'pycomplete)(setq auto-mode-alist (cons '("\\.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 debugging
In Emacs, you can call up pdb through M-x pdb to debug python code. However, it is found that in Windows, the debugging mode is always unavailable. The main reasons are:

(1) In windows, the location of pdb. py cannot be found. You need to customize the pdb path. You can set the path of the pdb using the following method:

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) In windows, the python-I parameter is not used when pdb is called.

For the above two problems, my solution is not to set the specific pdb path. After M-x pdb returns, the following command is displayed:

Run pdb (like this): pdb 

Then manually modify:

Run pdb (like this): python -i -m pdb test.py

This is done.

6. How to debug the GAE Program
GAE is a Web application that requires cross-thread debugging, while pdb itself does not support thread debugging well. When using pdb for thread debugging, only Insert the following code where debugging is needed:

import pdbpdb.set_trace()

Then run the code to be debugged directly, instead of executing the Code through python pdb, You can debug the multi-threaded code.

However, for Web applications such as Google App Engine, this method still cannot be debugged. It is related to stdin and stdout. Finally, a good solution is found:

def set_trace():  import pdb, sys  debugger = pdb.Pdb(stdin=sys.__stdin__,    stdout=sys.__stdout__)  debugger.set_trace(sys._getframe().f_back)

Call the above set_trace () function wherever debugging is required.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.