linux-cultist / venv-selector.nvim

Allows selection of python virtual environment from within neovim
MIT License
388 stars 41 forks source link

Anyway to get venv-selector to look for local python venv #36

Closed PyDev19 closed 1 year ago

PyDev19 commented 1 year ago

If I have a different python venv in the same directory as the python project how can I show that on venv selector. This is my current config for the plugin:

require("venv-selector").setup({
    name = { "env", "venv", ".venv" },
})

But it doesn't recognize any local python venvs. For clarification I am using the builitin python command to create a python venv by: python -m venv env

linux-cultist commented 1 year ago

Hi!

I cant seem to reproduce the issue here. I created a new dir called test_venvsel and then inside that dir, I created a new python virtual environment with the same command you posted:

python -m venv env

And I see it fine, its the last one in the list in the screenshot.

image

Lets try to add debug output and see what you get. Change your code to:

 require("venv-selector").setup({
    name = { "env", "venv", ".venv" },
    enable_debug_output = true
})

Now when you restart neovim, you will start to get messages from VenvSelect. Go to your python project and try to find virtual environments. Then you can type :messages in neovim to see all the messages. Copy all the output that is prefixed with VenvSelect: and post it here. :)

PyDev19 commented 1 year ago

Thanks for the reply, here are the debug messages:

VenvSelect: Setting fd binary to 'fd' since it was found on system and requested by user instead of fd.
VenvSelect: Telescope path: /home/pydev19/Documents/Projects/Python/PyTorch_Learning
VenvSelect: Refresh telescope since there are no previous results.
VenvSelect: Removed telescope results.
VenvSelect: Finding parent venvs in: /home/pydev19/Documents/Projects
VenvSelect: Found venv in parent search: /home/pydev19/Documents/Projects/Python/TubeFetchr-CLI/env/
VenvSelect: Found venv in parent search: /home/pydev19/Documents/Projects/Python/PyTorch_Learning/env/
VenvSelect: Found venv in parent search: /home/pydev19/Documents/Projects/Python/PyClickerX/env/
VenvSelect: Found venv in parent search: /home/pydev19/Documents/Projects/Python/PyTorch_Learning/env/lib/python3.11/site-packages/jedi/third_party/typeshed/stdlib/3/venv/
VenvSelect: Found venv in parent search: /home/pydev19/Documents/Projects/Python/ModDownloaderX/env/
VenvSelect: Found workspace folder: /home/pydev19/Documents/Projects/Python/PyTorch_Learning
VenvSelect: Running search for workspace venvs with: fd -HItd --absolute-path --color never '(^env$|^venv$|^.venv$)' --search-path /home/pydev19/Documents/Projects/Python/PyTorch_Learning 
VenvSelect: Found venv in Workspace search: /home/pydev19/Documents/Projects/Python/PyTorch_Learning/env/
VenvSelect: Found venv in Workspace search: /home/pydev19/Documents/Projects/Python/PyTorch_Learning/env/lib/python3.11/site-packages/jedi/third_party/typeshed/stdlib/3/venv/
VenvSelect: Found no venv manager directories to search for venvs.
VenvSelect: Telescope path: /home/pydev19/Documents/Projects/Python/PyTorch_Learning
VenvSelect: Removed telescope results.
VenvSelect: User refreshed results - buffer_dir is: /home/pydev19/Documents/Projects/Python/PyTorch_Learning
VenvSelect: Finding parent venvs in: /home/pydev19/Documents/Projects
VenvSelect: Found venv in parent search: /home/pydev19/Documents/Projects/Python/ModDownloaderX/env/
VenvSelect: Found venv in parent search: /home/pydev19/Documents/Projects/Python/PyClickerX/env/
VenvSelect: Found venv in parent search: /home/pydev19/Documents/Projects/Python/PyTorch_Learning/env/
VenvSelect: Found venv in parent search: /home/pydev19/Documents/Projects/Python/PyTorch_Learning/env/lib/python3.11/site-packages/jedi/third_party/typeshed/stdlib/3/venv/
VenvSelect: Found venv in parent search: /home/pydev19/Documents/Projects/Python/TubeFetchr-CLI/env/
VenvSelect: Found no workspaces to search for venvs.
VenvSelect: Found no venv manager directories to search for venvs.
VenvSelect: Removed telescope results.
VenvSelect: User refreshed results - buffer_dir is: /home/pydev19/Documents/Projects/Python/PyTorch_Learning
VenvSelect: Finding parent venvs in: /home/pydev19/Documents/Projects
VenvSelect: Found venv in parent search: /home/pydev19/Documents/Projects/Python/ModDownloaderX/env/
VenvSelect: Found venv in parent search: /home/pydev19/Documents/Projects/Python/PyClickerX/env/
VenvSelect: Found venv in parent search: /home/pydev19/Documents/Projects/Python/PyTorch_Learning/env/
VenvSelect: Found venv in parent search: /home/pydev19/Documents/Projects/Python/PyTorch_Learning/env/lib/python3.11/site-packages/jedi/third_party/typeshed/stdlib/3/venv/
VenvSelect: Found venv in parent search: /home/pydev19/Documents/Projects/Python/TubeFetchr-CLI/env/
VenvSelect: Found no workspaces to search for venvs.
VenvSelect: Found no venv manager directories to search for venvs.
VenvSelect: Removed telescope results.
VenvSelect: User refreshed results - buffer_dir is: /home/pydev19/Documents/Projects/Python/PyTorch_Learning
VenvSelect: Finding parent venvs in: /home/pydev19/Documents/Projects
E5108: Error executing lua .../nvim/lazy/venv-selector.nvim/lua/venv-selector/venv.lua:84: attempt to index local 'venv_row' (a nil value)
stack traceback:
    .../nvim/lazy/venv-selector.nvim/lua/venv-selector/venv.lua:84: in function 'set_venv_and_system_paths'
    .../nvim/lazy/venv-selector.nvim/lua/venv-selector/venv.lua:206: in function 'activate_venv'
    .../lazy/venv-selector.nvim/lua/venv-selector/telescope.lua:76: in function 'key_func'
    ...hare/nvim/lazy/telescope.nvim/lua/telescope/mappings.lua:352: in function 'execute_keymap'
    [string ":lua"]:1: in main chunk
