6.2 KiB
6.2 KiB
Python Development Workflow
This guide covers Python development in Neovim with virtual environment support, testing, and debugging.
Overview
Your config includes:
- LSP: Pyright (via LazyVim python extra)
- Linting: Ruff or pylint
- Testing: pytest via neotest
- Debugging: debugpy via DAP
- Virtual Environments: venv-selector
Getting Started
Project Setup
-
Open your Python project:
nvim /path/to/project -
Select virtual environment:
<leader>cv Open venv selector -
The LSP will restart with the selected venv.
Virtual Environment Management
Selecting a Virtual Environment
<leader>cv Open venv selector
This searches for:
.venv/in projectvenv/in project- Poetry environments
- Conda environments
- pyenv environments
Creating a Virtual Environment
If you don't have one:
# In terminal or with <leader>ft (float term)
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
Then <leader>cv to select it.
LSP Features
Navigation
| Key | Action |
|---|---|
gd |
Go to definition |
gr |
Go to references |
gI |
Go to implementation |
K |
Hover documentation |
Code Actions
| Key | Action |
|---|---|
<leader>ca |
Code actions |
<leader>cr |
Rename symbol |
<leader>cf |
Format document |
Diagnostics
| Key | Action |
|---|---|
<leader>cd |
Line diagnostics |
]d / [d |
Next/prev diagnostic |
<leader>xx |
Toggle trouble |
Testing with pytest
Running Tests
| Key | Action |
|---|---|
<leader>tt |
Run test at cursor |
<leader>tf |
Run file tests |
<leader>ta |
Run all tests |
<leader>tl |
Re-run last test |
Test Discovery
pytest finds tests in:
test_*.pyfiles*_test.pyfiles- Functions named
test_* - Classes named
Test*
Test Output
<leader>ts Toggle test summary
<leader>to Show test output
<leader>tO Toggle output panel
]t / [t Jump to next/prev failed test
Debugging Tests
<leader>td Debug test at cursor
This starts debugpy and allows stepping through test code.
Debugging
Setup
debugpy should be installed in your virtual environment:
pip install debugpy
Setting Breakpoints
| Key | Action |
|---|---|
<leader>db |
Toggle breakpoint |
<leader>dB |
Conditional breakpoint |
Running Debugger
| Key | Action |
|---|---|
<leader>dc |
Continue/Start |
<leader>di |
Step into |
<leader>do |
Step over |
<leader>dO |
Step out |
<leader>dt |
Terminate |
DAP UI
<leader>du Toggle DAP UI
<leader>de Evaluate expression
launch.json
For custom configurations, create .vscode/launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
},
{
"name": "Python: Flask",
"type": "python",
"request": "launch",
"module": "flask",
"args": ["run", "--debug"],
"env": {
"FLASK_APP": "app.py"
}
},
{
"name": "Python: FastAPI",
"type": "python",
"request": "launch",
"module": "uvicorn",
"args": ["main:app", "--reload"]
}
]
}
Code Quality
Formatting
<leader>cf Format with black/ruff
Import Sorting
If using isort or ruff:
<leader>ca Code actions → Organize imports
Type Checking
Pyright provides type checking. For strict mode, add pyrightconfig.json:
{
"typeCheckingMode": "strict"
}
Or in pyproject.toml:
[tool.pyright]
typeCheckingMode = "strict"
AI Assistance
| Key | Action |
|---|---|
<leader>ka |
Ask opencode |
<leader>ke |
Explain code |
<leader>kR |
Refactor selection |
<leader>kt |
Generate tests |
<leader>kd |
Generate docstrings |
Generate Docstrings
- Place cursor on function
<leader>kd(or use neogen:<leader>cn)- Review and adjust
Common Workflows
Starting New Feature
<leader>cv- Ensure correct venv- Create/open file
<leader>H- Mark with harpoon- Write code with LSP assistance
<leader>tt- Test as you go
Debugging an Issue
<leader>xx- View all diagnostics- Navigate to issue
<leader>db- Set breakpoint<leader>dc- Start debugger- Step through code
Adding Tests
- Create
test_*.pyfile - Write test function:
def test_something(): assert my_function() == expected <leader>tt- Run test<leader>to- View output
Project Configuration
pyproject.toml
Modern Python projects use pyproject.toml:
[project]
name = "myproject"
version = "0.1.0"
requires-python = ">=3.11"
[tool.pytest.ini_options]
testpaths = ["tests"]
python_files = "test_*.py"
[tool.ruff]
line-length = 88
select = ["E", "F", "I"]
[tool.pyright]
venvPath = "."
venv = ".venv"
pytest.ini
For pytest configuration:
[pytest]
testpaths = tests
python_files = test_*.py
addopts = -v --tb=short
Tips
Quick REPL
<leader>ft Open floating terminal
python Start Python REPL
Run Current File
:!python % Run current file
Or use executor:
<leader>Brs Set command: python %
<leader>Brr Run it
Documentation
Kon any symbol shows docstring- Works with standard library and packages
Find TODOs
<leader>xt Show all TODO/FIXME comments
Troubleshooting
LSP Not Finding Imports
- Check venv is selected:
<leader>cv :LspInfo- Verify pyright is attached- Check
pyrightconfig.jsonorpyproject.toml
Wrong Python Version
<leader>cv- Select correct venv- Or set explicitly in
pyrightconfig.json:{ "pythonVersion": "3.11" }
Tests Not Found
- Check file naming:
test_*.py - Check function naming:
test_* - Verify pytest is installed in venv
Debugger Not Starting
- Install debugpy:
pip install debugpy - Check
:DapShowLogfor errors - Verify venv is active