将Emacs打造成强大的Python代码编辑工具
基本配置
Emacs本身提供了python-mode,输入M-x python-mode,就可以进入python模式。相应地,会在菜单栏出现Python菜单。当然,一般来讲,如果是.py文件打开的话,也会自动进入该模式。
不过,默认的python模式功能上面用起来还是有点弱,而且许多地方做的并不好,最好下载第三方的python模式。python-mode是一个开源项目,可以在https://launchpad.net/python-mode进行下载。
1.安装
1).安装prog-modes:
aptitude install prolog-el
2).下载python-mode.el文件在项目主页上面。
3).编译:
C-x C-f /path/to/python-mode.el RET M-x byte-compile-file RET
4).在.emacs中加入python-mode.el路径:
(setq load-path (cons "/dir/of/python-mode/" load-path))
检测扩展是否加载路径,测试方法:M-x locate-library RET python-mode RET
2.配置.emacs文件
(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.操作
1).执行:C-c C-c,这样会在新的窗口及缓冲区执行脚本;
2).C-j:以相同的缩进插入新的一行;
3).C-M-a:跳至函数或类首;
4).C-M-e:跳至函数或类尾;
5).C-c C-w:运行PyChecker进行代码检测;
大体的使用方式就是这样的了,另外,还有许多类或函数的模板可以通过快捷键进行,在今后常用的时候会加强了解的。感谢你能看到这里!
安装扩展
在Emacs中,通过各种扩展,打造强大的Python IDE环境,包括Snippet工具,智能提示,自动补全,重构工具,调试以及GAE的调试,等等。以下各工具的安装前提是你对Emacs的配置文件有一定的了解,所有相关的el文件都必须放在load_path能够加载的地方。
1. YASnippet
snippet工具,可自定义一些模板,必不可少的好东西!看了下面这个很酷的演示动画就明白了:
http://yasnippet.googlecode.com/files/yasnippet.avi
安装方法:
(require 'yasnippet) (yas/initialize) (yas/load-directory "~/.emacs.d/plugins/yasnippet-0.6.1c/snippets")
2. AutoComplete
自动完成工具,会像VS里一样,弹出一个列表框让你去选择。
安装方法:
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) ;;; 列表中通过按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
非常棒的重构工具,比如rename,move,extract method等等。还有非常好用的goto difinition(跳到定义),show documents(显示文档)等等。安装Ropemacs前,必须先安装rope和pymacs 。
rope的安装方法:
python setup.py install
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)
Ropmacs的安装方法:
python setup.py install
.emacs中:
(pymacs-load "ropemacs" "rope-") (setq ropemacs-enable-autoimport t)
4. pycomplete
一个更加强大的智能提示工具,比如,输入time.cl 然后按TAB键,会列出time模块所有cl开头的函数名。在调用函数时,还会在mini buffer中提示函数的参数类型。这个东西需要先安装pymacs。
安装方法:
1. 拷贝 python-mode.el and pycomplete.el 到Emacs的load_path中。
2. 拷贝 pycomplete.py 到PYTHONPATH (比如: c:/python25/Lib/site-packages)
3. .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调试
在Emacs中,通过M-x pdb可调出pdb对python代码进行调试。但是发现在Windows系统中,总进入不了调试模式。主要原因有:
(1). windows中,找不到pdb.py位置。需自己制定pdb的路径。可以通过下面的方法设置pdb的路径:
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). windows中,调用pdb时,未使用python -i 参数。
针对上面两个问题,我的解决办法是,不设置pdb具体路径,M-x pdb 回车后,出现下面命令:
Run pdb (like this): pdb
然后手动修改一下:
Run pdb (like this): python -i -m pdb test.py
这样就搞定了。
6. 如何调试GAE程序
GAE是一个Web应用,需要跨线程进行调试,而pdb本身对线程调试支持不好。使用pdb进行线程调试时,只有在需要调试的地方插入下面代码:
import pdb pdb.set_trace()
然后直接运行被调试代码,而不是通过python pdb来执行,就可以多线程代码进行调试了。
但是Google App Engine这样的Web应用,使用这个方法还是不能调试,和stdin和stdout有关,最后找到一个很好的解决方法:
def set_trace(): import pdb, sys debugger = pdb.Pdb(stdin=sys.__stdin__, stdout=sys.__stdout__) debugger.set_trace(sys._getframe().f_back)
在任何需要调试的地方,调用上面的set_trace()函数。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

Docker uses Linux kernel features to provide an efficient and isolated application running environment. Its working principle is as follows: 1. The mirror is used as a read-only template, which contains everything you need to run the application; 2. The Union File System (UnionFS) stacks multiple file systems, only storing the differences, saving space and speeding up; 3. The daemon manages the mirrors and containers, and the client uses them for interaction; 4. Namespaces and cgroups implement container isolation and resource limitations; 5. Multiple network modes support container interconnection. Only by understanding these core concepts can you better utilize Docker.

In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.

VS Code is the full name Visual Studio Code, which is a free and open source cross-platform code editor and development environment developed by Microsoft. It supports a wide range of programming languages and provides syntax highlighting, code automatic completion, code snippets and smart prompts to improve development efficiency. Through a rich extension ecosystem, users can add extensions to specific needs and languages, such as debuggers, code formatting tools, and Git integrations. VS Code also includes an intuitive debugger that helps quickly find and resolve bugs in your code.

Python excels in automation, scripting, and task management. 1) Automation: File backup is realized through standard libraries such as os and shutil. 2) Script writing: Use the psutil library to monitor system resources. 3) Task management: Use the schedule library to schedule tasks. Python's ease of use and rich library support makes it the preferred tool in these areas.

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.
