2018-10-02 21:04:30 +08:00
|
|
|
---
|
|
|
|
title: "Use Vim as a Python IDE"
|
|
|
|
categories: [tutorials, blog]
|
|
|
|
excerpt: "A general guide for using SpaceVim as Python IDE, including layer configuration, requiems installation and usage."
|
|
|
|
type: BlogPosting
|
|
|
|
comments: true
|
|
|
|
commentsID: "Use Vim as a Python IDE"
|
|
|
|
---
|
|
|
|
|
|
|
|
# [Blogs](../blog/) >> Use Vim as a Python IDE
|
|
|
|
|
2019-02-03 22:27:06 +08:00
|
|
|
This is a general guide for using SpaceVim as a python IDE, including layer configuration and usage.
|
2018-10-02 21:04:30 +08:00
|
|
|
Each of the following sections will be covered:
|
|
|
|
|
|
|
|
<!-- vim-markdown-toc GFM -->
|
|
|
|
|
|
|
|
- [Enable language layer](#enable-language-layer)
|
|
|
|
- [Code completion](#code-completion)
|
2019-01-20 20:40:58 +08:00
|
|
|
- [Syntax linting](#syntax-linting)
|
2018-10-02 21:04:30 +08:00
|
|
|
- [Import packages](#import-packages)
|
|
|
|
- [Jump to test file](#jump-to-test-file)
|
|
|
|
- [running code](#running-code)
|
|
|
|
- [Code formatting](#code-formatting)
|
|
|
|
- [REPL](#repl)
|
|
|
|
|
|
|
|
<!-- vim-markdown-toc -->
|
|
|
|
|
|
|
|
### Enable language layer
|
|
|
|
|
2018-12-18 23:14:37 +08:00
|
|
|
To add python language support in SpaceVim, you need to enable the `lang#python` layer. Press `SPC f v d` to open
|
2018-10-02 21:04:30 +08:00
|
|
|
SpaceVim configuration file, and add following configuration:
|
|
|
|
|
|
|
|
```toml
|
|
|
|
[[layers]]
|
|
|
|
name = "lang#python"
|
|
|
|
```
|
|
|
|
|
|
|
|
for more info, you can read the [lang#python](../layers/lang/python/) layer documentation.
|
|
|
|
|
|
|
|
### Code completion
|
|
|
|
|
2018-12-18 23:14:37 +08:00
|
|
|
`lang#python` layer will load the jedi plugin automatically, unless overriden in your `init.toml`.
|
2018-10-02 21:04:30 +08:00
|
|
|
The completion menu will be opened as you type.
|
|
|
|
|
|
|
|
![complete python code](https://user-images.githubusercontent.com/13142418/46339650-f5a49280-c665-11e8-86d4-20944ec23098.png)
|
|
|
|
|
2018-12-18 23:14:37 +08:00
|
|
|
### Syntax linting
|
2018-10-02 21:04:30 +08:00
|
|
|
|
2019-02-03 22:27:06 +08:00
|
|
|
The checkers layer is enabled by default. This layer provides asynchronous syntax linting via [neomake](https://github.com/neomake/neomake).
|
|
|
|
It will run flake asynchronously.
|
|
|
|
|
|
|
|
To install flake8, just run following command in terminal.
|
|
|
|
|
|
|
|
```sh
|
|
|
|
pip install --user flake8
|
|
|
|
```
|
2018-10-02 21:04:30 +08:00
|
|
|
|
|
|
|
### Import packages
|
|
|
|
|
|
|
|
When edit python file, you can import the package automatically, remove unused package and format package list.
|
|
|
|
|
|
|
|
```sh
|
|
|
|
pip install --user isort
|
|
|
|
```
|
|
|
|
|
|
|
|
### Jump to test file
|
|
|
|
|
2019-02-03 22:27:06 +08:00
|
|
|
SpaceVim use built-in plugin to manager the files in a project,
|
|
|
|
you can add a `.project_alt.json` to the root of your project with following content:
|
2018-10-02 21:04:30 +08:00
|
|
|
|
|
|
|
```json
|
|
|
|
{
|
2019-02-03 22:27:06 +08:00
|
|
|
"src/*.py": {"alternate": "test/{}.py"},
|
|
|
|
"test/*.py": {"alternate": "src/{}.py"}
|
2018-10-02 21:04:30 +08:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2019-02-03 22:27:06 +08:00
|
|
|
with this configuration, you can jump between the source code and test file via command `:A`.
|
2018-10-02 21:04:30 +08:00
|
|
|
|
|
|
|
### running code
|
|
|
|
|
|
|
|
To run current script, you can press `SPC l r`, and a split windows
|
|
|
|
will be openen, the output of the script will be shown in this windows.
|
|
|
|
It is running asynchronously, and will not block your vim.
|
|
|
|
|
|
|
|
![code runner](https://user-images.githubusercontent.com/13142418/46293837-1c5fbc00-c5c7-11e8-9f3c-c11504e2e04a.png)
|
|
|
|
|
|
|
|
### Code formatting
|
|
|
|
|
2019-02-03 22:27:06 +08:00
|
|
|
The format layer is also enabled by default, with this layer you can use key binding `SPC b f` to format current buffer.
|
|
|
|
Before using this feature, please install yapf.
|
2018-10-02 21:04:30 +08:00
|
|
|
|
2019-02-03 22:27:06 +08:00
|
|
|
```sh
|
|
|
|
pip install --user yapf
|
|
|
|
```
|
2018-10-02 21:04:30 +08:00
|
|
|
|
|
|
|
### REPL
|
|
|
|
|
2019-02-03 22:27:06 +08:00
|
|
|
Start a `ipython` or `python` inferior REPL process with `SPC l s i`. After the REPL process has been started. you can
|
|
|
|
send code to inferior process, all key bindings are begin with `SPC l s` prefix, including sending line, sending selection or even
|
|
|
|
send whole buffer.
|
2018-10-02 21:04:30 +08:00
|
|
|
|
2019-02-03 22:27:06 +08:00
|
|
|
![pythonrepl](https://user-images.githubusercontent.com/13142418/52177776-0fffa000-2801-11e9-9698-8e32f2865f5a.gif)
|