Skip to main content
  1. Posts/

Set up Python Provider for Neovim

·252 words·2 mins·
Table of Contents

To make python plugins work in Neovim, we need to set up the python provider properly. Usually, this means setting up the correct path to Python executable and also installing the pynvim – Neovim client for Python.

With old version of python installed via homebrew on macOS, if you set up the PATH correctly (i.e., make sure which python3 returns /opt/homwbrew/bin/python3), you can easily install pynvim via pip:

pip3 install pynvim

Starting with python3.12, if you try to install pynvim directly without using virtual env in macOS, you will see error message like this:

error: externally-managed-environment
× This environment is externally managed
╰─> To install Python packages system-wide, try brew install
    xyz, where xyz is the package you are trying to
    install.

See here for more info. You can override this behavior with --break-system-packages but this is not advised.

The correct way to set up latest pynvim with neovim is to create a virtual environment:

uv venv

# Install latest pynvim
uv pip install --upgrade pynvim

If you use other tools, follow their guide to create a virtual env

# with python builtin venv
python -m venv .venv
source .venv/bin/activate
pip install pynvim

Then point the python executable to python inside the virtual env

vim.g.python3_host_prog = "path/to/python/in/the/virtual/env"

If you create a virtual env inside ~/.config/nvim, use the following config:

local config_path = vim.fn.stdpath("config")
local python3_path = vim.fs.joinpath(config_path, ".venv/bin/python3")
vim.g.python3_host_prog = python3_path

With this setup, nvim will correctly load the pynvim module, even when the virtual env is not activated.

References
#

Related