Skip to content

neovim Python

lamt edited this page Jun 13, 2022 · 4 revisions

neovim Python

Settings

Category Name
Language Server pyright
Debugger debugpy
Linter null-ls(flake8)
Formatter null-ls(black)
Formatter null-ls(isort)

Coding

mkdir hello-python
cd hello-python
# Optional
# You can keep the version of pyenv.
pyenv install <your version>
pyenv local <your version>
python -m pip install --upgrade pip
pip install debugpy
vi main.python
print("Hello World.")
# Optional
# Run
python main.py

Debugging

This sample use nvim-dap-python.

vi main.py
  1. F9: Set Breakpoint
  2. F5: Start Debug
  3. Select Launch file
  4. Stop at Breakpoint
  5. F10: Step over
  6. Exit Debug

Linting

vi main.py
import os

print("Hello World.")

Show the trouble window (leader x x).

# Optional
# Also runnable on the command line
flake8

Linter can change settings (e.g. add tox.ini).

See: flake8 -- Configuration Locations.

See also: flake8-black -- Configuration.

Formatting

vi main.py
import sys
import os

print(
    "Hello World."
)

:w or space f.

# Optional
# Also runnable on the command line
black .
isort .

Formatter can change settings (e.g. add pyproject.toml).

black takes the following stance.

Pro-tip: If you're asking yourself "Do I need to configure anything?" the answer is "No". Black is all about sensible defaults. Applying those defaults will have your code in compliance with many other Black formatted projects.

See: black -- Configuration.

See: isort -- Config Files.

See also: isort -- Black Compatibility.

Clone this wiki locally