VenvSelect: Cannot start a new search while old one is running.
VenvSelect: Cannot start a new search while old one is running.
VenvSelect: Cannot start a new search while old one is running.
VenvSelect: Found venv in parent search: /home/pydev19/Documents/Projects/Python/ModDownloaderX/env/
VenvSelect: Found venv in parent search: /home/pydev19/Documents/Projects/Python/PyClickerX/env/
VenvSelect: Found venv in parent search: /home/pydev19/Documents/Projects/Python/PyTorch_Learning/env/
VenvSelect: Found venv in parent search: /home/pydev19/Documents/Projects/Python/PyTorch_Learning/env/lib/python3.11/site-packages/jedi/third_party/typeshed/stdlib/3/venv/
VenvSelect: Found venv in parent search: /home/pydev19/Documents/Projects/Python/TubeFetchr-CLI/env/
VenvSelect: Found no workspaces to search for venvs.
VenvSelect: Found no venv manager directories to search for venvs.

While reading this I found that is does find the venv but doesn't add it to the list when I run VenvSelect it shows nothing. Further more if I run :lua print(require("venv-selector").get_active_venv()) I just get nil

linux-cultist commented 1 year ago

Yeah this is strange. I wonder what happens here... like you say, the venvs are found but there is an error when they are supposed to be added to the telescope picker.

And you have nvim-telescope/telescope.nvim installed as a requirement also, and its loaded?

I can try to look in the code and see what could go wrong, but its difficult when its working on my own system. I could add some more debug printouts to maybe understand more whats going on for you.

linux-cultist commented 1 year ago

I added some more debug output in a version i just pushed. Try again to run it and see if debug output shows some more info. Just trying to understand where it goes wrong. :)

PyDev19 commented 1 year ago

I tried it with the new push with new debug options but the output it still the same I think it has something to do with this message:

VenvSelect: Found no workspaces to search for venvs.
VenvSelect: Found no venv manager directories to search for venvs.

But if you need the full messages then this is it:

