Lsp Plugin for working with Python virtual environments

Overview

py_lsp.nvim

What is py_lsp?

py_lsp.nvim is a neovim plugin that helps with using the lsp feature for python development.

It tackles the problem about the activation and usage of python virtual environments for the nvim lsp.

Installation

Using vim-plug:

Plug 'HallerPatrick/py_lsp.nvim'

Using packer.nvim:

use {'HallerPatrick/py_lsp.nvim'}

Usage

Instead of initializing the server on your own, like in nvim-lspconfig, py_lsp is doing that for you.

Put this in your init.lua (or wrap in lua call for your init.vim)

require'py_lsp'.setup {
  -- This is optional, but allows to create virtual envs from nvim
  host_python = "/path/to/python/bin"
}

This minimal setup will automatically pass a python virtual environment path to the LSP client for completion/linting.

Features

py_lsp exposes several commands that help with a virtual env workflow.

Command Parameter Usage
:PyLspCurrentVenv No Prints the currently used python venv
:PyLspDeactiveVenv No Shuts down the current LSP client
:PyLspReload No Reload LSP client with current python venv
:PyLspActivateVenv venv name Activates a virtual env with given name (default: 'venv'). This venv should lie in project root
:PyLspCreateVenv venv name Creates a virtual env with given name (default: 'venv'). Requires host_python to be set and have virtualenv installed
:PyRun command Run files and modules from current virtuale env

Most of these commands can be also run over a popup menu with :PyLspPopup.

Example Workflow

You open up your python project. Because there is no python virtual env confirgured, the LSP is not starting.

  1. You run :PyLspCreateVenv venv to create a new virtual env from host_python.
  2. You run :PyLspCurrentVenv to check if the LSP client is using your new venv.
  3. You run :PyRun -m pip install -r requirements.txt to install project dependencies.
  4. You run :PyLspReload so that the LSP client also find your new site-packages for autocompletion and correct linting.

You start programming!

Extras

The virtual environment path and name can be retrieved with client.config.settings.python.pythonPath and client.config.settings.python.venv_name. This can for example be used in your statuslines.

Example provider for feline:

