# 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 1. Open your Python project: ```bash nvim /path/to/project ``` 2. Select virtual environment: ``` cv Open venv selector ``` 3. The LSP will restart with the selected venv. ## Virtual Environment Management ### Selecting a Virtual Environment ``` cv Open venv selector ``` This searches for: - `.venv/` in project - `venv/` in project - Poetry environments - Conda environments - pyenv environments ### Creating a Virtual Environment If you don't have one: ```bash # In terminal or with ft (float term) python -m venv .venv source .venv/bin/activate pip install -r requirements.txt ``` Then `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 | |-----|--------| | `ca` | Code actions | | `cr` | Rename symbol | | `cf` | Format document | ### Diagnostics | Key | Action | |-----|--------| | `cd` | Line diagnostics | | `]d` / `[d` | Next/prev diagnostic | | `xx` | Toggle trouble | ## Testing with pytest ### Running Tests | Key | Action | |-----|--------| | `tt` | Run test at cursor | | `tf` | Run file tests | | `ta` | Run all tests | | `tl` | Re-run last test | ### Test Discovery pytest finds tests in: - `test_*.py` files - `*_test.py` files - Functions named `test_*` - Classes named `Test*` ### Test Output ``` ts Toggle test summary to Show test output tO Toggle output panel ]t / [t Jump to next/prev failed test ``` ### Debugging Tests ``` td Debug test at cursor ``` This starts debugpy and allows stepping through test code. ## Debugging ### Setup debugpy should be installed in your virtual environment: ```bash pip install debugpy ``` ### Setting Breakpoints | Key | Action | |-----|--------| | `db` | Toggle breakpoint | | `dB` | Conditional breakpoint | ### Running Debugger | Key | Action | |-----|--------| | `dc` | Continue/Start | | `di` | Step into | | `do` | Step over | | `dO` | Step out | | `dt` | Terminate | ### DAP UI ``` du Toggle DAP UI de Evaluate expression ``` ### launch.json For custom configurations, create `.vscode/launch.json`: ```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 ``` cf Format with black/ruff ``` ### Import Sorting If using isort or ruff: ``` ca Code actions → Organize imports ``` ### Type Checking Pyright provides type checking. For strict mode, add `pyrightconfig.json`: ```json { "typeCheckingMode": "strict" } ``` Or in `pyproject.toml`: ```toml [tool.pyright] typeCheckingMode = "strict" ``` ## AI Assistance | Key | Action | |-----|--------| | `ka` | Ask opencode | | `ke` | Explain code | | `kR` | Refactor selection | | `kt` | Generate tests | | `kd` | Generate docstrings | ### Generate Docstrings 1. Place cursor on function 2. `kd` (or use neogen: `cn`) 3. Review and adjust ## Common Workflows ### Starting New Feature 1. `cv` - Ensure correct venv 2. Create/open file 3. `H` - Mark with harpoon 4. Write code with LSP assistance 5. `tt` - Test as you go ### Debugging an Issue 1. `xx` - View all diagnostics 2. Navigate to issue 3. `db` - Set breakpoint 4. `dc` - Start debugger 5. Step through code ### Adding Tests 1. Create `test_*.py` file 2. Write test function: ```python def test_something(): assert my_function() == expected ``` 3. `tt` - Run test 4. `to` - View output ## Project Configuration ### pyproject.toml Modern Python projects use `pyproject.toml`: ```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: ```ini [pytest] testpaths = tests python_files = test_*.py addopts = -v --tb=short ``` ## Tips ### Quick REPL ``` ft Open floating terminal python Start Python REPL ``` ### Run Current File ``` :!python % Run current file ``` Or use executor: ``` Brs Set command: python % Brr Run it ``` ### Documentation - `K` on any symbol shows docstring - Works with standard library and packages ### Find TODOs ``` xt Show all TODO/FIXME comments ``` ## Troubleshooting ### LSP Not Finding Imports 1. Check venv is selected: `cv` 2. `:LspInfo` - Verify pyright is attached 3. Check `pyrightconfig.json` or `pyproject.toml` ### Wrong Python Version 1. `cv` - Select correct venv 2. Or set explicitly in `pyrightconfig.json`: ```json { "pythonVersion": "3.11" } ``` ### Tests Not Found 1. Check file naming: `test_*.py` 2. Check function naming: `test_*` 3. Verify pytest is installed in venv ### Debugger Not Starting 1. Install debugpy: `pip install debugpy` 2. Check `:DapShowLog` for errors 3. Verify venv is active