VenvSelect: 
{
  anaconda_path = "$CONDA_PREFIX/envs",
  auto_refresh = false,
  cache_dir = "/home/pydev19/.cache/venv-selector/",
  cache_file = "/home/pydev19/.cache/venv-selector/venvs.json",
  changed_venv_hooks = { <function 1>, <function 2> },
  dap_enabled = false,
  enable_debug_output = true,
  fd_binary_name = "fd",
  name = { "env" },
  notify_user_on_activate = true,
  parents = 2,
  pipenv_path = "~/.local/share/virtualenvs",
  poetry_path = "~/.cache/pypoetry/virtualenvs",
  pyenv_path = "~/.pyenv/versions",
  search = true,
  search_venv_managers = true,
  search_workspace = true
}
VenvSelect: Setting fd binary to 'fd' since it was found on system and requested by user instead of fd.
"~/Documents/Projects/Python/YTDownloader/src/main.py" 19L, 435B written
VenvSelect: Telescope path: /home/pydev19/Documents/Projects/Python/YTDownloader/src
VenvSelect: Refresh telescope since there are no previous results.
VenvSelect: Removed telescope results.
VenvSelect: Finding parent venvs in: /home/pydev19/Documents/Projects/Python
VenvSelect: Found venv in parent search: /home/pydev19/Documents/Projects/Python/TubeFetchr-CLI/env/
VenvSelect: Found venv in parent search: /home/pydev19/Documents/Projects/Python/PyTorch_Learning/env/
VenvSelect: Found venv in parent search: /home/pydev19/Documents/Projects/Python/PyClickerX/env/
VenvSelect: Found venv in parent search: /home/pydev19/Documents/Projects/Python/YTDownloader/env/
VenvSelect: Found venv in parent search: /home/pydev19/Documents/Projects/Python/ModDownloaderX/env/
VenvSelect: Found workspace folder: /home/pydev19/Documents/Projects/Python/YTDownloader
VenvSelect: Running search for workspace venvs with: fd -HItd --absolute-path --color never '(^env$)' --search-path /home/pydev19/Documents/Projects/Python/YTDownloader 
VenvSelect: Found venv in Workspace search: /home/pydev19/Documents/Projects/Python/YTDownloader/env/
VenvSelect: Found no venv manager directories to search for venvs.
VenvSelect: Removed telescope results.
VenvSelect: User refreshed results - buffer_dir is: /home/pydev19/Documents/Projects/Python/YTDownloader/src
VenvSelect: Finding parent venvs in: /home/pydev19/Documents/Projects/Python
VenvSelect: Found venv in parent search: /home/pydev19/Documents/Projects/Python/ModDownloaderX/env/
VenvSelect: Found venv in parent search: /home/pydev19/Documents/Projects/Python/PyClickerX/env/
VenvSelect: Found venv in parent search: /home/pydev19/Documents/Projects/Python/PyTorch_Learning/env/
VenvSelect: Found venv in parent search: /home/pydev19/Documents/Projects/Python/TubeFetchr-CLI/env/
VenvSelect: Found venv in parent search: /home/pydev19/Documents/Projects/Python/YTDownloader/env/
VenvSelect: Found no workspaces to search for venvs.
VenvSelect: Found no venv manager directories to search for venvs.
VenvSelect: Removed telescope results.
VenvSelect: User refreshed results - buffer_dir is: /home/pydev19/Documents/Projects/Python/YTDownloader/src
VenvSelect: Finding parent venvs in: /home/pydev19/Documents/Projects/Python
VenvSelect: Found venv in parent search: /home/pydev19/Documents/Projects/Python/ModDownloaderX/env/
VenvSelect: Found venv in parent search: /home/pydev19/Documents/Projects/Python/PyClickerX/env/
VenvSelect: Found venv in parent search: /home/pydev19/Documents/Projects/Python/PyTorch_Learning/env/
VenvSelect: Found venv in parent search: /home/pydev19/Documents/Projects/Python/TubeFetchr-CLI/env/
VenvSelect: Found venv in parent search: /home/pydev19/Documents/Projects/Python/YTDownloader/env/
VenvSelect: Found no workspaces to search for venvs.
VenvSelect: Found no venv manager directories to search for venvs.
VenvSelect: Removed telescope results.
VenvSelect: User refreshed results - buffer_dir is: /home/pydev19/Documents/Projects/Python/YTDownloader/src
VenvSelect: Finding parent venvs in: /home/pydev19/Documents/Projects/Python
VenvSelect: Found venv in parent search: /home/pydev19/Documents/Projects/Python/ModDownloaderX/env/
VenvSelect: Found venv in parent search: /home/pydev19/Documents/Projects/Python/PyClickerX/env/
VenvSelect: Found venv in parent search: /home/pydev19/Documents/Projects/Python/PyTorch_Learning/env/
VenvSelect: Found venv in parent search: /home/pydev19/Documents/Projects/Python/TubeFetchr-CLI/env/
VenvSelect: Found venv in parent search: /home/pydev19/Documents/Projects/Python/YTDownloader/env/
VenvSelect: Found no workspaces to search for venvs.
VenvSelect: Found no venv manager directories to search for venvs.
VenvSelect: Removed telescope results.
VenvSelect: User refreshed results - buffer_dir is: /home/pydev19/Documents/Projects/Python/YTDownloader/src
VenvSelect: Finding parent venvs in: /home/pydev19/Documents/Projects/Python
VenvSelect: Found venv in parent search: /home/pydev19/Documents/Projects/Python/ModDownloaderX/env/
VenvSelect: Found venv in parent search: /home/pydev19/Documents/Projects/Python/PyClickerX/env/
VenvSelect: Found venv in parent search: /home/pydev19/Documents/Projects/Python/PyTorch_Learning/env/
VenvSelect: Found venv in parent search: /home/pydev19/Documents/Projects/Python/TubeFetchr-CLI/env/
VenvSelect: Found venv in parent search: /home/pydev19/Documents/Projects/Python/YTDownloader/env/
VenvSelect: Found no workspaces to search for venvs.
VenvSelect: Found no venv manager directories to search for venvs.
VenvSelect: Removed telescope results.
VenvSelect: User refreshed results - buffer_dir is: /home/pydev19/Documents/Projects/Python/YTDownloader/src
VenvSelect: Finding parent venvs in: /home/pydev19/Documents/Projects/Python
VenvSelect: Found venv in parent search: /home/pydev19/Documents/Projects/Python/ModDownloaderX/env/
VenvSelect: Found venv in parent search: /home/pydev19/Documents/Projects/Python/PyClickerX/env/
VenvSelect: Found venv in parent search: /home/pydev19/Documents/Projects/Python/PyTorch_Learning/env/
VenvSelect: Found venv in parent search: /home/pydev19/Documents/Projects/Python/TubeFetchr-CLI/env/
VenvSelect: Found venv in parent search: /home/pydev19/Documents/Projects/Python/YTDownloader/env/
VenvSelect: Found no workspaces to search for venvs.
VenvSelect: Found no venv manager directories to search for venvs.
linux-cultist commented 1 year ago

