Create a powerful Python code editing tool with Emacs

Source: Internet
Author: User
Tags autoload
Basic Configuration

Emacs itself provides the Python-mode, input m-x python-mode, you can enter the Python mode. Accordingly, a Python menu appears in the menu bar. Of course, in general, if the. py file is opened, it will also automatically enter the mode.
However, the default Python mode feature is a bit weak to use, and many places do not do well, it is best to download the third-party python mode. Python-mode is an open source project that can be downloaded in Https://launchpad.net/python-mode.
1. Installation
1). Install Prog-modes:

Aptitude Install Prolog-el

2). Download the Python-mode.el file above the project homepage.
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))

To detect if the extension is loading a path, 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.");;; 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). Execute: C-c c-c, this will execute the script in the new window and buffer;
2). C-J: Inserts a new line with the same indentation;
3). C-m-a: Jumps to function or class head;
4). C-M-E: Jumps to function or end of class;
5). C-c c-w: Run Pychecker for code detection;
The general use of this is the case, in addition, there are many classes or functions of the template can be done by shortcut keys, in the future will be used to strengthen the understanding. Thank you for seeing this!

Install extensions
In Emacs, a powerful Python IDE environment is built through various extensions, including snippet tools, smart tips, auto-completion, refactoring tools, Debugging and Gae debugging, and more. The following tools are installed on the premise that you have a certain understanding of Emacs configuration files, all the relevant El files must be placed in the Load_path can be loaded place.

1. Yasnippet
Snippet tool, can customize some templates, essential good things! Look at the following cool demo animation to 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 auto-complete tool, like the one In Vs, pops up a list box to let you choose.

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 ");;; Set the background color to look better than above (Define-key ac-completing-map "\m-n" ' ac-next);;; The list is moved down by pressing M-n (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 like Rename,move,extract method and so on. There are also very useful goto difinition (jump to Definition), show documents (show document) and so on. The rope and Pymacs must be installed before installing Ropemacs.

Installation method of rope:

Python setup.py Install

Installation method of Pymacs:

Python setup.py Install

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

Installation method of Ropmacs:

Python setup.py Install

. Emacs:

(pymacs-load "Ropemacs" "rope-") (setq ropemacs-enable-autoimport T)

4. Pycomplete
A more powerful smart tip tool, such as entering time.cl and then pressing tab, lists the function names for all CL starts in the time module. The function's parameter type is also prompted in mini buffer when the function is called. This thing needs to be installed Pymacs first.

Installation method:

1. Copy the Python-mode.el and Pycomplete.el into the load_path of Emacs.

2. Copy pycomplete.py to Pythonpath (e.g.: c:/python25/lib/site-packages)

3. Add in Emacs:

(Require ' pycomplete) (Setq auto-mode-alist (cons ' ("\\.py$". Python-mode) auto-mode-alist)) (AutoLoad ' Python-mode "Python-mode" "Python editing mode.") (Setq interpreter-mode-alist (cons ' ("Python". Python-mode)              interpreter-mode-alist))


5. PDB debugging
In Emacs, the code is debugged using the m-x PDB to bring up the PDB. However, it is found that in the Windows system, the debugging mode is not always accessible. The main reasons are:

(1). pdb.py location was not found in Windows. You need to make a path to the PDB yourself. You can set the path of the PDB in the following ways:

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 the PDB is called.

For the above two issues, my workaround is to not set the PDB specific path, m-x PDB return, the following command appears:

Then manually modify:

Run pdb (like this): Python-i-M PDB test.py

So it's done.

6. How to debug the Gae program
Gae is a Web application that needs to be debugged across threads, and the PDB itself is poorly supported for thread debugging. When you use the PDB for thread debugging, only insert the following code where you need to debug:

Import Pdbpdb.set_trace ()

Then run the debugged code directly, instead of executing it through the Python pdb, you can debug the multithreaded code.

But for Web applications like Google App engine, using this method or not debugging, and stdin and stdout, and finally find a good solution:

Def set_trace ():  import pdb, sys  debugger = pdb. Pdb (stdin=sys.__stdin__,    stdout=sys.__stdout__)  debugger.set_trace (Sys._getframe (). F_back)

In any place where debugging is required, call the Set_trace () function above.

  • Related Article

    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.