Skip to content

Commit b8899cb

Browse files
authored
Merge #594 feat: Nvim can detect venv python via "pynvim-python" tool
2 parents dc030e6 + 7f97466 commit b8899cb

File tree

5 files changed

+179
-17
lines changed

5 files changed

+179
-17
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: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""Wrapper to expose the Python interpreter as `pynvim-python`.
2+
3+
`setup.py` declares an entry point for the `main()` function below. When
4+
`pynvim` is installed, an executable named `pynvim-python` will be generated
5+
that will invoke `main()` below; that function then simply chains to the
6+
underlying Python interpreter, passing along all command-line arguments.
7+
8+
The intent is to have `pynvim-python` be on the `PATH` such that an invocation
9+
such as:
10+
11+
pynvim-python -c 'import pynvim'
12+
13+
is equivalent to explicitly running the correct Python interpreter where
14+
`pynvim` is installed:
15+
16+
/path/to/python -c 'import pynvim'
17+
18+
This allows Neovim to automatically detect the correct Python interpreter for
19+
use with `pynvim`.
20+
"""
21+
22+
import subprocess
23+
import sys
24+
25+
26+
def main() -> None:
27+
"""Chain to Python interpreter, passing all command-line args."""
28+
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
)

test/test_window.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,11 @@ def test_vars(vim: Nvim) -> None:
6161
def test_options(vim: Nvim) -> None:
6262
vim.current.window.options['colorcolumn'] = '4,3'
6363
assert vim.current.window.options['colorcolumn'] == '4,3'
64+
old_global_statusline = vim.options['statusline']
6465
# global-local option
6566
vim.current.window.options['statusline'] = 'window-status'
6667
assert vim.current.window.options['statusline'] == 'window-status'
67-
assert vim.options['statusline'] == ''
68+
assert vim.options['statusline'] == old_global_statusline
6869

6970
with pytest.raises(KeyError) as excinfo:
7071
vim.current.window.options['doesnotexist']

0 commit comments

Comments
 (0)