No those messages saying it doesn't find venv manager directories are Pipenv, Pyenv, Condas and so on. It's normal for those to not be found if you don't use those.

Workspaces may or may not exist also. You should have a workspace if you use pyright but even if you don't, it should pick up venv directories located in your directories, and it does, but can't show them for some reason.

So right now I don't know what's going on here. I can experiment what happens if I don't have a workspace, maybe it's a bug triggered by that...

Edit: The debug message should have more info if you are on latest version. Things like how many rows it wants to add to telescope and some other things. Are you sure you updated venv selector?

linux-cultist commented 1 year ago

I tried also to remove my venv manager directories so they dont show, but still cant trigger the bug you have.

Here is a truncated sample from my debug output, and it has the same messages as yours but still displays the results, and this is intended behavior.

VenvSelect: Found venv in parent search: /home/cado/Code/upsales/venv/                                                                                                                                                                             
VenvSelect: Found venv in parent search: /home/cado/Code/zendesk/venv/                                                                                                                                                                             
VenvSelect: Found no workspaces to search for venvs.                                                                                                                                                                                                                                      
VenvSelect: Found no venv manager directories to search for venvs.                                                                                                                                                                                                                        
VenvSelect: There is 10 results to show.                                                                                                                                                                                                                                                  

I also pushed another change so there is no error when you select an empty row in telescope (when there are no results).

PyDev19 commented 1 year ago

Ok, so now I am getting a new message:

VenvSelect: There is 5 results to show.

