Back to Spacevim

SpaceVim lang#python layer

docs/layers/lang/python.md

2.4.07.0 KB
Original Source

Available Layers >> lang#python

<!-- vim-markdown-toc GFM --> <!-- vim-markdown-toc -->

Description

This layer is for Python development.

Installation

Enable language layer

The lang#python layer is not loaded by default, to use this layer, you need to add following snippet into your spacevim configuration file.

toml
[[layers]]
  name = "lang#python"

Language tools

  • syntax checking:

    checker layer provides syntax checking feature, and for Python it uses the pylint package:

    sh
    pip install --user pylint
    
  • code formatting:

    The default key binding for formatting the current buffer is SPC b f, and you need to install yapf.

    sh
    pip install --user yapf
    

    To use another 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']
    
  • code formatting:

    The default formatter for python is yapf.

    pip install --user yapf
    

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

    pip install --user autoflake
    

    To be able to sort your imports, install isort

    pip install --user isort
    
  • code coverage:

    To be able to show code coverage, install coverage.py

    pip install --user coverage
    
  • language server

    To enable python support of lsp layer. You may need to install pyright or python-lsp-server:

    npm install -g pyright
    

    or

    pip install python-lsp-server
    

    Also you need enable lsp layer with pyright client:

    [[layers]]
      name = 'lsp'
      enabled_clients = ['pyright']
    

    If you want to use python-lsp-server, use following config:

    [[layers]]
      name = 'lsp'
      enabled_clients = ['pylsp']
    

Layer options

  • python_file_head: Default file head when create new python file.

    By default, when create a new python file, SpaceVim will insert file head automatically. to change the file head, use python_file_head option:

    toml
    [[layers]]
      name = "lang#python"
      python_file_head = [
          '#!/usr/bin/env python',
          '# -*- coding: utf-8 -*-',
          '',
          ''
      ]
    

    When the autocomplete layer is enabled, the symbol will be completed automatically. By default the type info is disabled, because it is too slow. To enable type info add the following to your configuration file:

    toml
    [[layers]]
      name = "lang#python"
      enable_typeinfo = true
    
  • format_on_save: Enable/disable file formatting when saving current python file. By default, it is disabled, to enable it:

    toml
    [[layers]]
        name = 'lang#python'
        format_on_save = true
    
  • python_interpreter: Set the python interpreter, by default, it is python3. The value of this option will be applied to g:neomake_python_python_exe and code runner.

    toml
    [[layers]]
        name = 'lang#python'
        python_interpreter = 'D:\scoop\shims\python.exe'
    
  • enabled_linters: Set the default linters for python language, by default it is ['python']. You can change it to ['python', 'pylint'].

    toml
    [[layers]]
      name = 'lang#python'
      enabled_linters = ['python', 'pylint']
    

Key bindings

Jump to definition

ModeKey BindingsDescription
normalg dJump to the definition position of the symbol under cursor

Code generation

ModeKey BindingDescription
normalSPC l g dGenerate docstring

Code Coverage

ModeKey BindingDescription
normalSPC l c rcoverage report
normalSPC l c scoverage show
normalSPC l c ecoverage session
normalSPC l c fcoverage refresh

Text objects and motions

This layer contains vim-pythonsense which provides text objects and motions for Python classes, methods, functions, and doc strings.

Text ObjectsDescriptions
acOuter class text object
icInner class text object
afInner function text object
ifInner function text object
adInner docstring text object
idInner docstring text object

Inferior REPL process

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.

Send code to inferior process commands:

Key BindingsDescriptions
SPC l s bsend buffer and keep code buffer focused
SPC l s lsend line and keep code buffer focused
SPC l s ssend selection text and keep code buffer focused

Running current script

To run a Python script, you can press SPC l r to run the current file without losing focus, and the result will be shown in a runner buffer.

Testing

Refactoring

Key BindingsDescriptions
SPC l i rremove unused imports with autoflake
SPC l i ssort imports with isort

LSP key Bindings

If the lsp layer is enabled for python, the following key bindings can be used:

key bindingDescription
g Djump to type definition
SPC l erename symbol
SPC l xshow references
SPC l sshow line diagnostics
SPC l dshow document
Kshow document
SPC l w llist workspace folder
SPC l w aadd workspace folder
SPC l w rremove workspace folder