Innehållsförteckning RecentChanges News ElispArea HowTo Problems Suggestions

PythonProgrammingInEmacs

Last edit

Sammanfattning: Major refactoring and cleanup. I killed some stuff that seemed very outdated, hope nothing important got lost.

Deleted:

< emacs.el
< == Overview ==

Ändrad:

< There are three major implementations of a Python mode for Emacs: one called python-mode.el and two called python.el. In order of beginning development, these are:
< # ProgrammingWithPythonModeDotEl --
[https://launchpad.net/python-mode python-mode.el], once distributed with Python, now maintained separately.
< # ProgrammingWithPythonDotEl -- python.el, as came with GNU Emacs 24.1 and before[http://www.loveshack.ukfsn.org/emacs/python.el],[https://github.com/leoliu/python-el]. "the main raison d'être for python.el was that we couldn't get
< copyright papers for python-mode
.el for inclusion in Emacs" [http://mail.python.org/pipermail/python-mode/2009-January/000514.html].
< # ProgrammingWithPythonDotElGallina --
[https://github.com/fgallina/python.el Fabián Ezequiel Gallina's python.el], included as the Emacs trunk [http://bzr.savannah.gnu.org/lh/emacs/trunk/annotate/head:/lisp/progmodes/python.el python.el] as of revno [http://bzr.savannah.gnu.org/lh/emacs/trunk/revision/108614 108614]. Gallina discusses his motivation [http://lists.gnu.org/archive/html/emacs-devel/2012-04/msg00583.html here].
< You may also find these pages useful:

< * [http://gabrielelanaro.github.com/emacs-for-python/ Gabriele Lanaro's python setup]. Includes and sets up several common python packages, including Gallina's python.el. Highly recommended for quickly setting up a Python programming environment, e.g. "If you google python+emacs, you’ll find a lot of packages and recipes how to glue it all together, but if you are Emacs Lisp newbie like me, having all that stuff in one easy-to-install, tested package
< would be priceless." [http://maddemcode
.com/python/emacs-and-python-the-definitive-answer/]

till

> == Python Modes ==
> There are a number of Python modes for Emacs. The major ones right now are [ProgrammingWithPythonDotElGallina python.el] which comes with Emacs 24.2 and up and [ProgrammingWithPythonModeDotEl python-mode.el] which used to be included in Python (up to 2.6.8). The older [ProgrammingWithPythonDotEl python.el] from Emacs 24.1 and before has a number of fans as well.
> === Emacs as a Python IDE ===
> Beyond the basic Python modes described above, there are a number of modes that support Python programming.
> *
[http://gabrielelanaro.github.com/emacs-for-python/ Emacs for Python], a collection of emacs extensions specifically collected for python development, with workflow guidelines.
> * [https://github.com/jorgenschaefer/elpy Elpy], an Emacs Python development environment available via package.el as a simple installation. Can use Rope or Jedi to provide completions.
> * [https://bitbucket.org/agr/ropemacs Ropemacs], a library using [[Pymacs]] to talk with
the Rope refactoring library. You need to install [http://rope.sourceforge.net/ Rope], [http://pypi.python.org/pypi/ropemode Ropemode], [http://pymacs.progiciels-bpi.ca/ Pymacs] and [http://rope.sourceforge.net/ropemacs.html Ropemacs] to make this work.
> === Other Support Modes ===
> *
[http://taesoo.org/Opensource/Pylookup taesoo.org/Opensource/Pylookup Pylookup mode], a mode to search the Python online/offline documentation ([http://github.com/tsgates/pylookup github.com/tsgates/pylookup github])
> * [https://github.com
/tavisrudd/emacs.d/blob/master/dss-completion.el Auto-Complete support in IPython shell buffers]
> * [https://github.com/aculich/virtualenv.el virtualenv.el], a mode for working with virtualenvs
> *
[https://bitbucket.org/durin42/nosemacs nosemacs], a mode to run the nosetests testing framework
> * [http://tkf.github.com/emacs-ipython-notebook/ Emacs IPython Notebook], an IPython notebook client for Emacs.
> == Various Python Interpreters ==
> === IPython ===
> Emacs
' 24.2+ python.el contains the following guide on how to set it up to use IPython 0.11:
> {{{
> (setq

> python-shell-interpreter "ipython"
> python-shell-interpreter-args ""
> python-shell-prompt-regexp "In \\[[0-9]+\\]: "
> python-shell-prompt-output-regexp "Out\\[[0-9]+\\]: "
> python-shell-completion-setup-code
> "from IPython.core.completerlib import module_completion"
> python-shell-completion-module-string-code
> "';'.join(module_completion('''%
s'''))\n"
> python-shell-completion-string-code
> "';'.join(get_ipython().Completer
.all_completions('''%s'''))\n")
> }}}
> For iPython 0
.10 everything would be the same except for `python-shell-completion-string-code` and `python-shell-completion-module-string-code`:
> {{{
> (setq python-shell-completion-string-code
> "';'.join(__IP
.complete('''%s'''))\n"
> python-shell-completion-module-string-code "")
> }}}
> Unfortunately running iPython on Windows needs some more tweaking
. The way you must set `python-shell-interpreter` and `python-shell-interpreter-args` is as follows:
> {{{
> (setq
> python-shell-interpreter "C:\\Python27\\
python.exe"
> python-shell
-interpreter-args
> "-i C:\\Python27\\Scripts\\ipython-script.py")
> }}}
> That will spawn the iPython process correctly (Of course you need to modify
the paths according to your system).
> === IronPython ===
> * Install [ProgrammingWithPythonModeDotEl python-mode]
> * ##(setq py-jython-command "c:/Program Files/IronPython 2.6 for .NET 4.0/ipy.exe")##
> * Open a .py file. C
-c C-t will toggle "jython", C-c ! will start a shell
> == Further Reading ==

Ändrad:

< * [http://www.enigmacurry.com/2009/01/21/autocompleteel-python-code-completion-in-emacs/ AutoComplete.el : Python Code Completion in Emacs]
< * see [http://www.enigmacurry.com/2009/01/19/my-emacs-config-on-github/ My Emacs config on Github] for dotfiles

till

> * [http://www.enigmacurry.com/2009/01/21/autocompleteel-python-code-completion-in-emacs/ AutoComplete.el: Python Code Completion in Emacs]
> ** See [http://www.enigmacurry.com/2009/01/19/my-emacs-config-on-github/ My Emacs config on Github] for dotfiles

Ändrad:

< == Emacs as a Python IDE ==
< # Install rope ([http://rope.sourceforge.net/ homepage], [http://rope.sourceforge.net/ download here])
< # Install ropemode ([http://pypi.python.org/pypi/ropemode homepage on PyPI])
< # Install Pymacs ([http://pymacs.progiciels-bpi.ca/ homepage])
< # Install ropemacs ([http://rope.sourceforge.net/ropemacs.html homepage], [http://bitbucket.org/agr/ropemacs/get/tip.gz direct link to snapshot])
< Rope is a refactoring library, and ropemode is a wrapper. Pymacs allows Emacs to spawn a Python process and interact with it. Finally, ropemacs gives a convenient in-emacs interface to all this, including Python documentation access, inlining, finding function definitions and so on.
< : I installed emacs-vcs-24.0.9999 on gentoo, and ropemacs stopped working;
< 'rope-completions' (or 'foo-completions', depending on how you import it) froze emacs on the first or second invocation. This had worked OK on emacs-23.2-r1, and did again once I reverted. This was possibly a problem with Pymacs, which might not handle problems well, allowing emacs to just hang. It's also bad that some of the functions added to save and exit hooks are also pymacs derived! --CH

< == Comment/Uncomment Region ==

till

> == Various Configuration Tips ==
> === Comment/Uncomment Region ===

Ändrad:

< == Search the python documentation in Emacs ==
< Search the python online/offline documentations with convenience in Emacs
< Github : [http://github.com/tsgates/pylookup github.com/tsgates/pylookup]
< Pylookup Mode : [http://taesoo.org/Opensource/Pylookup taesoo.org/Opensource/Pylookup ]
< ==
Browse the Python Documentation using Info ==

till

> === Browse the Python Documentation using Info ===

Ändrad:

< == Using Flymake with Python ===
< =
Using flymake with multiple lint, style and syntax checkers =

till

> === Using Flymake with Python ====
> ====
Using flymake with multiple lint, style and syntax checkers ====

Ändrad:

< = Using flymake with pylint =

till

> ==== Using flymake with pylint ====

Ändrad:

< = Using flymake with pyflakes =

till

> ==== Using flymake with pyflakes ====

Ändrad:

< = Using flymake with pyflakes on Windows =

till

> ==== Using flymake with pyflakes on Windows ====

Ändrad:

< * PyIndent
< == Issues ==
< This is a known bug (emacs 21.3): python-mode interferes with gud in such a way as to make the overlay arrow not work when debugging (even C source).
< Disable auto-loading python-mode to get around this bug.
< : This is one of the things fixed by the patch to python-mode.el,
see ProgrammingWithPythonDotEl
< == iPython Emacs 23 ==
< RichardRiley
< I found this which got me up and running with iPython integrated nicely into emacs 23.
< [http://lists.ipython.scipy.org/pipermail/ipython-user/2008-September/005791.html
< lists.ipython.scipy.org/pipermail/ipython-user/2008-September/005791.html
< ]
< The important file, ipython.el, can be found here : [http://ipython.scipy.org/dist/ipython.el
< ipython.scipy.org/dist/ipython.el
< ]
< If you load it, ipython becomes the default python shell assuming the ipython executable is available.
< my python integration is now like this:
< <pre>
< (setq load-path
< (append (list nil
< "~/.emacs.d/python-mode-1.0/"
< "~/.emacs.d/pymacs/"
< "~/.emacs.d/ropemacs-0.6"
< )
< load-path))
< (require 'ipython)
< (setq py-python-command-args '( "--colors" "Linux"))
< (require 'python-mode)
< (require 'pymacs)
< (pymacs-load "ropemacs" "rope-")
< ;; (defadvice py-execute-buffer (around python-keep-focus activate)
< ;; "Thie advice to make focus python source code after execute command `py-execute-buffer'."
< ;; (let ((remember-window (selected-window))
< ;; (remember-point (point)))
< ;; ad-do-it
< ;; (select-window remember-window)
< ;; (goto-char remember-point)))
< ;; (defun rgr/python-execute()
< ;; (interactive)
< ;; (if mark-active
< ;; (py-execute-string (buffer-substring-no-properties (region-beginning) (region-end)))
< ;; (py-execute-buffer)))
< ;; (global-set-key (kbd "C-c C-e") 'rgr/python-execute)
< (add-hook 'python-mode-hook
< '(lambda () (eldoc-mode 1)) t)
< (provide 'python-programming)
< </pre>
< More iPython info here: [http://ipython.scipy.org/moin/FrontPage
< ipython.scipy.org/moin/FrontPage
< ]
< My own python files are here : [http://richardriley.net/projects/emacs/dotprogramming#sec-1.4
< richardriley.net/projects/emacs/dotprogramming#sec-1.4
< ]
< Having got iPython working in emacs, I found the following "howto tutorial" videos to be superb for the beginner (me!) :
< [http://showmedo.com/videos/video?name=1000010&fromSeriesID=100
< showmedo.com/videos/video?name=1000010&fromSeriesID=100
< ]
< == Ipython completion ==
< Ipython provide good completion support without extra dependencies.
< However, to make it working in emacs, you will have to add this line to your .emacs, the full patch for ipython.el is at [https://github.com/ipython/ipython/issues/issue/204
< github.com/ipython/ipython/issues/issue/204
< ]
< <pre>
< (setq ipython-completion-command-string "print(';'.join(get_ipython().Completer.complete('%s')[1])) #PYTHON-MODE SILENT\n")
< </pre>
< The definition in ipython.el is wrong (or deprecated may be).
< Note that Lisp:anything-ipython.el already fix this.
< To have ido completion, simply comment and insert the following lines in ipython.el as shown below
< <pre>
< ;; (with-output-to-temp-buffer "*
IPython Completions*"
< ;; (display-completion-list (all-completions pattern completion-table)))
< (let* ((dotposition (- (length pattern ) (length (first (last (split-string pattern "[\.]")) ) ) ))
< (ido-ipython-selection (ido-completing-read (concat "Choose a completion: " pattern)
< (mapcar (lambda (x) (substring x dotposition))
< (all-completions pattern completion-table)))))
< (delete-char (- dotposition (length pattern)))
< (insert ido-ipython-selection))
< </pre>
< To have an even better completion, you should install also rlcompleter2.
< [http://codespeak.net/rlcompleter2/
< codespeak.net/rlcompleter2/
< ]
< Lisp:anything-ipython.el provide an anything interface for ipython completion in your python buffer.
< You will find all instructions to install Lisp:anything-ipython.el in the headers of file.
< You will need of course a complete Anything install, look how to install Anything at
< [http://www.emacswiki.org/cgi-bin/emacs/Anything
< www.emacswiki.org/cgi-bin/emacs/Anything
< ]
< Screenshot of anything-ipython in action:
< [http://farm4.static.flickr.com/3459/3760695525_887356961c.jpg
< farm4.static.flickr.com/3459/3760695525_887356961c.jpg]
< == auto-complete.el support in ipython shell buffers ==
< [https://github.com/tavisrudd/emacs.d/blob/master/dss-completion.el
< github.com/tavisrudd/emacs.d/blob/master/dss-completion.el
< ]
< == Ipython
debugging/pdbtrack ==

till

> Also see PyIndent
> ===
IPython debugging/pdbtrack ===

Ändrad:

< == IronPython ==
< * Install [ProgrammingWithPythonModeDotEl python-mode]
< * ##(setq py-jython-command "c:/Program Files/IronPython 2.6 for .NET 4.0/ipy.exe")##
< * Open a .py file. C-c C-t will toggle "jython", C-c ! will start a shell
< == Pedro Kroger's python setup ==
< === dependencies ===
< The following is based on [http://pedrokroger.net/2010/07/configuring-emacs-as-a-python-ide-2/ Pedro Kroger's python setup], which this user found to provide much functionality. Note Kroger's instructions are especially well suited for users of Ubuntu or other Debian-based systems. While the instructions at that page are quite good, some gotcha's were found, Since I cannot edit that page, the following tips are offered here from my experience setting this up in Feb 2011 using the following dependencies:
< * From the Ubuntu/Debian package management system. All were up-to-date as of 1 Feb 2011.
< ** Ubuntu Lucid 10.04.1 (kernel=2.6.32-28)
< ** GNU Emacs 23.1.50.1 (package=emacs-snapshot-gtk)
< ** Python=2.6.5
< ** IPython=0.10
< ** other python-related Ubuntu/Debian packages: python2.6-doc, pep8, pylint
< * Python packages installed with `easy_install' (as directed by [http://pedrokroger.net/2010/07/configuring-emacs-as-a-python-ide-2/ Kroger's page])
< ** ipdb
< ** reimport
< * From other sources, (as directed by [http://pedrokroger.net/2010/07/configuring-emacs-as-a-python-ide-2/ Kroger's page])
< ** python-mode.el=5.2.0
< ** ipython.el=2927
< ** anything.el=1.287
< ** anything-ipython.el=25 July 2009 18:48:31 (+0200)
< ** anything-show-completion.el=1.19
< ** pylookup=current (no versioning visible)
< ** autopair.el=0.4
< ** python-pep8.el=1.01
< ** python-pylint.el=1.01
< ** yasnippet=0.6.1c
< * Note I did not install
< ** lambda-mode
< ** flymake
< === my procedure
===
< (Apologies for manual numbering, but I don't know how to make the wiki resume numbering after, e.g., including a preformatted section.)
< 0. I previously had ubuntu, emacs, python, and ipython installed, with python configured for ##easy_install##.
< 1. I first followed all procedures in Kroger's section=/Tools installation and configuration/ except for ##lambda-mode##. (I also did not install ##flymake##; Kroger's page does not either, though it is mentioned.)
< * Note I also byte-compiled all the *.el after downloading.
< 2. For my first test, I made the following file=<code>/tmp/Test.py</code> in a buffer, noting syntax coloration.
< <pre>class Test():
< def foo1(self):
< print "foo1"
< def foo2(self):
< print "foo2"
< </pre>
< 3. from buffer=<code>Test.py</code>, I did
< 3.1 ##C-c !## to start an ipython buffer.
< == Emacs Mode for files not ending with '.py' ==
< You can set the mode for python files with file names not ending with '.py' by adding line
< <pre># -*- python -*-
< </pre>as second line in your file (the first usually being #!/usr/bin/env python).
< ==
Using PythonTidy to pretty-print a buffer ==

till

> === Using PythonTidy to pretty-print a buffer ===


This page collects information for creating a usable Python programming environment in Emacs.

Python Modes

There are a number of Python modes for Emacs. The major ones right now are python.el which comes with Emacs 24.2 and up and python-mode.el which used to be included in Python (up to 2.6.8). The older python.el from Emacs 24.1 and before has a number of fans as well.

Emacs as a Python IDE

Beyond the basic Python modes described above, there are a number of modes that support Python programming.

Other Support Modes

Various Python Interpreters

IPython

Emacs’ 24.2+ python.el contains the following guide on how to set it up to use IPython 0.11:

(setq
 python-shell-interpreter "ipython"
 python-shell-interpreter-args ""
 python-shell-prompt-regexp "In \\[[0-9]+\\]: "
 python-shell-prompt-output-regexp "Out\\[[0-9]+\\]: "
 python-shell-completion-setup-code
   "from IPython.core.completerlib import module_completion"
 python-shell-completion-module-string-code
   "';'.join(module_completion('''%s'''))\n"
 python-shell-completion-string-code
   "';'.join(get_ipython().Completer.all_completions('''%s'''))\n")

For iPython 0.10 everything would be the same except for `python-shell-completion-string-code` and `python-shell-completion-module-string-code`:

(setq python-shell-completion-string-code
      "';'.join(__IP.complete('''%s'''))\n"
      python-shell-completion-module-string-code "")

Unfortunately running iPython on Windows needs some more tweaking. The way you must set `python-shell-interpreter` and `python-shell-interpreter-args` is as follows:

(setq
 python-shell-interpreter "C:\\Python27\\python.exe"
 python-shell-interpreter-args
 "-i C:\\Python27\\Scripts\\ipython-script.py")

That will spawn the iPython process correctly (Of course you need to modify the paths according to your system).

IronPython

Further Reading

Various Configuration Tips

Comment/Uncomment Region

If you have ‘transient-mark-mode’ on, you can just use ‘comment-dwim’: select a region and hit `M-;’. The DoWhatIMean means that it will comment or uncomment the region as appropriate. If you do not have ‘transient-mark-mode’ on by default, you can hit C-SPC twice to activate it temporarily.

( doesn’t python-mode.el offer `py-comment-region? --CH )

You can also use “rectangles” with comment/uncomment region (among other things that you can do with rectangles). See RectangleCommands or “(emacs) Rectangles” in the Emacs manual.

Browse the Python Documentation using Info

To browse the new Python documentation using Info, you will need the necessary Info files. These files are not currently available from the official download site since they are generated by a development version of Sphinx, the documentation system used by Python. You can however download a copy of the documentation for

These Info files are different than the ones generated from the old Python documentation (prior to python 2.5) which were not built by Sphinx. One result of these differences is that ‘info-lookup-symbol’ (C-h S) will not work correctly in ‘python-mode’ since the InfoLook support in “python.el” is based on the old version. To fix this issue, you can add the following code to your .emacs:

(require 'info-look)

(info-lookup-add-help
 :mode 'python-mode
 :regexp "[[:alnum:]_]+"
 :doc-spec
 '(("(python)Index" nil "")))

An alternative method for fixing ‘info-lookup-symbol’ support is provided by the pydoc-info package.

Using Flymake with Python

Using flymake with multiple lint, style and syntax checkers

More than one code checking tool can be applied in flymake by using a wrapper script that runs the desired tools and combines their output. It has lots of issues, one being that flymake does not seem to show more than one error message per line of code, meaning that an error or warning which is intentionally left unfixed can mask an error or warning that would get more attention.

Additionally, the scripts that check python code are either rather anemic, and don’t notice too much (pychecker) or are aggressive, and warn about all sorts of things that they should not (pylint). pep8.py tends to be annoyingly aggressive about whitespace.

There are several scripts around to do this, supporting various tools. Each expects to find the code checking tools in your path (pep8.py, pychecker, pylint, pyflakes, etc). Additionally this script attempts to support virtual environments, but this is largely untested.

I added this code to pastebins (didn’t know which to prefer, so chose three) which should be better than dumping the code here. – CH

Using flymake with pylint

Please note that pylint now comes with an executable “epylint” that is meant to be the interface to Emacs.

The flymake mode in Emacs 22 does not understand Python, but it can be made to work with pylint. To do this, first you’ll need to create a script somewhere along your PATH called ‘epylint’. That script is here

more pastebins needed? – CH

It might appear that for some combinations of flymake, pylint and epylint the above script might not work properly (it didn’t for me). Debugging showed that the regexp wasn’t matching the output of pylint, ever, so the lines were being printed from pylint verbatim, meaning that flymake was doing a “best effort” match, and everything was coming up as an error, even minor warnings.

Here is an alternative version of the epylint script that works great for me. It doesn’t throw away all the R and C errors, just the most annoying ones. You can easily modify it to include or exclude messages to your preference. I find it easier to have the messages in the script rather than messing about with .pylintrc and the like.

(I’ve also got a version that inserts the overall code quality metric, as determined by pylint, so you can see at a glance how close you are to pylint perfection; holler here if you want to see it).

that would be useful holler --CH

Once you test that this works on sample Python files, then add the following to your InitFile to enable flymake checking of Python scripts using pylint:

    (when (load "flymake" t)
      (defun flymake-pylint-init ()
        (let* ((temp-file (flymake-init-create-temp-buffer-copy
                           'flymake-create-temp-inplace))
           (local-file (file-relative-name
                        temp-file
                        (file-name-directory buffer-file-name))))
          (list "epylint" (list local-file))))
      (add-to-list 'flymake-allowed-file-name-masks
               '("\\.py\\'" flymake-pylint-init)))

Now if you turn on ‘flymake-mode’ inside a Python file, you should see lines with warnings highlighted in blue, and lines with errors highlighted in red. Just hover your mouse over either type of line for a few seconds and you’ll see a tooltip describing the error/warning. Plus, the modeline will show you two numbers, X/Y, where X is the number of errors in your script, and Y is the total number of warnings. – JohnWiegley

wait, this is for XEmacs, right? – CH

You can customise the way flymake highlights your Python by running ‘M-x customize-face’ on ‘flymake-errline’ or ‘flymake-warnline’. For example, you can remove the background highlight and make the face be just a subtle coloured underline.

Using flymake with pyflakes

Found a desciption on how to use this pair at www.plope.com/Members/chrism/flymake-mode I did a copy ‘n paste for simplicity.

(when (load "flymake" t)
  (defun flymake-pyflakes-init ()
     ; Make sure it's not a remote buffer or flymake would not work
     (when (not (subsetp (list (current-buffer)) (tramp-list-remote-buffers)))
      (let* ((temp-file (flymake-init-create-temp-buffer-copy
                         'flymake-create-temp-inplace))
             (local-file (file-relative-name
                          temp-file
                          (file-name-directory buffer-file-name))))
        (list "pyflakes" (list local-file)))))
  (add-to-list 'flymake-allowed-file-name-masks
               '("\\.py\\'" flymake-pyflakes-init)))

The normal operation of flymake allows the user to see the error message for a particular line by “hovering” the mouse over the line. This is inconvenient for people who try to use the keyboard for all input. FlymakeCursor was designed to address that: it displays the flymake error in the minibuffer region, when the cursor is placed on a line containing a flymake error.

Using flymake with pyflakes on Windows

Download the pyflakes tgz file from Pypi. Unpack it, then run the setup:

\Python27\python.exe \temp\\temp\pyflakes-0.5.0\setup.py install

This will install pyflakes.

To run pyflakes from a command prompt, you then need something like this:

\Python27\python.exe \Python27\Scripts\pyflakes myfile.py

To make it simpler, you can create a file called pyflakes.cmd and put it into c:\python27 . The contents should be:

@echo off
goto START
-------------------------------------------------------
 pyflakes.cmd
-------------------------------------------------------
:START
SETLOCAL
if  _%1==_ goto USAGE
%~dp0\python.exe %~dp0\Scripts\pyflakes %*
goto ALL_DONE

:USAGE
  echo usage:  pyflakes ^<file^> [^<optionalargs^>]
  echo run the pyflakes syntax checker on a python source file.

:ALL_DONE
ENDLOCAL

Then, modify one line in the elisp snip given above, to be like this:

(when (load "flymake" t)
  (defun flymake-pyflakes-init ()
     ; Make sure it's not a remote buffer or flymake would not work
     (when (not (subsetp (list (current-buffer)) (tramp-list-remote-buffers)))
      (let* ((temp-file (flymake-init-create-temp-buffer-copy
                         'flymake-create-temp-inplace))
             (local-file (file-relative-name
                          temp-file
                          (file-name-directory buffer-file-name))))
        (list "c:\\Python27\\pyflakes.cmd" (list local-file)))))
  (add-to-list 'flymake-allowed-file-name-masks
               '("\\.py\\'" flymake-pyflakes-init)))

Also see PyIndent

IPython debugging/pdbtrack

I made the following changes to python-mode.el to make debugging work again with current IPython (0.9.1):

[…]

 ;; pdbtrack constants
 (defconst py-pdbtrack-stack-entry-regexp
  "^> \\(.*\\)(\\([0-9]+\\))\\([?a-zA-Z0-9_<>]+\\)()"; this is new
 ;  "^> \\([^(]+\\)(\\([0-9]+\\))\\([?a-zA-Z0-9_]+\\)()"
 ;  "^> \\(.*\\)(\\([0-9]+\\))\\([?a-zA-Z0-9_]+\\)()"
   "Regular expression pdbtrack uses to find a stack trace entry.")
 ;(defconst py-pdbtrack-input-prompt "\n[(<]*[Pp]db[>)]+ "
 (defconst py-pdbtrack-input-prompt "\n[(<]*[Ii]?[Pp]db[>)]+ "; this is new
   "Regular expression pdbtrack uses to recognize a pdb prompt.")

[…]

            (py-pdbtrack-overlay-arrow nil)
          (setq block (ansi-color-filter-apply block)) ; this is new
          (setq target (py-pdbtrack-get-source-buffer block))

[…]

Now when I debug in the IPython console (using %run -d, or %pdb on, etc.) pdbtrack makes sure to show me the current source code and corresponding line. Note that these changes may break debugging in a standard python-shell, I have not tested this.

Using PythonTidy to pretty-print a buffer

PythonTidy can be used to pretty-print a buffer in combination with shell-command-on-region. Installing PythonTidy? somewhere on the path, and add this to your .emacs

(defun pytidy-whole-buffer ()
  (interactive)
  (let ((a (point)))
    (shell-command-on-region (point-min) (point-max) "<Name_Of_Executable_Script>" t)
    (goto-char a)))

Note: <Name_Of_Executable_Script> should match the name of the pythonTidy script on your path, make sure you set execute permissions.

bind this to any key you wish, I have:

(define-key python-mode-map  (kbd "<f9>") 'pytidy-whole-buffer)

in my python-mode-hook. invoking the command pretty-prints the hole buffer, you can invoke undo if something went wrong, and the cursor stays put.


Categories: CategoryProgramming, ProgrammingModes