But when I run :VenvSelect it shows nothing

linux-cultist commented 1 year ago

This means everything is working fine right until the very last bit where the results are supposed to be shown by telescope.

Are you sure telescope is correctly installed? If you run Telescope old_files for example, it all works?

I added a printout of the results also, just to make sure there is not something weird about yours. Update the plugin and try to use it one last time, and show how the last part looks where it prints the actual results.

Otherwise im not sure what to do actually... its difficult to troubleshoot more than this since its literally just the telescope display part left now, and that should work if its installed correctly...

Whats your operating system and vim version?

PyDev19 commented 1 year ago

I tried also to remove my venv manager directories so they dont show, but still cant trigger the bug you have.

Here is a truncated sample from my debug output, and it has the same messages as yours but still displays the results, and this is intended behavior.

VenvSelect: Found venv in parent search: /home/cado/Code/upsales/venv/                                                                                                                                                                             
VenvSelect: Found venv in parent search: /home/cado/Code/zendesk/venv/                                                                                                                                                                             
VenvSelect: Found no workspaces to search for venvs.                                                                                                                                                                                                                                      
VenvSelect: Found no venv manager directories to search for venvs.                                                                                                                                                                                                                        
VenvSelect: There is 10 results to show.                                                                                                                                                                                                                                                  

I also pushed another change so there is no error when you select an empty row in telescope (when there are no results).

I still get this error even when I select an empty row, it is telling me that there are 5 results to show but doesn't actually show them

linux-cultist commented 1 year ago

Those are not errors, just information. Its fine to not have a workspace or to not use a venv manager. The plugin will work anyway.

If you update VenvSelector to the latest version, it should also log the actual content of the rows its trying to show in telescope. Would be interesting to see them.

PyDev19 commented 1 year ago

This means everything is working fine right until the very last bit where the results are supposed to be shown by telescope.

Are you sure telescope is correctly installed? If you run Telescope old_files for example, it all works?

I added a printout of the results also, just to make sure there is not something weird about yours. Update the plugin and try to use it one last time, and show how the last part looks where it prints the actual results.

Otherwise im not sure what to do actually... its difficult to troubleshoot more than this since its literally just the telescope display part left now, and that should work if its installed correctly...

Whats your operating system and vim version?

My system is fedora 38, neovim version 0.9.1. When I run Telescope old_files it shows everything perfectly fine

Those are not errors, just information. Its fine to not have a workspace or to not use a venv manager. The plugin will work anyway.

If you update VenvSelector to the latest version, it should also log the actual content of the rows its trying to show in telescope. Would be interesting to see them.

I did update it and yes it does show the content of the rows it is trying to display like this:

VenvSelect: Telescope path: /home/pydev19/Documents/Projects/C++/TubeFetchr/src
VenvSelect: There are 1 results to show:
VenvSelect: 
{ {
    display = <function 1>,
    icon = "󰅬",
    index = 1,
    ordinal = "/home/pydev19/Documents/Projects/C++/TubeFetchr/env",
    path = "/home/pydev19/Documents/Projects/C++/TubeFetchr/env",
    source = "Search",
    value = "/home/pydev19/Documents/Projects/C++/TubeFetchr/env"
  } }

But still nothing shows in the VenvSelect command.

A new thing did happen, when I don't run neovim inside python venv and I run VenvSelect it sets the neovim venv to {current_file_name}/bin, which is not a directory. (current_file_name is the first file I open in neovim once I run it)

linux-cultist commented 1 year ago

Its quite strange. I tried moving some of my own telescope code around and renaming some things to make sure there are no conflicts with the telescope plugin, but its a shot in the dark.

I really dont understand why its not showing for you, but you can try the latest commit, maybe it helps somehow.

PyDev19 commented 1 year ago

I figured out the problem and it was all my fault. I looked into it being a telescope problem so I check my config. Then I realized that I told telescope to ignore anything including 'env' here:

defaults = {
    file_ignore_patterns = {
        "node_modules",
                "env/*"
    },
}

After removing the env/* it works now. Sorry for all the trouble I caused.

linux-cultist commented 1 year ago

I'm just happy it's finally solved. I didn't understand how it could happen and finally it makes sense. Thank you, then we close this :)