Difference between revisions of "Python"

From WikEmacs
Jump to: navigation, search
(Section "all in one" and a word about elpy)
(helm-cscope)
 
(14 intermediate revisions by 2 users not shown)
Line 4: Line 4:
 
fgallina's [https://github.com/fgallina/python.el python.el] is the python.el included in Emacs 24.2 and onwards.
 
fgallina's [https://github.com/fgallina/python.el python.el] is the python.el included in Emacs 24.2 and onwards.
  
= External extensions =
+
= All-in-one solutions =
 +
 
 +
== emacs-for-python ==
 +
 
 +
'''emacs-for-python''' is a bundle of the above modes (and more), and it's an easy way to turn Emacs into a Python IDE.
 +
 
 +
[https://github.com/gabrielelanaro/emacs-for-python/ emacs-for-python at github]
 +
 
 +
== Elpy ==
 +
 
 +
'''Elpy''' is a collection of elisp packages for Python too.
 +
 
 +
https://github.com/jorgenschaefer/elpy/wiki/Features
 +
 
 +
 
 +
= Refactoring =
  
 
== rope & ropemacs ==
 
== rope & ropemacs ==
  
'''rope''' is a library for refactoring and manipulating Python code. '''{{ModeLink|ropemacs}}''' is an Emacs interface to the rope library.
+
'''rope''' is a library for refactoring and manipulating Python code, '''pymacs''' is an interface between emacs lisp and python, and '''{{ModeLink|ropemacs}}''' is an Emacs interface to the rope library which uses rope and pymacs.
 +
 
 +
If you do some search and replace of code objects in your code and find it sometimes tricky, and/or you need to do it in more than one file, then you should consider a good tool of refactoring.
 +
 
 +
But Rope can do more than this simple example, it can:
 +
 
 +
* Rename anything
 +
* Extract method/local variable
 +
* Change method signature
 +
* Perform cross-project refactorings
 +
* Support Mercurial, GIT, Darcs and SVN in refactorings
 +
 
 +
Rope can also help IDE's with:
 +
 
 +
* Auto-completion
 +
* Finding definition location
 +
* Getting pydoc
 +
* Finding occurrences
 +
* Organizing imports (removing unused and duplicate imports and sorting them)
 +
* Generating python elements
 +
 
 +
You need to install rope with:
 +
 
 +
    aptitude install python-rope
 +
or
 +
    pip install rope
 +
 
 +
and then install ropemacs and pymacs (on [[ELPA]]).
  
 
[http://rope.sourceforge.net/ rope homepage]
 
[http://rope.sourceforge.net/ rope homepage]
  
[http://rope.sourceforge.net/ropemacs.html ropemacs homepage]
+
[http://rope.sourceforge.net/ropemacs.html ropemacs homepage] and see the [https://github.com/python-rope/ropemacs documentation on github].
 +
 
 +
= Auto-Completion =
 +
 
 +
 
 +
== Jedi ==
 +
 
 +
Jedi.el is a Python auto-completion package for Emacs. It aims at helping your Python coding in a non-destructive way. It also helps you to find information about Python objects, such as docstring, function arguments and code location.
 +
 
 +
Jedi is simple to install and it works out of the box.
 +
 
 +
See screenshots and get the full documentation : http://tkf.github.io/emacs-jedi/released/
 +
 
 +
Jedi's official page: http://jedi.jedidjah.ch/en/latest/
 +
 
 +
=== Installation ===
 +
 
 +
Install Jedi.el via el-get, Marmalade or MELPA (see install for more info) and add this to your Emacs configuration:
 +
    (add-hook 'python-mode-hook 'jedi:setup)
 +
    (setq jedi:setup-keys t)                      ; optional
 +
    (setq jedi:complete-on-dot t)                ; optional
 +
 
 +
or call M-x jedi:setup
 +
 
 +
''Note'': it's nice to use it in a python interpreter inside emacs :)
 +
 
 +
== Anaconda ==
 +
 
 +
'''Anaconda-mode''' is a mode for '''code navigation, documentation lookup''' and '''completion''' for Python.
 +
 
 +
It runs on '''emacs 24.3''' with python >= 2.6.
 +
 
 +
It provides:
 +
 
 +
* context-sensitive code completion for Python
 +
* jump to definition
 +
* find references
 +
* view documentation
 +
* virtualenv management
 +
 
 +
The package is available in [[MELPA]]. For more information, [https://github.com/proofit404/anaconda-mode read its documentation].
 +
 
 +
= Code Checker =
  
 
== Flymake ==
 
== Flymake ==
Line 21: Line 105:
 
To run pep8, pylint, pyflakes and unit-tests (with nose), you can be interested in using [https://github.com/akaihola/flymake-python flymake-python].
 
To run pep8, pylint, pyflakes and unit-tests (with nose), you can be interested in using [https://github.com/akaihola/flymake-python flymake-python].
  
== Pymacs ==
+
Don't forget about '''pylint''' too.
  
'''{{ModeLink|Pymacs}}''' is an Emacs extension that enables the use of Python alongside with Emacs Lisp.
+
= Indexing sources: ctags, cscope, pycscope =
  
[http://pymacs.progiciels-bpi.ca/index.html Pymacs homepage]
+
Indexing sources allows you to do neat things, like going to the definition of a function or finding which functions are calling another one.
 +
 
 +
== etags, ctags ==
 +
 
 +
'''Etags''' (Exuberant Ctags) generates an index (or tag) file of language objects found in source files that allows these items to be quickly and easily located by a text editor or other utility. A tag signifies a language object for which an index entry is available (or, alternatively, the index entry created for that object). Etags is a multilingual implementation of '''ctags'''.
 +
 
 +
The primary use for the tags files is looking up class/method/function/constant/etc declaration/definitions. Cscope is more powerful (see below).
 +
 
 +
=== usage ===
 +
 
 +
Generate the tags with this command at the root of your project:
 +
 
 +
    find . -name "*.py" -print | etags -
 +
 
 +
it creates the file TAGS.
 +
 
 +
Note that projects like [[Projectile]] or [[Helm]] provide an integrated use of etags (finding one, re-generating the index, etc).
 +
 
 +
== cscope ==
 +
 
 +
'''cscope''' is a much more powerful beast. While it operates on more or less the same principle (generating a file of useful metadata) it allows you do some fancier things like find all references to a symbol, see where a function is being invoked, etc.+ (you can find definitions as well).
 +
 
 +
It was originally designed for C and C++, but thanks to version 0.3 of '''pycscope''', pythonistas can make use of it.
 +
 
 +
=== usage ===
 +
 
 +
The following commands should get you running:
 +
 
 +
    apt-get install cscope
 +
    pip install pycscope
 +
    # in project base dir:
 +
    find . -name '*.py' > cscope.files
 +
    cscope -R
 +
 
 +
now install the '''xcscope''' emacs package with [[ELPA]] and require it:
 +
    (require 'xcscope)
 +
 
 +
When done, you can either enable the mode with '''cscope-minor-mode''' (which will add a menu you are free to explore) or call some functions like {{Command|cscope-find-global-definition}}, {{Command|cscope-find-functions-calling-this-function}}, etc.
 +
 
 +
=== Interactivity with helm-cscope ===
  
 +
You can do all that interactively with [https://github.com/sergey-pashaev/helm-cscope helm-cscope] (that may come with bugs at the moment).
  
 +
= Debugging =
  
 
== Ipdb, ipython debugger ==
 
== Ipdb, ipython debugger ==
Line 51: Line 176:
 
A tool to use in a non-emacs terminal would be [https://pypi.python.org/pypi/pdbpp/ pdbpp]
 
A tool to use in a non-emacs terminal would be [https://pypi.python.org/pypi/pdbpp/ pdbpp]
  
= All-in-one solutions =
+
= Other tools =
  
== emacs-for-python ==
+
== Pymacs ==
  
'''emacs-for-python''' is a bundle of the above modes (and more), and it's an easy way to turn Emacs into a Python IDE.
+
'''{{ModeLink|Pymacs}}''' is an Emacs extension that enables the use of Python alongside with Emacs Lisp.
  
[https://github.com/gabrielelanaro/emacs-for-python/ emacs-for-python at github]
+
[http://pymacs.progiciels-bpi.ca/index.html Pymacs homepage]
  
== Elpy ==
+
== Auto include import statements ==
  
'''Elpy''' is a collection of elisp packages for Python too.
+
[[Ropemacs]] (see above) is a plugin for performing python refactorings in emacs. It uses rope library and pymacs. It has '''rope-auto-import''', so if you write
  
https://github.com/jorgenschaefer/elpy/wiki/Features
+
    rmtree
  
 +
and then execute {{Command|rope-auto-import}},
 +
 +
    from shutil import rmtree
 +
 +
is inserted at the top of the file.
 +
 +
Unfortunately Ropemacs can not do non-relative imports, it can only create imports of the "from X import Y" variety (if you type ''shutil.rmtree'' it doesn't write ''import shutil'').
 +
 +
== Sort import statements ==
 +
 +
See the '''py-isort''' [[Melpa]] package to automatically sort import statements.
  
 
= See also =
 
= See also =
  
You'll be certainly interested in :
+
You'll be certainly interested in the following packages (that you will find on [[ELPA]] or MELPA):
 +
 
 +
* '''Magit''', a git interface. Emacs + git is magic : see [[magit]]
 +
 
 +
* '''Yasnippet''', a template system: [[yasnippet]]
  
=== Magit, a git interface ===
+
* '''virtualenvwrapper''', to load a virtualenv (so than you can use it with M-x compile) (available through ELPA)
  
Emacs + git is magic : [[magit]]
+
* '''helm-pydoc''' to browse the documentation of installed packages and import one
  
=== Yasnippet, a template system ===
+
* '''py-isort''' to sort import statements
  
[[yasnippet]]
+
* '''pungi''' to integrate jedi, virtualenv and buildout.
  
=== ELPA packages ===
+
* other '''ELPA packages'''
  
If you're running Emacs 24, check out some [[ELPA]] packages:
+
If you're running Emacs 24, check out what is available in [[ELPA]]:
; {{Command | list-packages}}  :  and see flymake-pyhon-pyflakes, flymake-shell, virtualenv, abl-mode (a python TDD minor-mode), jedi (a python auto-completion for emacs), nose (easy python test-running in emacs), pyregexp, python-magic and more.
+
; {{Command | list-packages}}  :  and see flymake-pyhon-pyflakes, flymake-shell, abl-mode (a python TDD minor-mode), nose (easy python test-running in emacs), pyregexp, python-magic and more.
  
 
[[Category:Programming languages]]
 
[[Category:Programming languages]]
 +
[[Category:Programming]]

Latest revision as of 05:31, 26 September 2014

Default modes[edit]

There are a number of python modes for Emacs. fgallina's python.el is the python.el included in Emacs 24.2 and onwards.

All-in-one solutions[edit]

emacs-for-python[edit]

emacs-for-python is a bundle of the above modes (and more), and it's an easy way to turn Emacs into a Python IDE.

emacs-for-python at github

Elpy[edit]

Elpy is a collection of elisp packages for Python too.

https://github.com/jorgenschaefer/elpy/wiki/Features


Refactoring[edit]

rope & ropemacs[edit]

rope is a library for refactoring and manipulating Python code, pymacs is an interface between emacs lisp and python, and ropemacs is an Emacs interface to the rope library which uses rope and pymacs.

If you do some search and replace of code objects in your code and find it sometimes tricky, and/or you need to do it in more than one file, then you should consider a good tool of refactoring.

But Rope can do more than this simple example, it can:

  • Rename anything
  • Extract method/local variable
  • Change method signature
  • Perform cross-project refactorings
  • Support Mercurial, GIT, Darcs and SVN in refactorings

Rope can also help IDE's with:

  • Auto-completion
  • Finding definition location
  • Getting pydoc
  • Finding occurrences
  • Organizing imports (removing unused and duplicate imports and sorting them)
  • Generating python elements

You need to install rope with:

   aptitude install python-rope

or

   pip install rope

and then install ropemacs and pymacs (on ELPA).

rope homepage

ropemacs homepage and see the documentation on github.

Auto-Completion[edit]

Jedi[edit]

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

Jedi is simple to install and it works out of the box.

See screenshots and get the full documentation : http://tkf.github.io/emacs-jedi/released/

Jedi's official page: http://jedi.jedidjah.ch/en/latest/

Installation[edit]

Install Jedi.el via el-get, Marmalade or MELPA (see install for more info) and add this to your Emacs configuration:

   (add-hook 'python-mode-hook 'jedi:setup)
   (setq jedi:setup-keys t)                      ; optional
   (setq jedi:complete-on-dot t)                 ; optional

or call M-x jedi:setup

Note: it's nice to use it in a python interpreter inside emacs :)

Anaconda[edit]

Anaconda-mode is a mode for code navigation, documentation lookup and completion for Python.

It runs on emacs 24.3 with python >= 2.6.

It provides:

  • context-sensitive code completion for Python
  • jump to definition
  • find references
  • view documentation
  • virtualenv management

The package is available in MELPA. For more information, read its documentation.

Code Checker[edit]

Flymake[edit]

flymake is a on-the-fly syntax checker for Emacs. We can use it alongside with flyspell.

To run pep8, pylint, pyflakes and unit-tests (with nose), you can be interested in using flymake-python.

Don't forget about pylint too.

Indexing sources: ctags, cscope, pycscope[edit]

Indexing sources allows you to do neat things, like going to the definition of a function or finding which functions are calling another one.

etags, ctags[edit]

Etags (Exuberant Ctags) generates an index (or tag) file of language objects found in source files that allows these items to be quickly and easily located by a text editor or other utility. A tag signifies a language object for which an index entry is available (or, alternatively, the index entry created for that object). Etags is a multilingual implementation of ctags.

The primary use for the tags files is looking up class/method/function/constant/etc declaration/definitions. Cscope is more powerful (see below).

usage[edit]

Generate the tags with this command at the root of your project:

    find . -name "*.py" -print | etags -

it creates the file TAGS.

Note that projects like Projectile or Helm provide an integrated use of etags (finding one, re-generating the index, etc).

cscope[edit]

cscope is a much more powerful beast. While it operates on more or less the same principle (generating a file of useful metadata) it allows you do some fancier things like find all references to a symbol, see where a function is being invoked, etc.+ (you can find definitions as well).

It was originally designed for C and C++, but thanks to version 0.3 of pycscope, pythonistas can make use of it.

usage[edit]

The following commands should get you running:

   apt-get install cscope
   pip install pycscope
   # in project base dir:
   find . -name '*.py' > cscope.files
   cscope -R 

now install the xcscope emacs package with ELPA and require it:

   (require 'xcscope)

When done, you can either enable the mode with cscope-minor-mode (which will add a menu you are free to explore) or call some functions like M-x cscope-find-global-definition, M-x cscope-find-functions-calling-this-function, etc.

Interactivity with helm-cscope[edit]

You can do all that interactively with helm-cscope (that may come with bugs at the moment).

Debugging[edit]

Ipdb, ipython debugger[edit]

If you call ipdb, the execution will stop and give a nice ipython-looking prompt. Just add `import ipdb; ipdb.set_trace()`

You can use the following to highlight this line, and not forget it :

; Highlight the call to ipdb
; src http://pedrokroger.com/2010/07/configuring-emacs-as-a-python-ide-2/
(defun annotate-pdb ()
  (interactive)
  (highlight-lines-matching-regexp "import ipdb")
  (highlight-lines-matching-regexp "ipdb.set_trace()"))
(add-hook 'python-mode-hook 'annotate-pdb)

pdb track[edit]

If you use emacs-for-python given above, you can track your source code while debugging with pdbtrack.

A tool to use in a non-emacs terminal would be pdbpp

Other tools[edit]

Pymacs[edit]

Pymacs is an Emacs extension that enables the use of Python alongside with Emacs Lisp.

Pymacs homepage

Auto include import statements[edit]

Ropemacs (see above) is a plugin for performing python refactorings in emacs. It uses rope library and pymacs. It has rope-auto-import, so if you write

   rmtree

and then execute M-x rope-auto-import,

   from shutil import rmtree

is inserted at the top of the file.

Unfortunately Ropemacs can not do non-relative imports, it can only create imports of the "from X import Y" variety (if you type shutil.rmtree it doesn't write import shutil).

Sort import statements[edit]

See the py-isort Melpa package to automatically sort import statements.

See also[edit]

You'll be certainly interested in the following packages (that you will find on ELPA or MELPA):

  • Magit, a git interface. Emacs + git is magic : see magit
  • virtualenvwrapper, to load a virtualenv (so than you can use it with M-x compile) (available through ELPA)
  • helm-pydoc to browse the documentation of installed packages and import one
  • py-isort to sort import statements
  • pungi to integrate jedi, virtualenv and buildout.
  • other ELPA packages

If you're running Emacs 24, check out what is available in ELPA:

M-x list-packages  
and see flymake-pyhon-pyflakes, flymake-shell, abl-mode (a python TDD minor-mode), nose (easy python test-running in emacs), pyregexp, python-magic and more.