spacemacs/layers/+lang/python
syl20bnr 82fdd9a511 Use evil in holy-mode
Motivation

While disabling Evil in holy-mode makes its implementation shorter and
sounds elegant on the paper, in practice it puts a big burden on the
configuration parts which need to know if Evil is enable or not. This is
a bad separation of concerns and the bunch of fixes that we were forced
to do in the past weeks shows this issue. Those fixes were about
removing the knowledge of the activation of Evil by implementing new
dispatching functions to be used by layers, this is cumbersome and makes
Spacemacs layer configuration more subtle which is not good. There was
additional bad consequences of the removal of Evil state like the
impossibility to use Evil lisp state or iedit states, or we would have
been forced to implement a temporary activation of Evil which is
awkward.

Instead I reintroduce Evil as the central piece of Spacemacs design thus
Evil is now re-enabled in holy-mode. It provides the abstraction we need
to isolate editing styles and be able to grow the Spacemacs
configuration coverage sanely. Layers don't need to check whether the
holy mode is active or not and they don't need to know if Evil is
available (it is always available). We also don't need to write
additional dispatching functions, this is the job of Evil, and I think
it provides everything for this. Ideally configuration layer should be
implemented with only Evil in mind and the holy-mode (and hybrid-mode)
should magically make it work for Emacs style users, for instance we can
freely use `evil-insert-state` anywhere in the code without any guard.

Evil is now even more part of Spacemacs, we can really say that
Spacemacs is Emacs+Evil which is now an indivisible pair. Spacemacs
needed this stable API to continue on the right track.

While these changes should be rather transparent to the user, I'm sorry
for this experimental period, I failed to see all the implications of
such a change, I was just excited about the possibility to make Evil
optional. The reality is that Spacemacs has to embrace it and keep its
strong position on being Emacs+Evil at the core.

Implementation

- insert, motion and normal states are forced to emacs state using an
advice on `evil-insert-state`, `evil-motion-state` and
`evil-normal-state` respectively. These functions can be used freely in
the layer configuration.
- A new general hook `spacemacs-editing-style-hook` allow to hook any
code that need to be configured based on the editing style. Functions
hooked to this hook takes the current style as parameter, this
basically generalize the hook used to setup hjkl navigation bindings.
- ESC has been removed from the emacs state map.
- Revert unneeded changes
  - Revert "evil: enter insert-state only from normal-state"
    commit bdd702dfbe.
  - Revert "avoid being evil in deft with emacs editing style"
    commit f3a16f49ed.

Additional changes

All editing style packages have been moved to a layer called
`spacemacs-editing-styles`

Notes

I did not have time to attack hybrid mode, I should be able to do it
later.
2016-03-13 21:16:55 -04:00
..
img Use + instead of ! for layer categories 2015-09-11 00:13:51 -04:00
local Fix makefile for python version 2.7.10 2015-12-03 23:59:15 -05:00
config.el Clean copyrights and update for year 2016 2016-01-11 21:42:17 -05:00
funcs.el Revert "Set global pyenv version if no .python-version file exists" 2016-01-26 01:21:02 -05:00
packages.el Use evil in holy-mode 2016-03-13 21:16:55 -04:00
README.org [doc] Fix some additional SPC : to SPC SPC 2016-02-07 14:39:54 +01:00

Python layer

/TakeV/spacemacs/media/commit/ca5b8be90b3de41f35b24770857d50f90f6c5544/layers/+lang/python/img/python.png

Description

This layer adds support for the Python language.

Features

Install

Layer

To use this configuration layer, add it to your ~/.spacemacs. You will need to add python to the existing dotspacemacs-configuration-layers list in this file.

Test runner

Both nose and pytest are supported. By default nose is used. To choose your test runner set the layer variable python-test-runner to either nose or pytest.

The root of the project is detected with a .git directory or a setup.cfg file.

(setq-default dotspacemacs-configuration-layers
  '((python :variables python-test-runner 'pytest)))

Anaconda dependencies

anaconda-mode tries to install the dependencies itself but sometimes it does not work and you may encounter the following message when opening a python buffer:

    Blocking call to accept-process-output with quit inhibited!!

To fix this, install the anaconda-mode anaconda-deps by hand:

    pip install  jedi==0.8.1 json-rpc==1.8.1 service_factory==0.1.2

Source: https://github.com/proofit404/anaconda-mode#issues

If you are facing errors such as "Unable to run anaconda-mode server", try setting your PYTHONPATH as explained at https://github.com/proofit404/anaconda-mode#pythonpath

Automatic buffer formatting on save

