Skip to content

Auto-detect pynvim located in dedicated virtual environmentΒ #593

@drmikehenry

Description

@drmikehenry

I'd like Neovim to automatically detect pynvim even when it has been installed in its own virtual environment. Finding the python interpreter executable associated with pynvim can be problematic when supporting a shared Vim configuration for multiple users on multiple platforms, as user preferences and operating system variations lead to a variety of different locations for the virtual environment.

The standard method for finding executables that have been installed in arbitrary locations is the PATH environment variable. When the executable name is unique, it may be anywhere on PATH, and the executable's actual location need not be configured.

To make it possible to detect the pynvim virtual environment's python interpreter, I've been using the below Python shim and exposing it on PATH as the executable named pynvim-python-interpreter:

import subprocess
import sys


def main() -> None:
    subprocess.run([sys.executable] + sys.argv[1:])

pynvim-python-interpreter chains to the Python interpreter associated with the virtual environment where it was installed. pynvim is a dependency that is also installed in that environment. The only Neovim configuration needed is to set g:python3_host_prog to pynvim-python-interpreter.

See https://github.com/drmikehenry/pynvim-python-interpreter for details on the implementation (published on PyPI as well). This makes installation straightforward: uv tool install pynvim-python-interpreter

If a similar shim were shipped as part of pynvim itself, then pynvim could be installed using uv tool install pynvim. I've saved the shorter name pynvim-python as a suggestion for pynvim to use in lieu of pynvim-python-interpreter.

Neovim could automatically detect this interpreter and set g:python3_host_prog via something like:

if !exists('g:python3_host_prog') && executable('pynvim-python')
    let g:python3_host_prog = 'pynvim-python'
endif

With those changes, users could uv tool install pynvim to place pynvim in a dedicated virtual environment and require no additional Neovim configuration.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions