spacemacs/layers/+lang/python/README.org

184 lines
8.6 KiB
Org Mode
Raw Normal View History

2015-06-10 16:44:30 +00:00
#+TITLE: Python contribution layer for Spacemacs
#+HTML_HEAD_EXTRA: <link rel="stylesheet" type="text/css" href="../../../css/readtheorg.css" />
2015-06-10 16:44:30 +00:00
[[file:img/python.png]]
2015-06-10 16:44:30 +00:00
* Table of Contents :TOC_4_org:noexport:
- [[Description][Description]]
- [[Features:][Features:]]
- [[Install][Install]]
- [[Layer][Layer]]
- [[Test runner][Test runner]]
- [[Anaconda dependencies][Anaconda dependencies]]
- [[Automatic buffer formatting on save][Automatic buffer formatting on save]]
- [[autoflake][autoflake]]
- [[pylookup][pylookup]]
- [[Key Bindings][Key Bindings]]
- [[Inferior REPL process][Inferior REPL process]]
- [[Running Python Script in shell][Running Python Script in shell]]
- [[Testing][Testing]]
- [[Refactoring][Refactoring]]
- [[Other Python commands][Other Python commands]]
- [[Configuration][Configuration]]
- [[Fill column][Fill column]]
2015-06-10 16:44:30 +00:00
* Description
This layer adds support for the Python language.
** Features:
- Auto-completion using [[https://github.com/proofit404/anaconda-mode][anaconda-mode]]
- Code Navigation using [[https://github.com/proofit404/anaconda-mode][anaconda-mode]]
2015-06-10 16:44:30 +00:00
- Documentation Lookup using [[https://github.com/proofit404/anaconda-mode][anaconda-mode]] and [[https://github.com/tsgates/pylookup][pylookup]]
- Test Runners using [[https://github.com/syl20bnr/nose.el][nose.el]] or [[https://github.com/ionrock/pytest-el][pytest]]
- Virtual Environment using [[https://github.com/jorgenschaefer/pyvenv][pyvenv]] and [[https://github.com/yyuu/pyenv][pyenv]]
- semantic mode is enabled
- PEP8 compliant formatting via [[https://github.com/google/yapf][YAPF]]
- Suppression of unused import with [[https://github.com/myint/autoflake][autoflake]]
- Use the ~%~ key to jump between blocks with [[https://github.com/redguardtoo/evil-matchit][evil-matchit]]
2015-06-10 16:44:30 +00:00
* Install
2015-06-10 16:44:30 +00:00
** Layer
To use this contribution add it to your =~/.spacemacs=
#+BEGIN_SRC emacs-lisp
(setq-default dotspacemacs-configuration-layers '(python))
#+END_SRC
** 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.
#+BEGIN_SRC emacs-lisp
(setq-default dotspacemacs-configuration-layers
'((python :variables python-test-runner 'pytest)))
#+END_SRC
** 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:
#+begin_example
2015-06-10 16:44:30 +00:00
Blocking call to accept-process-output with quit inhibited!!
#+end_example
2015-06-10 16:44:30 +00:00
To fix this, install the =anaconda-mode= [[https://github.com/proofit404/anaconda-mode/blob/master/requirements.txt][anaconda-deps]] by hand:
#+begin_src sh
pip install jedi==0.8.1 json-rpc==1.8.1 service_factory==0.1.2
#+end_src
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
2015-06-10 16:44:30 +00:00
** Automatic buffer formatting on save
To enable automatic buffer formatting on save with [[https://github.com/google/yapf][YAPF]] set the variable
=python-enable-yapf-format-on-save= to =t=.
#+BEGIN_SRC emacs-lisp
(setq-default dotspacemacs-configuration-layers '(
(python :variables python-enable-yapf-format-on-save t)))
#+END_SRC
** autoflake
To be able to suppress unused imports easily, install [[https://github.com/myint/autoflake][autoflake]]:
#+BEGIN_SRC sh
pip install autoflake
#+END_SRC
** pylookup
To use =pylookup= on ~SPC m h H~, make sure you update the database first, using
~SPC : pylookup-update~.
2015-06-10 16:44:30 +00:00
* 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
2015-06-10 16:44:30 +00:00
compilation command.
** Testing
Test commands start with ~m t~:
2015-06-10 16:44:30 +00:00
| 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 |
2015-06-10 16:44:30 +00:00
** Refactoring
| Key Binding | Description |
|-------------+--------------------------------------|
| ~SPC m r i~ | remove unused imports with [[https://github.com/myint/autoflake][autoflake]] |
** Other Python commands
| Key Binding | Description |
|-------------+------------------------------------------------------------------------------|
| ~SPC m =~ | Reformat the buffer according to PEP8 using [[https://github.com/google/yapf][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 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 |
2015-10-18 18:13:11 +00:00
| ~SPC m h H~ | open documentation in =firefox= using [[https://github.com/tsgates/pylookup][pylookup]] |
| ~SPC m v s~ | activate a virtual environment with [[https://github.com/yyuu/pyenv][pyenv]] |
| ~SPC m v u~ | deactivate a virtual environment with [[https://github.com/yyuu/pyenv][pyenv]] |
| ~SPC m V~ | activate a virtual environment with [[https://github.com/jorgenschaefer/pyvenv][pyvenv]] |
* Configuration
** Fill column
If you want to customize the fill column value, use something like this inside the ~user-init~ function in your ~.spacemacs~:
#+BEGIN_SRC elisp
(setq python-fill-column 99)
#+END_SRC