Advertisement
Wurfkreuz

Untitled

May 13th, 2023
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.25 KB | None | 0 0
  1. Features
  2. Browse existing python virtual environments on your computer and select one to activate with pyright inside neovim.
  3.  
  4. Plug and play, no configuration required
  5. Switch back and forth between virtual environments without restarting neovim
  6. Cached virtual environment that ties to your workspace for easy activation subsequently
  7. Support pyright and pylsp with ability to config hooks for other LSP
  8. Requires fd and Telescope for fast searches, and visual pickers.
  9. Requires nvim-dap-python, debugpy and nvim-dap for debugger support
  10. clipboard Installation and Configuration
  11. The plugin works with pyright and pylsp lsp servers. If you want to take advantage of this plugin's default behaviour, you need to have either of them installed and configured using lspconfig. If you want to use custom integration, see hooks section before using this plugin. You can see example setup instructions here: https://github.com/neovim/nvim-lspconfig/blob/master/doc/server_configurations.md#pyright
  12.  
  13. Using folke/lazy.nvim
  14. A minimal config looks like this and would typically go into your folder where you have plugins:
  15.  
  16. return {
  17. "linux-cultist/venv-selector.nvim",
  18. dependencies = { "neovim/nvim-lspconfig", "nvim-telescope/telescope.nvim" },
  19. config = true,
  20. event = "VeryLazy", -- Optional: needed only if you want to type `:VenvSelect` without a keymapping
  21. }
  22. Feel free to add a nice keymapping here as well:
  23.  
  24. return {
  25. "linux-cultist/venv-selector.nvim",
  26. dependencies = { "neovim/nvim-lspconfig", "nvim-telescope/telescope.nvim" },
  27. config = true,
  28. event = "VeryLazy", -- Optional: needed only if you want to type `:VenvSelect` without a keymapping
  29. keys = {{
  30. "<leader>vs", "<cmd>:VenvSelect<cr>",
  31. -- key mapping for directly retrieve from cache. You may set autocmd if you prefer the no hand approach
  32. "<leader>vs", "<cmd>:VenvSelectCached<cr>"
  33. }}
  34. }
  35. If you want to change the default options, you can add an opts table like this:
  36.  
  37. return {
  38. "linux-cultist/venv-selector.nvim",
  39. dependencies = {
  40. "neovim/nvim-lspconfig",
  41. "nvim-telescope/telescope.nvim",
  42. -- for DAP support
  43. "mfussenegger/nvim-dap-python"
  44. },
  45. config = true,
  46. keys = {{
  47. "<leader>vs", "<cmd>:VenvSelect<cr>",
  48. -- optional if you use a autocmd (see #🤖-Automate)
  49. "<leader>vc", "<cmd>:VenvSelectCached<cr>"
  50. }},
  51. opts = {
  52.  
  53. -- auto_refresh (default: false). Will automatically start a new search every time VenvSelect is opened.
  54. -- When its set to false, you can refresh the search manually by pressing ctrl-r. For most users this
  55. -- is probably the best default setting since it takes time to search and you usually work within the same
  56. -- directory structure all the time.
  57. auto_refresh = false,
  58.  
  59. -- search_venv_managers (default: true). Will search for Poetry and Pipenv virtual environments in their
  60. -- default location. If you dont use the default location, you can
  61. search_venv_managers = true,
  62.  
  63. -- search_workspace (default: true). Your lsp has the concept of "workspaces" (project folders), and
  64. -- with this setting, the plugin will look in those folders for venvs. If you only use venvs located in
  65. -- project folders, you can set search = false and search_workspace = true.
  66. search_workspace = true,
  67.  
  68. -- path (optional, default not set). Absolute path on the file system where the plugin will look for venvs.
  69. -- Only set this if your venvs are far away from the code you are working on for some reason. Otherwise its
  70. -- probably better to let the VenvSelect search for venvs in parent folders (relative to your code). VenvSelect
  71. -- searchs for your venvs in parent folders relative to what file is open in the current buffer, so you get
  72. -- different results when searching depending on what file you are looking at.
  73. -- path = "/home/username/your_venvs",
  74.  
  75. -- search (default: true). Search your computer for virtual environments outside of Poetry and Pipenv.
  76. -- Used in combination with parents setting to decide how it searches.
  77. -- You can set this to false to speed up the plugin if your virtual envs are in your workspace, or in Poetry
  78. -- or Pipenv locations. No need to search if you know where they will be.
  79. search = true,
  80.  
  81. -- dap_enabled (default: false). When true, uses the selected virtual environment with the debugger.
  82. -- require nvim-dap-python from https://github.com/mfussenegger/nvim-dap-python
  83. -- require debugpy from https://github.com/microsoft/debugpy
  84. -- require nvim-dap from https://github.com/mfussenegger/nvim-dap
  85. dap_enabled = false
  86.  
  87. -- parents (default: 2) - Used when search = true only. How many parent directories the plugin will go up
  88. -- (relative to where your open file is on the file system when you run VenvSelect). Once the parent directory
  89. -- is found, the plugin will traverse down into all children directories to look for venvs. The higher
  90. -- you set this number, the slower the plugin will usually be since there is more to search.
  91. -- You may want to set this to to 0 if you specify a path in the path setting to avoid searching parent
  92. -- directories.
  93. parents = 2,
  94.  
  95. -- name (default: venv) - The name of the venv directories to look for.
  96. name = "venv", -- NOTE: You can also use a lua table here for multiple names: {"venv", ".venv"}`
  97.  
  98. -- fd_binary_name (default: fd) - The name of the fd binary on your system.
  99. fd_binary_name = "fd",
  100.  
  101.  
  102. -- notify_user_on_activate (default: true) - Prints a message that the venv has been activated
  103. notify_user_on_activate = true,
  104.  
  105. }
  106. event = "VeryLazy", -- Optional: needed only if you want to type `:VenvSelect` without a keymapping
  107. }
  108. Or you can manually run the setup function with options like this:
  109.  
  110. return {
  111. "linux-cultist/venv-selector.nvim",
  112. dependencies = { "neovim/nvim-lspconfig", "nvim-telescope/telescope.nvim" },
  113. keys = {{
  114. "<leader>vs", "<cmd>:VenvSelect<cr>",
  115. -- optional if you use a autocmd (see #🤖-Automate)
  116. "<leader>vc", "<cmd>:VenvSelectCached<cr>"
  117. }},
  118. config = function()
  119. require("venv-selector").setup({
  120.  
  121. -- auto_refresh (default: false). Will automatically start a new search every time VenvSelect is opened.
  122. -- When its set to false, you can refresh the search manually by pressing ctrl-r. For most users this
  123. -- is probably the best default setting since it takes time to search and you usually work within the same
  124. -- directory structure all the time.
  125. auto_refresh = false,
  126.  
  127. -- search_venv_managers (default: true). Will search for Poetry and Pipenv virtual environments in their
  128. -- default location. If you dont use the default location, you can
  129. search_venv_managers = true,
  130.  
  131. -- search_workspace (default: true). Your lsp has the concept of "workspaces" (project folders), and
  132. -- with this setting, the plugin will look in those folders for venvs. If you only use venvs located in
  133. -- project folders, you can set search = false and search_workspace = true.
  134. search_workspace = true,
  135.  
  136. -- path (optional, default not set). Absolute path on the file system where the plugin will look for venvs.
  137. -- Only set this if your venvs are far away from the code you are working on for some reason. Otherwise its
  138. -- probably better to let the VenvSelect search for venvs in parent folders (relative to your code). VenvSelect
  139. -- searchs for your venvs in parent folders relative to what file is open in the current buffer, so you get
  140. -- different results when searching depending on what file you are looking at.
  141. -- path = "/home/username/your_venvs",
  142.  
  143. -- search (default: true) - Search your computer for virtual environments outside of Poetry and Pipenv.
  144. -- Used in combination with parents setting to decide how it searches.
  145. -- You can set this to false to speed up the plugin if your virtual envs are in your workspace, or in Poetry
  146. -- or Pipenv locations. No need to search if you know where they will be.
  147. search = true,
  148.  
  149. -- dap_enabled (default: false) Configure Debugger to use virtualvenv to run debugger.
  150. -- require nvim-dap-python from https://github.com/mfussenegger/nvim-dap-python
  151. -- require debugpy from https://github.com/microsoft/debugpy
  152. -- require nvim-dap from https://github.com/mfussenegger/nvim-dap
  153. dap_enabled = false
  154.  
  155. -- parents (default: 2) - Used when search = true only. How many parent directories the plugin will go up
  156. -- (relative to where your open file is on the file system when you run VenvSelect). Once the parent directory
  157. -- is found, the plugin will traverse down into all children directories to look for venvs. The higher
  158. -- you set this number, the slower the plugin will usually be since there is more to search.
  159. -- You may want to set this to to 0 if you specify a path in the path setting to avoid searching parent
  160. -- directories.
  161. parents = 2,
  162.  
  163. -- name (default: venv) - The name of the venv directories to look for.
  164. name = "venv", -- NOTE: You can also use a lua table here for multiple names: {"venv", ".venv"}`
  165.  
  166. -- fd_binary_name (default: fd) - The name of the fd binary on your system. Some Debian based Linux Distributions like Ubuntu use ´fdfind´.
  167. fd_binary_name = "fd",
  168.  
  169.  
  170. -- notify_user_on_activate (default: true) - Prints a message that the venv has been activated
  171. notify_user_on_activate = true,
  172.  
  173. })
  174. end
  175. event = "VeryLazy", -- Optional: needed only if you want to type `:VenvSelect` without a keymapping
  176. }
  177. Hooks
  178. By default, the plugin tries to setup pyright and pylsp automatically using hooks. If you want to add a custom integration, you need to write a hook with following signature:
  179.  
  180. --- @param venv_path string A string containing the absolute path to selected virtualenv
  181. --- @param venv_python string A string containing the absolute path to python binary in selected venv
  182. function your_hook_name(venv_path, venv_python)
  183. --- your custom integration here
  184. end
  185. And provide it to a setup function:
  186.  
  187. require("venv-selector").setup({
  188. --- other configuration
  189. changed_venv_hooks = { your_hook_name }
  190. })
  191. The plugin-provided hooks are exposed for convenience in case you want to use them alongside your custom one:
  192.  
  193. local venv_selector = require("venv-selector")
  194.  
  195. venv_selector.setup({
  196. --- other configuration
  197. changed_venv_hooks = { your_hook_name, venv_selector.hooks.pyright }
  198. })
  199. Currently provided hooks are:
  200.  
  201. require("venv-selector").hooks.pyright
  202. require("venv-selector").hooks.pylsp
  203. Helpful functions
  204. The selected virtual environment and path to the python executable is available from these two functions:
  205.  
  206. require("venv-selector").get_active_path() -- Gives path to the python executable inside the activated virtual environment
  207. require("venv-selector").get_active_venv() -- Gives path to the activated virtual environment folder
  208. require("venv-selector").retrieve_from_cache() -- To activate the last virtual environment set in the current working directory
  209. require("venv-selector").deactivate_venv() -- Deactivates the virtual environment and unsets VIRTUAL_ENV environment variable.
  210. This can be used to print out the virtual environment in a status bar, or make the plugin work with other plugins that want this information.
  211.  
  212. comet Getting started
  213. Once the plugin has been installed, the :VenvSelect command is available.
  214.  
  215. This plugin will look for python virtual environments located close to your code.
  216.  
  217. It will start looking in the same directory as your currently opened file. Usually the venv is located in a parent directory. By default it will go up 2 levels in the directory tree (relative to your currently open file), and then go back down into all the directories under that directory. Finally it will give you a list of found virtual environments so you can pick one to activate.
  218.  
  219. robot Automate
  220. After choosing your virtual environment, the path to the virtual environment will be cached under the current working directory. To activate the same virtual environment the next time, simply use :VenvSelectCached to reactivate your virtual environment.
  221.  
  222. This can also be automated to run whenever you enter into a python project, for example.
  223.  
  224. vim.api.nvim_create_autocmd("VimEnter", {
  225. desc = "Auto select virtualenv Nvim open",
  226. pattern = "*",
  227. callback = function()
  228. local venv = vim.fn.findfile("pyproject.toml", vim.fn.getcwd() .. ";")
  229. if venv ~= "" then
  230. require("venv-selector").retrieve_from_cache()
  231. end
  232. end,
  233. once = true,
  234. })
  235. If you use Poetry, Pipenv or Pyenv-virtualenv
  236. If you use Poetry, Pipenv or Pyenv-virtualenv, you typically have all the virtual environments located in the same path as subfolders.
  237.  
  238. VenvSelector automatically looks in the default paths for both Poetry, Pipenv and Pyenv-virtualenv virtual environments:
  239.  
  240. Mac:
  241.  
  242. Poetry: $HOME/Library/Caches/pypoetry/virtualenvs
  243. Pipenv: $HOME/.local/share/virtualenvs
  244. Pyenv: HOME/.pyenv.versions
  245. Linux:
  246.  
  247. Poetry: $HOME/.cache/pypoetry/virtualenvs
  248. Pipenv: $HOME/.local/share/virtualenvs
  249. Pyenv: HOME/.pyenv.versions
  250. Windows:
  251.  
  252. Poetry: %APPDATA%\\pypoetry\\virtualenvs
  253. Pipenv: $HOME\\virtualenvs
  254. Pyenv: %USERPROFILE%\\.pyenv\\versions
  255. You can override the default paths if the virtual environments are not being found by VenvSelector:
  256.  
  257. require("venv-selector").setup({
  258. poetry_path = "your_path_here",
  259. pipenv_path = "your_path_here",
  260. pyenv_path = "your_path_here",
  261. })
  262. Find out where your virtual environments are located
  263. Poetry
  264. First run poetry env info in your project folder where you are using poetry to manage the virtual environments. You should get some output simular to this:
  265.  
  266. Virtualenv
  267. Python: 3.10.10
  268. Implementation: CPython
  269. Path: /home/cado/.cache/pypoetry/virtualenvs/poetry-demo-EUUW_nAM-py3.10
  270. Executable: /home/cado/.cache/pypoetry/virtualenvs/poetry-demo-EUUW_nAM-py3.10/bin/python
  271. Valid: True
  272.  
  273. System
  274. Platform: linux
  275. OS: posix
  276. Python: 3.10.10
  277. Path: /usr
  278. Executable: /usr/bin/python3.10
  279. You can see that the path shows that the virtual environments are located under /home/cado/.cache/pypoetry/virtualenvs in this case.
  280.  
  281. Copy the virtualenv path and set it as a parameter to the VenvSelector setup function:
  282.  
  283. require("venv-selector").setup({
  284. poetry_path = "/home/cado/.cache/pypoetry/virtualenvs",
  285. })
  286. Pipenv
  287. First run pipenv --venv in your project folder where you are using pipenv to manage the virtual environments. You should get some output simular to this:
  288.  
  289. /home/cado/.local/share/virtualenvs/pipenv_test-w6BD3kWZ
  290. You can see that the path shows that the virtual environments are located under /home/cado/.local/share/virtualenvs in this case.
  291.  
  292. Copy the virtualenv path and set it as a parameter to the VenvSelector setup function:
  293.  
  294. require("venv-selector").setup({
  295. pipenv_path = "/home/cado/.local/share/virtualenvs",
  296. })
  297. Pyenv-virtualenv
  298. First run pyenv root to get the rootfolder for pyenv versions and shims are kept. You should get some output similar to this:
  299.  
  300. /home/cado/.pyenv
  301.  
  302. The virtualenvs are stored under the versions folder inside that directory. In this case it would be /home/cado/.pyenv/versions.
  303.  
  304. Copy the virtualenv path and set it as a parameter to the VenvSelector setup function:
  305.  
  306. require("venv-selector").setup({
  307. pyenv_path = "/home/cado/.pyenv/versions",
  308. })
  309. Dependencies
  310. This plugin has been built to be as fast as possible. It will search your computer for virtual environments while you continue working, and it wont freeze the neovim gui while looking for them. Usually it will give you a result in a few seconds.
  311.  
  312. Even better, with caching enabled, this plugin instantly activate previously configured virtual environment without having to spend time on searching again.
  313.  
  314. Note: You need fd installed on your system. This plugin uses it to search for the virtual environments as fast as possible.
  315.  
  316. Telescope is also needed to let you pick a virtual environment to use.
  317.  
  318. nvim-python-dap is required for DAP function. Enable DAP at config.
  319.  
  320. bulbTips and Tricks
  321. VS Code like statusline functionality with heirline
  322. To add a clickable component to your heirline statusline.
  323.  
  324. local actived_venv = function()
  325. local venv_name = require("venv-selector").get_active_venv()
  326. if venv_name ~= nil then
  327. return string.gsub(venv_name, ".*/pypoetry/virtualenvs/", "(poetry) ")
  328. else
  329. return "venv"
  330. end
  331. end
  332.  
  333. local venv = {
  334. {
  335. provider = function() return "  " .. actived_venv() end,
  336. },
  337. on_click = {
  338. callback = function() vim.cmd.VenvSelect() end,
  339. name = "heirline_statusline_venv_selector",
  340. },
  341. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement