2014-11-10 04:54:10 +00:00
# Python contribution layer for Spacemacs
2015-01-31 03:42:32 +00:00
![logo_python ](img/python.png ) ![logo_django ](img/django.png )
2014-11-10 04:54:10 +00:00
2014-12-10 04:36:33 +00:00
<!-- markdown - toc start - Don't edit this section. Run M - x markdown - toc/generate - toc again -->
**Table of Contents**
- [Python contribution layer for Spacemacs ](#python-contribution-layer-for-spacemacs )
- [Description ](#description )
- [Install ](#install )
2015-04-12 15:26:19 +00:00
- [Layer ](#layer )
- [Anaconda dependencies ](#anaconda-dependencies )
2015-04-22 04:02:23 +00:00
- [Automatic buffer formatting on save ](#automatic-buffer-formatting-on-save )
2014-12-10 04:36:33 +00:00
- [Key Bindings ](#key-bindings )
- [Inferior REPL process ](#inferior-repl-process )
2015-03-14 06:58:11 +00:00
- [Running Python Script in shell ](#running-python-script-in-shell )
2014-12-10 04:36:33 +00:00
- [Testing in Python ](#testing-in-python )
- [Other Python commands ](#other-python-commands )
2015-01-31 03:41:55 +00:00
- [Django ](#django )
- [Fabric ](#fabric )
- [Files ](#files )
- [Interactive ](#interactive )
- [Server ](#server )
- [South/Syncdb ](#southsyncdb )
- [Test ](#test )
2014-12-10 04:36:33 +00:00
<!-- markdown - toc end -->
## Description
This layer adds support for the Python language.
Features:
- Auto-completion using [anaconda-mode][]
- Code Navigation using [anaconda-mode][]
- Documentation Lookup using [anaconda-mode][] and [pylookup][]
- Test Runner using [nose.el][]
2015-03-21 04:58:59 +00:00
- Virtual Environment using [pyvenv][] and [pyenv][]
2014-12-10 04:36:33 +00:00
- semantic mode is enabled
2015-01-31 03:41:55 +00:00
- Django support via [pony-mode][]
2015-04-22 04:02:23 +00:00
- PEP8 compliant formatting via [YAPF][]
2014-11-10 04:54:10 +00:00
## Install
2015-04-12 15:26:19 +00:00
### Layer
2014-11-10 04:54:10 +00:00
To use this contribution add it to your `~/.spacemacs`
```elisp
2015-02-18 03:36:12 +00:00
(setq-default dotspacemacs-configuration-layers '(python))
2014-11-10 04:54:10 +00:00
```
2015-04-12 15:26:19 +00:00
### 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` [dependencies][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
2015-04-22 04:02:23 +00:00
### 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` .
```elisp
(setq-default dotspacemacs-configuration-layers '(
(python :variables python-enable-yapf-format-on-save t)))
```
2014-11-10 04:54:10 +00:00
## Key Bindings
### Inferior REPL process
2015-02-18 03:07:43 +00:00
Start a Python or iPython inferior REPL process with < kbd > SPC m s i< / kbd > .
2014-12-12 01:08:38 +00:00
If `ipython` is available in system executable search paths, `ipython`
2014-11-10 04:54:10 +00:00
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:
2015-02-18 03:07:43 +00:00
Key Binding | Description
----------------------|------------------------------------------------------------
< kbd > SPC m s b< / kbd > | send buffer and keep code buffer focused
< kbd > SPC m s B< / kbd > | send buffer and switch to REPL in insert mode
< kbd > SPC m s f< / kbd > | send function and keep code buffer focused
< kbd > SPC m s F< / kbd > | send function and switch to REPL in insert mode
< kbd > SPC m s i< / kbd > | start inferior REPL process
< kbd > SPC m s r< / kbd > | send region and keep code buffer focused
< kbd > SPC m s R< / kbd > | send region and switch to REPL in insert mode
< kbd > CTRL+j< / kbd > | next item in REPL history
< kbd > CTRL+k< / kbd > | previous item in REPL history
2014-11-10 04:54:10 +00:00
2015-03-14 06:58:11 +00:00
### Running Python Script in shell
2015-03-11 07:26:57 +00:00
2015-03-14 06:58:11 +00:00
To run a Python script like you would in the shell press < kbd > SPC m c c< / kbd >
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
----------------------|------------------------------------------------------------
< kbd > SPC m c c< / kbd > | Execute current file in a comint shell
< kbd > SPC m c C</ kbd > | Execute current file in a comint shell and switch to it in `insert state`
**Note** With the universal argument < kbd > SPC u< / kbd > you can enter a new
compilation command.
2015-03-11 07:26:57 +00:00
2014-11-10 04:54:10 +00:00
### Testing in Python
`Spacemacs` uses [nose][nose] as a test runner. An improved version of
[nose.el][nose.el] is shipped with `Spacemacs` , this version adds:
- windows support
- test suite support
The root of the project is detected with a `.git` directory or a `setup.cfg` file.
2014-12-12 01:08:38 +00:00
Test commands (start with < kbd > m t< / kbd > or < kbd > m T< / kbd > ):
2014-11-10 04:54:10 +00:00
2014-12-12 01:08:38 +00:00
No Debug | Description
---------------------|------------------------------------------------------------
< kbd > SPC m t a< / kbd > | launch all tests of the project
< kbd > SPC m t b< / kbd > | launch all tests of the current buffer (same as module)
< kbd > SPC m t m< / kbd > | launch all tests of the current module
< kbd > SPC m t s< / kbd > | launch all tests of the current suite
< kbd > SPC m t t< / kbd > | launch the current test (function)
2014-11-10 04:54:10 +00:00
2014-12-12 01:08:38 +00:00
Debug | Description
---------------------|------------------------------------------------------------
< kbd > SPC m T a< / kbd > | launch all tests of the project in debug mode
< kbd > SPC m T b< / kbd > | launch all tests of the current buffer (module) in debug mode
< kbd > SPC m T m< / kbd > | launch all tests of the current module in debug mode
< kbd > SPC m T s< / kbd > | launch all tests of the current suite in debug mode
< kbd > SPC m T t< / kbd > | launch the current test (function) in debug mode
2014-11-10 04:54:10 +00:00
### Other Python commands
2015-01-14 15:12:09 +00:00
Key Binding | Description
----------------------|------------------------------------------------------------
2015-04-22 04:02:23 +00:00
< kbd > SPC m =< / kbd > | Reformat the buffer according to PEP8 using [YAPF][]
2015-01-14 15:12:09 +00:00
< kbd > SPC m d b< / kbd > | toggle a breakpoint
2015-02-13 03:21:35 +00:00
< kbd > SPC m g g</ kbd > | go to definition using `anaconda-mode-goto` (< kbd > C-o</ kbd > to jump back)
2015-04-04 04:19:17 +00:00
< kbd > SPC m h d</ kbd > | look for documentation using `helm-pydoc`
2015-02-13 03:21:35 +00:00
< kbd > SPC m h h< / kbd > | quick documentation using anaconda
< kbd > SPC m h H</ kbd > | open documentation in `firefox` using [pylookup][pylookup]
2015-03-21 04:58:59 +00:00
< kbd > SPC m v< / kbd > | activate a virtual environment with [pyenv][pyenv]
< kbd > SPC m V< / kbd > | activate a virtual environment with [pyvenv][pyvenv]
2015-04-24 05:12:30 +00:00
< kbd > SPC c i< / kbd > | remove unused imports with [autoflake][autoflake]
2014-11-10 04:54:10 +00:00
2015-01-31 03:41:55 +00:00
### Django
Django related key bindings uses [pony-mode][] and are behind the prefix
< kbd > SPC m j< / kbd > .
2015-01-31 15:08:10 +00:00
Configuration options for pony-mode are documented at
[deadpansincerity.com ](http://www.deadpansincerity.com/docs/pony/configuration.html )
2015-01-31 03:41:55 +00:00
Manage Django with < kbd > SPC m j m< / kbd > .
#### Fabric
Key Binding | Description
----------------------|------------------------------------------------------------
2015-01-31 15:08:10 +00:00
< kbd > mjaf< / kbd > | Run a fabric command
< kbd > mjad</ kbd > | Deploy project with `fab deploy`
2015-01-31 03:41:55 +00:00
#### Files
Key Binding | Description
----------------------|------------------------------------------------------------
2015-01-31 15:08:10 +00:00
< kbd > mjfs</ kbd > | Open the `settings.py` for this project
< kbd > mjfc< / kbd > | Interactively display a setting value in the minibuffer
< kbd > mjft< / kbd > | Jump to template at point
< kbd > mjfr< / kbd > | Jump to the view file that the URL resolves to (experimental)
2015-01-31 03:41:55 +00:00
#### Interactive
Key Binding | Description
----------------------|------------------------------------------------------------
2015-01-31 15:08:10 +00:00
< kbd > mjid< / kbd > | Run interpreter for this project's default database as an inferior process
< kbd > mjis</ kbd > | Open a Python shell with the current pony project's context loaded. If the project has the django_extras package installed, then use the excellent `shell_plus` command. Otherwise, fall back to `manage.py shell`
2015-01-31 03:41:55 +00:00
#### Server
Key Binding | Description
----------------------|------------------------------------------------------------
2015-01-31 15:08:10 +00:00
< kbd > mjrd< / kbd > | Stop the dev server
< kbd > mjro< / kbd > | Open a tab at the dev server
< kbd > mjrr< / kbd > | Restart the dev server (works better with django_extras/werkzeug)
< kbd > mjru< / kbd > | Start or open the dev server
< kbd > mjrt< / kbd > | Open a second server with a "throwaway" host/port
2015-01-31 03:41:55 +00:00
#### South/Syncdb
Key Binding | Description
----------------------|------------------------------------------------------------
2015-01-31 15:08:10 +00:00
< kbd > mjsc< / kbd > | Convert an existing app to south
< kbd > mjsh< / kbd > | Create migration for modification
< kbd > mjsi< / kbd > | Run the initial south migration for an app
< kbd > mjsm< / kbd > | Migrate an app
< kbd > mjss< / kbd > | Run syncdb on the current project
2015-01-31 03:41:55 +00:00
#### Test
Key Binding | Description
----------------------|------------------------------------------------------------
2015-01-31 15:08:10 +00:00
< kbd > mjtd< / kbd > | Move down the traceback one level
< kbd > mjte< / kbd > | Go to the file and line of the last stack trace in a test buffer
< kbd > mjto< / kbd > | Open the file in a traceback at the line specified
< kbd > mjtt</ kbd > | Run the test(s) given by `command`
< kbd > mjtu< / kbd > | Move up the traceback one level
2015-01-31 03:41:55 +00:00
2014-11-10 04:54:10 +00:00
[anaconda-mode]: https://github.com/proofit404/anaconda-mode
[pyvenv]: https://github.com/jorgenschaefer/pyvenv
2015-03-21 04:58:59 +00:00
[pyenv]: https://github.com/yyuu/pyenv
2014-11-10 04:54:10 +00:00
[pylookup]: https://github.com/tsgates/pylookup
[nose]: https://github.com/nose-devs/nose/
[nose.el]: https://github.com/syl20bnr/nose.el
2015-01-31 03:41:55 +00:00
[pony-mode]: https://github.com/davidmiller/pony-mode
2015-04-12 15:26:19 +00:00
[anaconda-deps]: https://github.com/proofit404/anaconda-mode/blob/master/requirements.txt
2015-04-22 04:02:23 +00:00
[YAPF]: https://github.com/google/yapf
2015-04-24 05:12:30 +00:00
[autoflake]: https://github.com/myint/autoflake