2017-09-10 15:13:03 +08:00
---
title: "SpaceVim lang#python layer"
2019-02-03 20:51:01 +08:00
description: "This layer is for Python development, provide autocompletion, syntax checking, code format for Python file."
2017-09-10 15:13:03 +08:00
---
2018-06-23 14:37:41 +08:00
# [Available Layers](../../) >> lang#python
2017-09-10 15:13:03 +08:00
<!-- vim - markdown - toc GFM -->
2017-12-02 23:23:58 +08:00
- [Description ](#description )
- [Features ](#features )
2017-12-11 19:55:53 +08:00
- [Install ](#install )
2019-06-02 16:43:38 +08:00
- [enable layer ](#enable-layer )
- [language tools ](#language-tools )
2019-09-06 00:34:36 +08:00
- [Configuration ](#configuration )
2017-12-11 19:55:53 +08:00
- [Key bindings ](#key-bindings )
2018-06-23 14:37:41 +08:00
- [Jump to definition ](#jump-to-definition )
2018-01-20 19:35:13 +08:00
- [Code generation ](#code-generation )
2019-06-02 16:43:38 +08:00
- [Code Coverage ](#code-coverage )
2019-04-24 22:54:44 +08:00
- [Text objects and motions ](#text-objects-and-motions )
2017-12-11 19:55:53 +08:00
- [Inferior REPL process ](#inferior-repl-process )
- [Running current script ](#running-current-script )
- [Testing ](#testing )
- [Refactoring ](#refactoring )
2017-09-10 15:13:03 +08:00
<!-- vim - markdown - toc -->
## Description
This layer is for Python development.
## Features
2018-05-11 20:08:39 +08:00
- Auto-completion using [deoplete-jedi ](https://github.com/zchee/deoplete-jedi ) or [jedi-vim ](https://github.com/davidhalter/jedi-vim )
2017-09-10 15:13:03 +08:00
- Documentation Lookup using [jedi-vim ](https://github.com/davidhalter/jedi-vim )
2017-12-11 19:55:53 +08:00
## Install
2019-06-02 16:43:38 +08:00
### enable layer
2017-12-11 19:55:53 +08:00
2018-06-23 14:37:41 +08:00
To use this configuration layer, add following snippet to your custom configuration file.
```toml
[[layers]]
name = "lang#python"
```
2017-12-11 19:55:53 +08:00
2019-06-02 16:43:38 +08:00
### language tools
**syntax checking:**
2017-12-11 19:55:53 +08:00
2019-02-03 20:51:01 +08:00
`checker` layer provide syntax checking feature, and for Python it uses `flake8` package:
2017-12-11 19:55:53 +08:00
```sh
pip install --user flake8
```
2019-06-02 16:43:38 +08:00
**code formatting:**
2017-12-11 19:55:53 +08:00
2019-09-25 18:37:22 +08:00
The default key binding for formatting buffer is `SPC b f` ,
and you need to install `yapf` .
To enable automatic buffer formatting on save,
load this layer with setting `format_on_save` to `1` .
2017-12-11 19:55:53 +08:00
2019-09-25 18:37:22 +08:00
```toml
2018-08-30 20:38:21 +08:00
[[layers]]
name = "lang#python"
2019-09-25 18:37:22 +08:00
format_on_save = 1
2018-08-30 20:38:21 +08:00
```
2017-12-11 19:55:53 +08:00
```sh
pip install --user yapf
```
2020-04-16 22:56:26 +08:00
To use other tool as the format command, for example `black` , change the neoformat option in bootstrap
function.
```viml
let g:neoformat_python_black = {
\ 'exe': 'black',
\ 'stdin': 1,
\ 'args': ['-q', '-'],
\ }
let g:neoformat_enabled_python = ['black']
```
2019-06-02 16:43:38 +08:00
**format imports:**
2017-12-11 19:55:53 +08:00
To be able to suppress unused imports easily, install [autoflake ](https://github.com/myint/autoflake ):
```sh
pip install --user autoflake
```
To be able to sort your imports, install [isort ](https://github.com/timothycrosley/isort )
```sh
pip install --user isort
```
2019-06-02 16:43:38 +08:00
**code coverage:**
To be able to show code coverage, install coverage.py
```sh
pip install --user coverage
```
2019-09-06 00:34:36 +08:00
## Configuration
By default, when create a new python file, SpaceVim will insert file head automatically.
2019-09-25 18:37:22 +08:00
to change the file head, use `python_file_head` option:
2019-09-06 00:34:36 +08:00
```toml
[[layers]]
name = "lang#python"
2019-09-25 18:37:22 +08:00
python_file_head = [
2019-09-06 00:34:36 +08:00
'#!/usr/bin/env python',
'# -*- coding: utf-8 -*-',
'',
''
]
```
2019-09-25 18:37:22 +08:00
When enable autocomplete layer, the symbol will be complete automatically. By default the type info
is disabled, because it is too slow. To enable type info:
```toml
[[layers]]
name = "lang#python"
enable_typeinfo = true
```
2017-12-11 19:55:53 +08:00
## Key bindings
2018-06-23 14:37:41 +08:00
### Jump to definition
2019-02-03 20:51:01 +08:00
| Mode | Key Bindings | Description |
| ------ | ------------ | ------------------------------------------------ |
| normal | `g d` | Jump to the definition position of cursor symbol |
2018-06-23 14:37:41 +08:00
2018-01-20 19:35:13 +08:00
### Code generation
2018-06-23 14:37:41 +08:00
| Mode | Key Binding | Description |
| ------ | ----------- | ------------------ |
| normal | `SPC l g d` | Generate docstring |
2018-01-20 19:35:13 +08:00
2019-06-02 16:43:38 +08:00
### Code Coverage
| Mode | Key Binding | Description |
| ------ | ----------- | ----------------- |
| normal | `SPC l c r` | coverager report |
| normal | `SPC l c s` | coverager show |
| normal | `SPC l c e` | coverager session |
| normal | `SPC l c f` | coverager refresh |
2019-04-24 22:54:44 +08:00
### Text objects and motions
This layer contains vim-pythonsense which provides text objects and motions for Python classes, methods, functions, and doc strings.
| Text Objects | Descriptions |
| ------------ | ------------------------ |
| `ac` | Outer class text object. |
2017-12-11 19:55:53 +08:00
### Inferior REPL process
2019-02-03 20:51:01 +08:00
Start a Python or iPython inferior REPL process with `SPC l 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.
2017-12-11 19:55:53 +08:00
Send code to inferior process commands:
2019-02-03 20:51:01 +08:00
| Key Bindings | Descriptions |
| ------------ | ------------------------------------------------ |
| `SPC l s b` | send buffer and keep code buffer focused |
| `SPC l s l` | send line and keep code buffer focused |
| `SPC l s s` | send selection text and keep code buffer focused |
2017-12-11 19:55:53 +08:00
### Running current script
2019-02-03 20:51:01 +08:00
To running a Python script, you can press `SPC l r` to run current file without loss focus, and the result will be shown in a runner buffer.
2017-12-11 19:55:53 +08:00
### Testing
### Refactoring
2019-02-03 20:51:01 +08:00
| Key Bindings | Descriptions |
| ------------ | ------------------------------------ |
| `SPC l i r` | remove unused imports with autoflake |
| `SPC l i s` | sort imports with isort |