blog.lobraun.de Python

ELPY Cheat Sheet

ELPY is a python programming environment. The features are documented here.

The following table summarises the default key bindings for the functions of version 1.11.0:

Key Binding Function
C-down Forward one indentation block
C-up Backward one indentation block
C-left Backward one indentation level
C-right Forward one indentation level
M-down Move line or region down
M-up Move line or region up
M-left Move line or indentation block left
M-right Move line or indentation block right
M-x elpy-set-project-root Set the root directory of the python project
C-c C-f Find file in project
C-c C-s Regular expression match in project
M-TAB Complete current statement
M-. Goto defition
M-* Return to position from last definition lookup
C-c C-z Switch to python shell
C-c C-c Send python code to shell (active region or complete buffer)
C-c RET Send current line to python shell
C-M-x Sends code of current top level function to python shell
C-c C-v Syntax check with flake8
C-c C-n Next flake8 error
C-c C-p Previous flake8 error
C-c C-t Start tests
C-c C-e Edit all occurrences of the symbol at point at once
C-c C-r f Format code using the available formatter
C-c C-r i Query for new imports of unresolved symbols, and remove unreferenced imports
C-c C-r r Run the Elpy refactoring interface for Python code

Python PIP Package Updates on Mac OS X

Updating all python packages that are installed via pip is often referred to as running something like the commands

pip freeze --local | grep -v '^\-e' | cut -d = -f 1  | xargs -n1 pip install -U

However, some problems can occur on Mac OS X, as certain default packages are included into the base python installation.

Packages that rely on new versions of pre-installed packages will throw errors as the default python search path prefers the pre-installed packages. An example is matplotlib: Version 1.5 of matplotlib requires a newer version of the six package than version 1.4.1 that is shipped on El Capitan and errors out with the following message:

/Library/Python/2.7/site-packages/dateutil/rrule.py in <module>()
 14 
 15 from six import advance_iterator, integer_types
---> 16 from six.moves import _thread
 17 
 18 __all__ = ["rrule", "rruleset", "rrulestr",

ImportError: cannot import name _thread

You can fix this problem by properly setting your python path environment to prefer the python pip installed packages. The path can for example be set in the ~/.bashrc to point to

export PYTHONPATH="/Library/Python/2.7/site-packages:$PYTHONPATH"

PEP8 for vim using flake8

In order to auto format your python code according to the guidelines defined by PEP8, you can use some sane default for editing python files and flake8 to check and fix possible errors in your scripts.

Default indentation rules for python files

The default indentation rules do not comply with PEP8. In order to change them for python files, create a new file in ~/.vim/ftplugin/python.vim and place the following content in this file:

setlocal tabstop=4
setlocal softtabstop=4
setlocal shiftwidth=4
setlocal smarttab
setlocal expandtab
setlocal shiftround
setlocal autoindent

PEP8 recommends that code should have a maximum line length of 79 characters. You can enforce this by adding the following line to the file:

setlocal textwidth=79

However, it can be a good idea to have longer lines, I prefer to not have a hard wrap at 79. You can therefore opt to show a marker that helps you to avoid the character line limit by setting:

setlocal colorcolumn=80
Install flake8

flake8 is a python module that you can install using your favourite package manager, e.g. using pip:

sudo pip install flake
Install vim-pathogen and vim-flake8

vim-pathogen is a vim script that helps with managing the runtimepath to simplify the vim plugin management. You can install it by running the following command:

mkdir -p ~/.vim/autoload ~/.vim/bundle && \
curl -LSso ~/.vim/autoload/pathogen.vim https://tpo.pe/pathogen.vim

then add the following to your .vimrc

execute pathogen#infect()

Afterwards, install vim-flake8:

cd ~/.vim/bundle
git clone https://github.com/nvie/vim-flake8

This is everything you need to do. Afterwards, you can open vim on a python file and press < F7 > to start flake and obtain information on your formatting.