The perfect Neovim setup for Python
https://www.youtube.com/watch?v=4BnVeOUeZxc
无作者
Intro
in, this video we’re going to set up neovim to be the perfect editor, for working with python by, the end of this guide you’ll, have Auto suggestions and code completion enabled encode, static analysis Auto formatting on Save and Powerful debugging capabilities to, get started you’ll need to make.
Getting Started
sure you have the latest version, of Neo then installed at, the time of recording this is version, 0.9.1, download, and install that as per the instructions for your operating system for, this video I’m using Mac OS so I’ll install near them using Brew with, neovim, installed the next step is to add a base configuration, to simplify our setup process, my preferred option is nvchad, which provides some powerful, plugins and defaults while still, allowing a great deal of flexibility, for customization, if you have an existing neovim, configuration you’ll first want to back this up and delete your local neovim, cache using the following commands, you can then install nvchat, by using the following git clone, command which will download it into your neovim configuration, directory once, that’s done open up neovim, and you should be greeted with, a prompt asking if you want to, install an example configuration, make, sure to type in n as in no so you start from the same base configuration, as I am after, that you should see the base.
packages and plugins being installed, through the lazy plugin manager, once that’s done feel free to set your theme to one you prefer you, can do so by opening up the theme switcher using space T and H I’ll, be setting my theme to the best theme ever Capuchin, with that done we’re ready to start customizing the event for python.
Code Suggestions & Autocomplete
the first feature we want is to enable code suggestions and autocomplete, we can do this by setting up a language server protocol or LSP server, there’s a number of LSP servers, we can use with python but, we’re going to go with Pyrite for this configuration to, do so we’re going to first need to get pyrite installed, on our system we can do this using either pip or npm or, from within neovim using a plugin called Mason the, Mason plugin acts as a package manager for external dependencies that neovim, uses, the benefit to doing it in Mason is it keeps our configuration as code which, is preferable when compared to installing dependencies ad hoc to, install using Mason first, open up your neovim configuration, files in near them by changing it over to the config directory, and typing nvim inside, once neovin is loaded you can, then open up the tree view by, pressing Ctrl and N next, we’re going to need to create, a new file which will keep track, of our neovin plugins.
we’ll create this in the lower custom directory of our configuration, once we’ve navigated here we, can create the file by pressing, the a key and entering a name, for the file in the prompt let’s, call this plugins.lure, once that’s created you can open it up by pressing enter on the file this, file is where we’ll Define any, plugins that we want to add to, near them or to extend any config, from the plugins that nvchat, installs as, Mason is installed as part of, MV chat we’ll be overriding the, options here to install pyrite, in, this file let’s define an empty lure table called plugins and, then add a line underneath to return it, next open up this table and add an entry for Mason, inside, of this entry We’ll add a block, for our options which will Define, our ensure installed, table, the contents of this table defines, the dependencies that Mason should manage for us, let’s add pyrite to this list, next we want to make sure we import this file whenever we load near them to, do this head over to the chatrc.lure.
file in the custom directory if you don’t have this file be sure to create it, inside of this file is where we’re going to load our custom plugins to, do so add in the following line which will tell nvchad, where our custom plugin file lives, the file saved go ahead and restart neovim to reload our configuration, once, you’re back inside of neovim, call the Mason install all command, to download and install our new, dependency, after Mason is finished we can go ahead and configure pyrite, heading, back over to our custom plugins, file let’s add a new entry to our custom plugins table, we’re going to override another plugin from nvchat called neovim nvim, LSP config, this plugin is created by near them and provides some default configurations, for use with LSP servers, inside, of this entry add a function, for the config and add the following, lines to both import the default, and V chat LSP config, and also a new custom file we’re about to create next, we’re going to create a new file in our custom directory under configs LSP.
config.lure, you, can do this in neovim by navigating to the tree view pressing, the a key and typing out the full file path, once you’ve created the file open it up in your editor this, file is where we’re going to store the configuration for our LSP servers, first we need to import the existing nvchat LSP configuration, in order to obtain the on attach and capabilities functions, these are used to define our, editor’s capabilities and to, perform a callback for when our, LSP server is attached, next we import the LSP config plugin which, is what we’ll use for setting up pyrite, to set up pyrite we call the LSP, config.pyrite.setup function and pass in our options, in these options we’ll set the unattach and capabilities, values to the functions that, we imported earlier and we’ll, set the file type that we want, the LSP server to load on to, be python this should be all, we need for a default setup with, Pyrite let’s, go and check it out with some python, code here, I have a python function to generate prime numbers.
as I start typing we can see pyrite provides some code suggestions, for use with our list as, well as this pyrite will also provide some inline messages whenever, we have errors in our code, this suggests that our LSP config, is working as expected which, we can validate using the LSP, info, command here, we can see that pyrite is attached to our current python buffer, nice.
Static Analysis
the, next feature we’re going to add, is static analysis and linting, for our python code now, we could just change from using pyrite to using python LSP server, which has a number of static analyzers built in however, I believe it’s more educational and, extensible to show how to do this using the null LS plugin instead, null, LS is a neovim plugin that enables non-lsp tooling, such as formatters and linters to have an LSP interface, null, LS is a really powerful plugin for language tooling for, our python configuration we’re going to use two tools with null LS mipi, for static type checking and rough for faster python linting there, are a number of other python, tools that work with the null, LS so be sure to check out the, built-ins documentation, on GitHub to see if there’s any others you’d like to use the, link is in the description, to get started let’s add an entry for null LS in our custom plugins.lure, file we’ll set the file type, to be Python and add in a block, for our custom options as, this configuration can get a.
little heavy We’ll add our options, into another file so, let’s require our, custom.configs.null LS file and return it here, now we can create this new file in our custom configs, directory called null ls.lua, once, that’s done open it up and inside, we’ll Define an empty table called, ops and return it, now we need to import the null LS package in order to use the built-in sources, after, that we’ll add the sources we want to our sources table in the options let’s, add an entry for both my pi and rough with the two following lines, now we just need to make sure both of these tools exist on our system let’s, go ahead and do this using Mason jumping, back over to the custom plugins, lure file we can add both my, pi and rough to our ensure installed, list in our Mason entry now, if we run the Mason install all, command we should see a window, open up with our new dependencies, being installed, if, this doesn’t work for you you, may need to restart any of them, once that’s done we can test, out our new linters over, in our primes python code we.
should be able to see any inline, messages for any code that violates, our linting rules we, can trigger this by adding an, unused variable which rough will, detect and throw a warning for, we can also trigger an error, from my Pi by passing an invalid, type as the argument to our primes, function, very cool.
Autoformatting
the next feature to add is auto formatting, which will reorganize and format our code whenever we save, we can add Auto formatting, to near them using null LS and the black Auto formatting, tool for python which formats code to the pep 8 standard if, you prefer a different style for auto formatting, fortunately, there are other options provided by another LS, be, sure to check in the built-in sources documentation, to see which one you want to use, to get started with adding in, our Auto formatter we first need, to install black on our system, this, is probably pretty familiar by, now but we can do so by adding, it to the Mason insure, installed, list, once, added we can then call the Mason install or command to download and install it, now we can add black to our sources list in our custom null LS config file this, should be all we need for a basic, setup if we go back to our python, primes code we can test this, out nvchat, provides a simple mnemonic, keybinding for running any attached Auto formatters manually by.
typing space F and M if we run, this we’ll see our code is formatted, according to the pep 8 Style, this, is nice and dull but as humans, are terrible at remembering things, it’s preferable to set this to, automatically, format on Save, we can set this up pretty easily and neovim, to do so head back over to the null LS config file here, we want to define a new Auto Group for our Auto formatting Auto command then, we’ll add an unattach, method to our options which will, make use of this order group, and Define a pre-save hook for, our buffers which, will run the Vim LSP format, command, now, let’s jump on back over to our code and add in some bad formatting, to test this out when, we go ahead and save using colon w we should see the file automatically, format nice.
Debugging
the last feature we want to add, is a debugger so that we’re able, to test run and debug python, code from within neovim, to achieve this we’re going to use a plugin called nvim, dap which stands for debug adapter protocol this, plugin allows us to connect neovim to a number of different external, debugging tools as, we’re setting up neovin for python we’ll also need to add another plugin, called nvim, dap python, which, provides an extension to DAP in order to communicate with the debug Pi debugger, let’s, add both of these by heading back to our custom plugins, file and adding in an entry for both nvimdap, and nvimdap python, in the nvm.python, entry let’s specify the file type to load this plugin for as Python and, also add a table for our plugins dependencies, with an entry of NVM, dap this means that whenever, our and then dap python plugin, is loaded then end in Dap will, be loaded as well, next add in a config function to the nvim.python, entry and, add in the following line which, points to the virtual environment.
path of our debug Pi instance, then, call the DAP python setup function with this path, finally we just need to make sure we have debug Pi installed on our system we, can do this by adding an entry to Mason in the insure installed list, after doing so make sure to call the Mason install all command, with that we have a basic debugger setup that we can use but, I think it’s worth adding in, another plugin to level up the, debugging experience in neovim, that plugin is called envym dap UI, which provides visual elements to make inspecting, values and working with debug commands and absolute Joy, let’s go ahead and add this to our plugins table and, set up nvin dap as a dependency, next we’ll want to add a configuration, function which will set up the, UI to automatically, load in response to debugging events, make sure to copy the following lines as I have you, can also find a link to my configuration, in the description if copying, is too much, All That Remains is to add in the nvimdap UI as a dependency of our nvimdap.
python plugin so that the UI is also loaded whenever we load a python file, with that we have all of the plugins we need but, there is one final thing we should do, to make working with the debugger, easier you can add some custom, mappings to nvchat, in order to set breakpoints, and to run debug pi, to do this first create a new file called mappings.lure in the custom directory, inside of this file to find an empty lure table called M and return it, now add a new field to this table called dap which, we’ll use to store any custom mappings associated with the mvimdap plugin, in this table we’ll set the value of plugin to be true which, means these mappings will only exist when we explicitly, load them, next add the following lines, to specify that we want this, mapping to work only on normal, mode as, defined by the end field then, we can add our mapping which is defined by the leader or space key followed, by D and B you, can remember this mnemonically as debug breakpoint.
this mapping will call the DAP toggle breakpoint command for us, the next mapping we want to add, is for running the nearest test, case in our python code, to do this we need to add another entry, to our end table called dap underscore python, we’ll also set the plugin value of this entry to be true and, Define a mapping for normal mode this, mapping will be for space DPR, which stands for debug python run, this mapping will load the NVM dap python package and call the test method, function, with, our mappings defined the last thing we need to do is go ahead and load them jump, on over to the Chad rc.lure, file and import the mappings file and assign them to the end table, next head back over to our plugins.lure, file and add a config function block to our mvimdap, entry which we will use to load the dab mappings, we’ll do the same thing again, in our MVM dap python config, block and add a line in to load, the DAP python mappings, with that done we should be complete finally let’s go ahead and test this out, back.
over in our Prime code let’s, go ahead and install the Python language for tree sitter this, is required by MVM dap to better navigate and understand the syntax of our code, once that’s done we’ll need to, add in some tests in order to, run our debugger I’m, going to generate these tests using AI with a secret command if, you’re interested in knowing how I did this don’t, worry I’ll be showcasing in another video, with, our tests generated and moved, to the correct file let’s go, ahead and use our new mapping, of space D, and B to set a breakpoint which, you can see is set by the B next to the line number then, we can use the mapping of space d p and R to run our tests, as you can see once our breakpoint, is hit a number of panels are, opened up which is part of MVM, dap UI, we can use these panels for inspecting our local variables, or for controlling our debugger by stepping over lines of code continuing, until the next breakpoint or just stopping execution, with that we have turned neovim from a basic text editor.
into a powerful and configurable IDE for, working with python, if you have any other languages, you’d like to see me do in this, series then please let me know, in the comments down below, I hope you enjoyed this video and I’ll see you on the next one.