Skip to content

Commit d72acec

Browse files
committed
feat: Improve Neovim detection of pynvim
Problem: Detection of the pynvim module is currently done by finding the first Python interpreter in the `PATH` and checking if it can import pynvim. This has several effects: - Activation of an unrelated Python virtual environment will break automatic detection, unless pynvim is also installed in that environment. - Installing pynvim to the expected location is difficult. User installation into the system-wide or user-wide Python site area is now deprecated. On Ubuntu 24.04 with Python 3.12, for example, the command `pip install --user pynvim` now fails with the error message `error: externally-managed-environment`. - Users may create a dedicated virtual environment in which to install pynvim, but Neovim won't detect it; instead, they must either activate it before launching Neovim (which interferes with the user of other virtual environments) or else hard-code the variable `g:python3_host_prog` in their `init.vim` to the path of the correct Python interpreter. Neither option is desirable. Solution: Expose pynvim's Python interpreter on the `PATH` under the name `pynvim-python`. In the typical flow: - User installs either uv or pipx. - User installs pynvim via: uv tool install --upgrade pynvim # Or: pipx install --upgrade pynvim With corresponding changes in Neovim, the above is all that's needed for Neovim to detect the installed location of pynvim, even if an unrelated Python virtual environments is activated. It uses standard Python tooling to automate the necessary creation of a Python virtual environment for pyenv and the publication of `pynvim-python` to a directory on `PATH`. See #593 for additional discussion of this idea.
1 parent dc030e6 commit d72acec

File tree

4 files changed

+155
-16
lines changed

4 files changed

+155
-16
lines changed

README.md

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,35 @@ Install
1212

1313
Supports python 3.7 or later.
1414

15-
pip3 install pynvim
15+
- Installation option #1: install using uv (recommended):
1616