local function lsp_provider(component)

    local clients = {}
    local icon = component.icon or ''

    for _, client in pairs(vim.lsp.buf_get_clients()) do
        if client.name == "pyright" then
          -- Check if lsp was initialized with py_lsp
          if client.config.settings.python["pythonPath"] ~= nil then
            local venv_name = client.config.settings.python.venv_name
            clients[#clients+1] = icon .. client.name .. '('.. venv_name .. ')'
          end
        else
          clients[#clients+1] = icon .. client.name
        end
    end

    return table.concat(clients, ' ')
end

This will give you a VSCode like status:

Statusline with LSP server and venv name

Configuration

The configurations are not sensible yet, and are suiting my setup. This will change.

Default:

Default Values:
    auto_source = true,
    language_server = "pyright",
    on_attach = nil,
    source_strategies = {"default", "poetry", "system"},
    capabilities = nil,
    host_python = nil

Todo

  • Support for different environment systems:
    • virtualenvwrapper
    • Conda
    • Pipenv

Limitations

  • All features are currently only available with pyright. pylsp is weird. It will still be started, but all features are run with a 'pyright' server or not at all.
  • py_lsp expects to find virtualenv in the cwd, please check for that

Note

This plugin is created due to following Issue.

This plugin currently includes a utility to automatically pass a virtualenv to the pyright lsp server before initialization also take from the Issue. (Thanks lithammer and others).

Comments
  • Telescope is a necessary dependency?

    Telescope is a necessary dependency?

    After installing the plugin:

    E5113: Error while calling lua chunk: ...m/site/pack/packer/start/py_lsp.nvim/lua/py_lsp/init.lua:1: module 'telescope.pickers' not found:
            no field package.preload['telescope.pickers']
            no file './telescope/pickers.lua'
            no file '/__w/neovim/neovim/.deps/usr/share/luajit-2.1.0-beta3/telescope/pickers.lua'
            no file '/usr/local/share/lua/5.1/telescope/pickers.lua'
            no file '/usr/local/share/lua/5.1/telescope/pickers/init.lua'
            no file '/__w/neovim/neovim/.deps/usr/share/lua/5.1/telescope/pickers.lua'
            no file '/__w/neovim/neovim/.deps/usr/share/lua/5.1/telescope/pickers/init.lua'
            no file './telescope/pickers.so'
            no file '/usr/local/lib/lua/5.1/telescope/pickers.so'
            no file '/__w/neovim/neovim/.deps/usr/lib/lua/5.1/telescope/pickers.so'
            no file '/usr/local/lib/lua/5.1/loadall.so'
            no file './telescope.so'
            no file '/usr/local/lib/lua/5.1/telescope.so'
            no file '/__w/neovim/neovim/.deps/usr/lib/lua/5.1/telescope.so'
            no file '/usr/local/lib/lua/5.1/loadall.so'
    stack traceback:
            [C]: in function 'require'
            ...m/site/pack/packer/start/py_lsp.nvim/lua/py_lsp/init.lua:1: in main chunk
            [C]: in function 'require'
            ...user/.config/nvim/lua/settings.lua:46: in main chunk
            [C]: in function 'require'
            /home/user/.config/nvim/init.lua:2: in main chunk
    
    opened by Turbid 3
  • Add conda integration

    Add conda integration

    I noticed this is already on your todos, but it would be a really nice feature to see. It seems like you could integrate it pretty easily by searching the envs path at the conda installation location (usually something like ~/conda). I was able to follow the example here to create my own little change Python interpreter code:

    function change_python_interpreter(path)
        vim.lsp.stop_client(vim.lsp.get_active_clients())
        configs.pyright.settings.python.pythonPath = path
        require'lspconfig'.pyright.setup(configs.pyright)
        vim.cmd('e%')
    end
    
    function get_python_interpreters(a, l, p)
        local paths = {}
        local is_home_dir = function()
            return vim.fn.getcwd(0) == vim.fn.expand("$HOME")
        end
        local commands = {'fd --glob -tl python $HOME/mambaforge', 'which -a python', is_home_dir() and '' or 'find . -name python'}
        for _, cmd in ipairs(commands) do
            local _paths = vim.fn.systemlist(cmd)
            if _paths then
                for _, path in ipairs(_paths) do
                    table.insert(paths, path)
                end
            end
        end
        table.sort(paths)
        local res = {}
        for i, path in ipairs(paths) do
            if path ~= paths[i+1] then table.insert(res, path) end
        end
        return res
    end
    
    vim.api.nvim_exec([[
    command! -nargs=1 -complete=customlist,PythonInterpreterComplete PythonInterpreter lua change_python_interpreter(<q-args>)
    
    function! PythonInterpreterComplete(A,L,P) abort
      return v:lua.get_python_interpreters()
    endfunction
    ]], false)
    

    This provides a PythonInterpreter vim command which offers completion options of all conda environments and works well for me. However, I like what you are doing with this plugin much better.

    opened by cnrrobertson 3
  • Improve options: remove on_attach preset and add capabilities

    Improve options: remove on_attach preset and add capabilities

    Also removes the completion dependency.

    Resolves https://github.com/HallerPatrick/py_lsp.nvim/issues/7

    PS: I think I added some automatic styling in the first, older commit – sorry!

    opened by d-miketa 3
  • Issue with :PyFindVenvs

    Issue with :PyFindVenvs

    So when running this command I get this error image

    Doing some research (and editing my stratagies.lua the function table.unpack isn't available in older versions of lua info - any way I was able to fix it by removing the table.

    Is this something you want to fix ? or should we recommend users update there Lua to 5.2 ?

    opened by thomascrha 2
  • Conda integration

    Conda integration

    As discussed in #18, this adds support for finding and activating conda environments. Reloading and closing environment will work out of the box while PyRun and PyLspCreateVenv will need further work.

    opened by cnrrobertson 2
  • [Help]: more LSP support

    [Help]: more LSP support

    Currently the plugin supports pyright out of box. Is there any plannings of support other python LSP such as https://github.com/python-lsp/python-lsp-server and https://github.com/pappasam/jedi-language-server.

    opened by younger-1 2
  • Add completion.nvim plugin to Installation section in README

    Add completion.nvim plugin to Installation section in README

    Hey thanks for this plugin, it works great!

    When I installed this for the first time with Vim-Plug I did not know this plugin requires completion-nvim so the require("completion") part in options.lua caused an error when resourcing my init.vim file.

    Therefore, I added this to the Installation section :)

    opened by smjonas 1
  • Remove 'completion' from on_attach, add capabilities

    Remove 'completion' from on_attach, add capabilities

    Hi, thanks for the plugin! I'm currently deciding on my Python workflow and this'll probably make the cut. :) I have two improvements to suggest:

    1. It seems unnecessary to demand the completion plugin. It only appears in the default on_attach setting and can be safely removed.
    2. It'd be good to also expose a capabilities field when passing options to require'lspconfig'.setup as that's where you turn on snippet support.
    opened by d-miketa 0
  • Make :PyRun async with output printed to stdout

    Make :PyRun async with output printed to stdout

    Its annoying to wait for a pip install to finish. Also maybe do a auto reload of lsp server after so, the changes in site-packages are seen by Pyright

    enhancement 
    opened by HallerPatrick 0
  • Incompatible with LSP installed by LspInstall

    Incompatible with LSP installed by LspInstall

    https://github.com/kabouzeid/nvim-lspinstall is a convenient plugin to install LSP servers. They end up in a separate directory outside of $PATH (~/.local/share/nvim/lspinstall/python/./node_modules/.bin/pyright-langserver in my case), but https://github.com/kabouzeid/nvim-lspinstall/blob/54b439241e83e0a9ce8f6bcdf3b2c560a2328792/lua/lspinstall.lua#L94 corrects the default cmd for lspconfig to point to that location. However py_lsp doesn't pick up on it for some reason, perhaps overriding cmd or cmd_cwd in default_config?

    This is with py_lsp installed and used to set up Pyright: image image

    And this is with py_lsp disabled: (note cmd) image

    opened by d-miketa 2
Releases(v0.0.1)
Owner
Patrick Haller
Computer Science Master, based in Berlin
Patrick Haller
A command line connect 4 game against a minimax agent.

A command line connect 4 game against a minimax agent.

1 Oct 17, 2021
A tool to manage the study of courses at the university.

todo-cli A tool to manage the study of courses at the university

Quentin 6 Aug 01, 2022
Commandline script to interact with volkswagencarnet library

volkswagencarnet-client command line script to interact with volkswagencarnet library Table of Contents General Info Setup Usage Example Acknowledgeme

3 Jan 19, 2022
A simple weather tool. I made this as a way for me to learn Python, API, and PyPi packaging.

A simple weather tool. I made this as a way for me to learn Python, API, and PyPi packaging.

Clint E. 105 Dec 31, 2022
A begginer reverse shell tool python.

A begginer tools for hacking. The theme of this repository is to bring some ready-made open-source tools for anyone new to the world of hacking. This

Dio brando 2 Jan 05, 2022
Un module simple pour demander l'accord de l'utilisateur dans une CLI.

Demande de confirmation utilisateur pour CLI Présentation ask_lib est un module pour le langage Python proposant une seule fonction; ask(). Le but pri

CallMePixelMan 7 May 09, 2022
Cek Username IG Yang Masih Bisa Dipake

Cek Username IG Cara Install $ pkg update && pkg upgrade $ pkg install python $ pkg install git $ git clone https://github.com/Dekusec/ig-checker $ cd

Deku 3 Nov 28, 2021
Convert shellcode into :sparkles: different :sparkles: formats!

Bluffy Convert shellcode into ✨ different ✨ formats! Bluffy is a utility which was used in experiments to bypass Anti-Virus products (statically) by f

AD995 305 Dec 17, 2022
CLI/library to control FNIRSI DC Power Supply (DC-6006L, etc)

dc6006l - CLI/library to control FNIRSI DC Power Supply (DC-6006L, etc) What is this? FNIRSI DC6006L is a programmable DC power supply that is quite c

Taisuke Yamada 7 Sep 25, 2022
A simple command line virtual operating system, written in python

Virtual operating system A simple virtual operating system written in python. (Under development). Currently, the following commands are supported: Co

B.Jothin kumar 7 Nov 15, 2022
A terminal tool for git. When we use git, do you feel very uncomfortable with too long commands

PIGIT A terminal tool for git. When we use git, do you feel very uncomfortable with too long commands. For example: git status --short, this project c

Zachary 1 Apr 09, 2022
Create argparse subcommands with decorators.

python-argparse-subdec This is a very simple Python package that allows one to create argparse's subcommands via function decorators. Usage Create a S

Gustavo José de Sousa 7 Oct 21, 2022
Cthulhu is a simple python CLI application that streams torrents directly from 1337x.

Cthulhu is a simple python CLI application that facilitates the streaming of torrents directly from 1337x. It uses webtorrent to stream video

Raiyan 27 Dec 27, 2022
🏃 Python3 Solutions of All Problems in GCJ 2022 (In Progress)

GoogleCodeJam 2022 Python3 solutions of Google Code Jam 2022. Solution begins with * means it will get TLE in the largest data set. Total computation

kamyu 12 Dec 20, 2022
Simple CLI interface for linear task manager

Linear CLI (Unmaintained) Simple CLI interface for linear task manager Usage Install: pip install linearcli Setup: Generate a pe

Mike Lyons 1 Jan 07, 2022
Loading animation; a progress bar

Loading animation; a progress bar. When you know the remaining time or task completion percentage, then you’re able to show an animated progress bar:

Goldy 1 Jan 23, 2022
'rl_UK' is an open-source command-line tool in Python for calculating the shortest path between BUS stop sequences in the UK

'rl_UK' is an open-source command-line tool in Python for calculating the shortest path between BUS stop sequences in the UK. As input files, it uses an ATCO-CIF file and 'OS Open Roads' dataset from

Nesh P. 0 Feb 16, 2022
Simple subcommand CLIs with argparse

multicommand Simple subcommand CLIs with argparse. multicommand uses only the standard library and is ~150 lines of code (modulo comments and whitespa

Andrew Ross 10 Aug 01, 2022
PdpCLI is a pandas DataFrame processing CLI tool which enables you to build a pandas pipeline from a configuration file.

PdpCLI Quick Links Introduction Installation Tutorial Basic Usage Data Reader / Writer Plugins Introduction PdpCLI is a pandas DataFrame processing CL

Yasuhiro Yamaguchi 15 Jan 07, 2022
asciinema - Terminal session recorder 📹

asciinema - Terminal session recorder 📹

asciinema 11.1k Dec 27, 2022