2018-10-02 21:04:30 +08:00
|
|
|
---
|
|
|
|
title: "Use Vim as a Python IDE"
|
|
|
|
categories: [tutorials, blog]
|
2020-05-10 14:05:04 +08:00
|
|
|
description: "A general guide for using SpaceVim as Python IDE, including layer configuration, requiems installation and usage."
|
2020-05-17 14:08:03 +08:00
|
|
|
type: article
|
2018-10-02 21:04:30 +08:00
|
|
|
comments: true
|
|
|
|
commentsID: "Use Vim as a Python IDE"
|
|
|
|
---
|
|
|
|
|
|
|
|
# [Blogs](../blog/) >> Use Vim as a Python IDE
|
|
|
|
|
2020-11-09 22:00:16 +08:00
|
|
|
This tutorial introduces you to SpaceVim as a Python environment,
|
|
|
|
by using the `lang#python` layer, you make SpaceVim into a great lightweight Python IDE.
|
|
|
|
|
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)
|
2020-11-09 22:00:16 +08:00
|
|
|
- [Select a Python interpreter](#select-a-python-interpreter)
|
2018-10-02 21:04:30 +08:00
|
|
|
- [Code completion](#code-completion)
|
2019-01-20 20:40:58 +08:00
|
|
|
- [Syntax linting](#syntax-linting)
|
2018-10-02 21:04:30 +08:00
|
|
|
- [Code formatting](#code-formatting)
|
2020-11-09 22:00:16 +08:00
|
|
|
- [Import packages](#import-packages)
|
|
|
|
- [Jump between alternate files](#jump-between-alternate-files)
|
|
|
|
- [Code running](#code-running)
|
2018-10-02 21:04:30 +08:00
|
|
|
- [REPL](#repl)
|
|
|
|
|
|
|
|
<!-- vim-markdown-toc -->
|
|
|
|
|
2020-11-09 22:00:16 +08:00
|
|
|
This tutorial is not intended to teach you Python itself.
|
|
|
|
|
|
|
|
If you have any problems, feel free to join the [SpaceVim gitter chatting room](https://gitter.im/SpaceVim/SpaceVim) for general discussion.
|
|
|
|
|
|
|
|
|
2018-10-02 21:04:30 +08:00
|
|
|
### Enable language layer
|
|
|
|
|
2020-11-09 22:00:16 +08:00
|
|
|
The python language support in SpaceVim is provided by `lang#python` layer, and it is not enabled by default.
|
|
|
|
You need to enable it in SpaceVim configuration file. Press `SPC f v d` to open the SpaceVim configuration file,
|
|
|
|
and add following snippet to your configuration file.
|
2018-10-02 21:04:30 +08:00
|
|
|
|
|
|
|
```toml
|
|
|
|
[[layers]]
|
|
|
|
name = "lang#python"
|
|
|
|
```
|
|
|
|
|
2019-02-13 23:25:34 +08:00
|
|
|
For more info, you can read the [lang#python](../layers/lang/python/) layer documentation.
|
2018-10-02 21:04:30 +08:00
|
|
|
|
2020-11-09 22:00:16 +08:00
|
|
|
### Select a Python interpreter
|
|
|
|
|
|
|
|
Python is an interpreted language, and in order to run Python code and get semantic information,
|
|
|
|
you need to tell SpaceVim which interpreter to use. This can be set with `python_interpreter` layer
|
|
|
|
option. For example:
|
|
|
|
|
|
|
|
```toml
|
|
|
|
[[layers]]
|
|
|
|
name = 'lang#python'
|
|
|
|
python_interpreter = 'D:\scoop\shims\python.exe'
|
|
|
|
```
|
|
|
|
|
|
|
|
This option will be applied to neomake's python maker and python code runner.
|
|
|
|
|
2018-10-02 21:04:30 +08:00
|
|
|
### Code completion
|
|
|
|
|
2020-11-09 22:00:16 +08:00
|
|
|
Code autocompletion is provided by `autocomplete` layer, which is loaded by default.
|
|
|
|
The language completion source is included in `lang#python` layer.
|
|
|
|
This layer includes `deoplete-jedi` for neovim.
|
2018-10-02 21:04:30 +08:00
|
|
|
|
|
|
|
![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
|
|
|
|
2020-11-09 22:00:16 +08:00
|
|
|
Code Linting is provided by [`checkers`](../layers/checkers/) layer, which is also enabled by default.
|
|
|
|
There are two syntax linters enabled by default,
|
|
|
|
python and [pylint](https://pylint.org/), both of them run asynchronously.
|
2019-02-03 22:27:06 +08:00
|
|
|
|
2020-11-09 22:00:16 +08:00
|
|
|
To install pylint, just run following command in terminal.
|
2019-02-03 22:27:06 +08:00
|
|
|
|
|
|
|
```sh
|
2020-11-09 22:00:16 +08:00
|
|
|
pip install --user pylint
|
2019-02-03 22:27:06 +08:00
|
|
|
```
|
2018-10-02 21:04:30 +08:00
|
|
|
|
2020-11-09 22:00:16 +08:00
|
|
|
### Code formatting
|
|
|
|
|
|
|
|
Code formatting is provided by [`format`](../layers/format/) layer, this layer is also enabled by default.
|
|
|
|
And the key binding to format current buffer is `SPC b f`. The default formatter for python code is [yapf](https://github.com/google/yapf).
|
|
|
|
|
|
|
|
So, before using this feature, please install yapf.
|
|
|
|
|
|
|
|
```sh
|
|
|
|
pip install --user yapf
|
|
|
|
```
|
2018-10-02 21:04:30 +08:00
|
|
|
### Import packages
|
|
|
|
|
2019-02-13 23:25:34 +08:00
|
|
|
When edit Python file, you can import the package automatically, remove unused package and format package list.
|
2018-10-02 21:04:30 +08:00
|
|
|
|
|
|
|
```sh
|
|
|
|
pip install --user isort
|
|
|
|
```
|
|
|
|
|
2020-11-09 22:00:16 +08:00
|
|
|
### Jump between alternate files
|
|
|
|
|
|
|
|
The alternate files manager provides a command `:A`, with this command,
|
|
|
|
you can jump between alternate files within a project.
|
2018-10-02 21:04:30 +08:00
|
|
|
|
2020-11-09 22:00:16 +08:00
|
|
|
The alternate file structure can be definded in a `.project_alt.json`
|
|
|
|
file in the root of your project.
|
|
|
|
|
|
|
|
For example:
|
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
|
|
|
|
2020-11-09 22:00:16 +08:00
|
|
|
### Code running
|
2018-10-02 21:04:30 +08:00
|
|
|
|
2020-11-09 22:00:16 +08:00
|
|
|
Code running is provided by builtin code runner. To run current script,
|
|
|
|
you can press `SPC l r`, and a split window will open,
|
|
|
|
the output of the script will be shown in this window.
|
2019-02-13 23:25:34 +08:00
|
|
|
It is running asynchronously, and will not block your Vim.
|
2018-10-02 21:04:30 +08:00
|
|
|
|
|
|
|
![code runner](https://user-images.githubusercontent.com/13142418/46293837-1c5fbc00-c5c7-11e8-9f3c-c11504e2e04a.png)
|
|
|
|
|
|
|
|
|
|
|
|
### REPL
|
|
|
|
|
2019-02-13 23:25:34 +08:00
|
|
|
Start a `ipython` or `python` inferior REPL process with `SPC l s i`. After the REPL process being started, you can
|
|
|
|
send code to inferior process. All key bindings prefix with `SPC l s`, including sending line, sending selection or even
|
2019-02-03 22:27:06 +08:00
|
|
|
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)
|