To enable automatic buffer formatting on save with YAPF set the variable python-enable-yapf-format-on-save to t.

  (setq-default dotspacemacs-configuration-layers '(
    (python :variables python-enable-yapf-format-on-save t)))

autoflake

To be able to suppress unused imports easily, install autoflake:

  pip install autoflake

pylookup

To use pylookup on SPC m h H, make sure you update the database first, using SPC SPC pylookup-update.

Management of Python versions and virtual environments

Manage virtual environments with pyvenv

A virtual environment provides isolation of your Python package versions. For a general overview see this site. Virtualenvwrapper which is also explained in the previous link, is a program which manages your virtual environments in a central location set by the WORKON_HOME environment variable.

Spacemacs integration of virtual environments and virtualenvwrapper is provided by the pyvenv package. It provides the following keybindings:

Key Binding Description
SPC m V a activate a virtual environment in any directory
SPC m V d deactivate active virtual environment
SPC m V w work on virtual environment in WORKON_HOME

Manage multiple Python versions with pyenv

If you need multiple Python versions (e.g. Python 2 and Python 3) then take a look at pyenv. It enables the installation and managment of multiple Python versions. This blogpost gives a good overview on how to use the tool. Spacemacs integration is provided by pyenv mode which has the following keybindings.

Key Binding Description
SPC m v s set a pyenv environment with pyenv
SPC m v u unset a pyenv environment with pyenv

Pyenv can also manage virtual environments for each of the Python versions it has installed. Those will be listed alongside your Python versions.

Automatic activation of local pyenv version

A project-specific pyenv version may be written to a file called .python-version using the pyenv local command.

Spacemacs can search in parent directories for this file, and automatically set the pyenv version. The behavior can be set with the variable python-auto-set-local-pyenv-version to:

  • on-visit (default) set the version when you visit a python buffer,
  • on-project-switch set the version when you switch projects,
  • nil to disable.

Key Bindings

Inferior REPL process

Start a Python or iPython inferior REPL process with SPC m s i. If ipython is available in system executable search paths, ipython will be used to launch python shell; otherwise, default python interpreter will be used. You may change your system executable search path by activating a virtual environment.

Send code to inferior process commands:

Key Binding Description
SPC m s b send buffer and keep code buffer focused
SPC m s B send buffer and switch to REPL in insert mode
SPC m s f send function and keep code buffer focused
SPC m s F send function and switch to REPL in insert mode
SPC m s i start inferior REPL process
SPC m s r send region and keep code buffer focused
SPC m s R send region and switch to REPL in insert mode
CTRL+j next item in REPL history
CTRL+k previous item in REPL history

Running Python Script in shell

To run a Python script like you would in the shell press SPC m c c to start the Python script in comint mode. This is useful when working with multiple Python files since the REPL does not reload changes made in other modules.

Key Binding Description
SPC m c c Execute current file in a comint shell
SPC m c C Execute current file in a comint shell and switch to it in insert state

Note: With the universal argument SPC u you can enter a new compilation command.

Testing

Test commands start with m t:

No Debug Description
SPC m t a launch all tests of the project
SPC m t b launch all tests of the current buffer (same as module)
SPC m t m launch all tests of the current module
SPC m t s launch all tests of the current suite (only with nose)
SPC m t t launch the current test (function)
Debug Description
SPC m t A launch all tests of the project in debug mode
SPC m t B launch all tests of the current buffer (module) in debug mode
SPC m t M launch all tests of the current module in debug mode
SPC m t S launch all tests of the current suite in debug mode (only with nose)
SPC m t T launch the current test (function) in debug mode

Refactoring

Key Binding Description
SPC m r i remove unused imports with autoflake

Live coding

Live coding is provided by the live-py-plugin.

Key Binding Description
SPC m l Toggle live-py-mode

Other Python commands

Key Binding Description
SPC m = Reformat the buffer according to PEP8 using YAPF
SPC m d b toggle a breakpoint
SPC m g g go to definition using anaconda-mode-find-definitions (C-o to jump back)
SPC m g a go to assignment using anaconda-mode-find-assignments (C-o to jump back)
SPC m g b jump back
SPC m g u navigate between usages with anaconda-mode-find-references
SPC m h d look for documentation using helm-pydoc
SPC m h h quick documentation using anaconda
SPC m h H open documentation in firefox using pylookup
SPC m v s set a pyenv environment with pyenv
SPC m v u unset a pyenv environment with pyenv
SPC m V w work on virtual environment in WORKON_HOME
SPC m V a activate a virtual environment in any directory
SPC m V d deactivate active virtual environment

Configuration

Fill column

If you want to customize the fill column value, use something like this inside the user-init function in your .spacemacs:

(setq python-fill-column 99)