17-
You can install the package without being root by adding the `--user` flag.
18-
Anytime you upgrade Neovim, make sure to upgrade pynvim as well:
17+
- Install uv (https://docs.astral.sh/uv/).
1918

20-
pip3 install --upgrade pynvim
19+
- Install pynvim (the `--upgrade` switch ensures installation of the latest
20+
version):
2121

22-
Alternatively, you can install the development version by cloning this
23-
repository and executing the following at the top level:
22+
uv tool install --upgrade pynvim
2423

25-
pip3 install .
24+
- Anytime you upgrade Neovim, make sure to upgrade pynvim as well by
25+
re-running the above command.
26+
27+
- Installation option #2: install using pipx:
28+
29+
- Install pipx (https://pipx.pypa.io/stable/).
30+
31+
- Install pynvim (the `--upgrade` switch ensures installation of the latest
32+
version):
33+
34+
pipx install --upgrade pynvim
35+
36+
- Anytime you upgrade Neovim, make sure to upgrade pynvim as well by
37+
re-running the above command.
38+
39+
- Other installation options:
40+
41+
- See [pynvim installation
42+
documentation](https://pynvim.readthedocs.io/en/latest/installation.html)
43+
for additional installation options and information.
2644

2745
Python Plugin API
2846
-----------------

docs/installation.rst

Lines changed: 119 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,63 @@ Installation
33

44
The Neovim Python client supports Python 3.7 or later.
55

6-
Using pip
7-
---------
6+
Using uv or pipx
7+
----------------
88

9-
You can install the package without being root by adding the ``--user`` flag::
9+
For automatic detection by Neovim, pynvim should be installed in a dedicated
10+
Python virtual environment and the ``pynvim-python`` executable should be placed
11+
on the ``PATH``. The recommended approach for this is to use a tool like uv
12+
(https://docs.astral.sh/uv/) or pipx (https://pipx.pypa.io/stable/); the
13+
``--upgrade`` switch ensures installation of the latest version:
1014

11-
pip3 install --user pynvim
15+
- Install using uv (recommended)::
1216

13-
If you follow Neovim HEAD, make sure to upgrade ``pynvim`` when you upgrade
14-
Neovim::
17+
uv tool install --upgrade pynvim
1518

16-
pip3 install --upgrade pynvim
19+
- Install using pipx::
20+
21+
pipx install --upgrade pynvim
22+
23+
**NOTE** For Neovim before v0.12.0, set the variable ``python3_host_prog`` in
24+
``init.vim`` to point to ``pynvim-python``::
25+
26+
let g:python3_host_prog = 'pynvim-python'
27+
28+
Using manually created Python virtual environment
29+
-------------------------------------------------
30+
31+
Alternatively, you may manually create a Python virtual environment
32+
(https://docs.python.org/3.13/library/venv.html)::
33+
34+
python3 -m venv pynvim-venv
35+
36+
Then install pynvim into the virtual environment; the
37+
``--upgrade`` switch ensures installation of the latest version::
38+
39+
- For Unix::
40+
41+
pynvim-venv/bin/python -m pip install --upgrade pynvim
42+
43+
- For Windows::
44+
45+
pynvim-venv\Scripts\python -m pip install --upgrade pynvim
46+
47+
Then copy the ``pynvim-python`` executable somewhere on the ``PATH``:
48+
49+
- For Unix::
50+
51+
# Assuming `~/.local/bin` is on `PATH`:
52+
cp pynvim-venv/bin/pynvim-python ~/.local/bin/pynvim-python
53+
54+
- For Windows::
55+
56+
REM Assuming `C:\apps` is on `PATH`:
57+
copy pynvim-venv\Scripts\pynvim-python.exe C:\apps\pynvim-python.exe
58+
59+
**NOTE** For Neovim before v0.12.0, set the variable ``python3_host_prog`` in
60+
``init.vim`` to point to ``pynvim-python``::
61+
62+
let g:python3_host_prog = 'pynvim-python'
1763

1864
Install from source
1965
-------------------
@@ -23,6 +69,70 @@ Clone the repository somewhere on your disk and enter to the repository::
2369
git clone https://github.com/neovim/pynvim.git
2470
cd pynvim
2571

26-
Now you can install it on your system::
72+
Now you can install it following the instructions above, using ``.`` instead of
73+
``pynvim``; the ``--upgrade`` switch ensures installation of the latest version:
74+
75+
- Install from source using uv::
76+
77+
uv tool install --upgrade .
78+
79+
- Install from source using pipx::
80+
81+
pipx install --upgrade .
82+
83+
- Install from source using manually created Python virtual environment:
84+
85+
- Create ``pynvim-venv`` as above.
86+
87+
- Install:
88+
89+
- For Unix::
90+
91+
pynvim-venv/bin/python -m pip install --upgrade .
92+
93+
- For Windows::
94+
95+
pynvim-venv\Scripts\python -m pip install --upgrade .
96+
97+
- Copy ``pynvim-python`` executable as above.
98+
99+
**NOTE** For Neovim before v0.12.0, set the variable ``python3_host_prog`` in
100+
``init.vim`` to point to ``pynvim-python``::
101+
102+
let g:python3_host_prog = 'pynvim-python'
103+
104+
Upgrade pynvim when upgrading Neovim
105+
------------------------------------
106+
107+
Make sure to upgrade ``pynvim`` when you upgrade Neovim. Follow the previous
108+
instructions; the ``--upgrade`` switch will ensure installation of the latest
109+
version.
110+
111+
Explicitly choosing pynvim virtual environment
112+
----------------------------------------------
113+
114+
As an alternative to exposing ``pynvim-python`` on ``PATH``, you may configure
115+
Neovim to use a specific Python interpreter that has pynvim installed; this may
116+
be useful when working on pynvim itself.
117+
118+
After installing into a virtual environment named ``pynvim-venv``, add the
119+
following into Neovim's ``init.vim`` file:
120+
121+
- For Unix::
122+
123+
let g:python3_host_prog = '/path/to/pynvim-venv/bin/python'
124+
125+
- For Windows::
126+
127+
let g:python3_host_prog = 'c:\path\to\pynvim-venv\bin\python.exe'
128+
129+
Installing outside of a virtual environment is deprecated
130+
---------------------------------------------------------
131+
132+
Installing into the per-user Python site package area is a deprecated practice
133+
with recent Python versions. For example, the following command fails on Ubuntu
134+
24.04 with the error message ``error: externally-managed-environment``::
135+
136+
pip install --user pynvim
27137

28-
pip3 install .
138+
Instead, always install into a virtual environment.

pynvim/python.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import subprocess
2+
import sys
3+
4+
5+
def main() -> None:
6+
subprocess.run([sys.executable] + sys.argv[1:])

setup.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,9 @@
5858
setup_requires=setup_requires,
5959
tests_require=tests_require,
6060
extras_require=extras_require,
61+
entry_points={
62+
'console_scripts': [
63+
'pynvim-python=pynvim.python:main',
64+
],
65+
},
6166
)

0 commit comments

Comments
 (0)