mfussenegger / nvim-dap-python

An extension for nvim-dap, providing default configurations for python and methods to debug individual test methods or classes.
GNU General Public License v3.0
571 stars 51 forks source link

Are we supposed to install debugpy in the virtual environment? #79

Closed niderhoff closed 1 year ago

niderhoff commented 1 year ago

I have a question regarding how to setup this properly. I am assuming I don't understand the author's intention, so I am asking here how it is supposed to be setup.

The doc says this

It is recommended to install debugpy into a dedicated virtualenv. To do so:

mkdir .virtualenvs cd .virtualenvs python -m venv debugpy debugpy/bin/python -m pip install debugpy

The debugger will automatically pick-up another virtual environment if it is activated before neovim is started.

Later it states that

Call setup in your init.vim to register the adapter and configurations:

lua require('dap-python').setup('~/.virtualenvs/debugpy/bin/python')

If I set it up this way it is behaving quite strangely. I am assuming this last line will always run debugpy from the special "debugpy"-virtual environment using the python executable therein. This works, if no virtualenv is activated.

However, if a virtual environment in the shell is activated, it will try to run the debugpy library from the path ~/.virtualenvs/debugpy/site-packages/... with the python executable from the currently activated virtual env (which might be a completely incompatible version).

So if it detects the virtual env from the VIRTUAL_ENV variable, I would assume another viable route is to install debugpy into my virtual env. But it wont let me setup dap-python without providing the path in .setup().

So how is it supposed to work with virtual environments?

mfussenegger commented 1 year ago

However, if a virtual environment in the shell is activated, it will try to run the debugpy library from the path ~/.virtualenvs/debugpy/site-packages/... with the python executable from the currently activated virtual env (which might be a completely incompatible version).

It assumes that you're using the same python version. The main idea behind this is that it avoids having to install debugpy in every virtualenv you create.

I would assume another viable route is to install debugpy into my virtual env. But it wont let me setup dap-python without providing the path in .setup().

If you install debugpy in every projects virtualenv you can use 'python3' or 'python' as path. It honors $PATH and virtualenv activation adds the virtual-env bin folder to the start of $PATH, so it will pick the right interpreter.