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 startpage configured aesthetically with terminal-esque link formatting

Terminal-y Startpage Setup Clone the repository, then make an unformatted.txt file following the specifications in example.txt. Run format.py Open ind

belkarx 13 May 01, 2022
A Python package for a basic CLI and GUI user interface

Organizer CLI Organizer CLI is a python command line tool that goes through a given directory and organizes all un-folder bound files into folders by

Caltech Library 12 Mar 25, 2022
Hack-All is a simple CLI tool that helps ethical-hackers to make a reverse connection without knowing the target device in use is it computer or phone

Hack-All is a simple CLI tool that helps ethical-hackers to make a reverse connection without knowing the target device in use is it computer

LightYagami17 5 Nov 22, 2022
touch command for Windows

Touch touch command for Windows Setup: Clone the repository git clone https://github.com/g-paras/touch.git cd touch Install touch module python setup.

Paras Gupta 5 Jan 04, 2022
A Tempmail Tool for Terminal and Termux.

A Tempmail Tool for Terminal and Termux.

MAO-COMMUNITY 8 Oct 19, 2022
A Python module and command-line utility for converting .ANS format ANSI art to HTML

ansipants A Python module and command-line utility for converting .ANS format ANSI art to HTML. Installation pip install ansipants Command-line usage

4 Oct 16, 2022
A supercharged Git/GitHub command line interface (CLI)

A supercharged Git/GitHub command line interface (CLI).

Donne Martin 7.4k Jan 07, 2023
A terminal spreadsheet multitool for discovering and arranging data

VisiData v2.6.1 A terminal interface for exploring and arranging tabular data. VisiData supports tsv, csv, sqlite, json, xlsx (Excel), hdf5, and many

Saul Pwanson 6.2k Jan 04, 2023
This is a command line program to play cricket made using Python.

SimpleCricketPython This is a command line program to play cricket made using Python How it works First you have the option of selecting whether you

Imira Randeniya 1 Sep 11, 2022
A communist shell written in Python

kash A communist shell written in Python It doesn't support escapes, quotes, comment lines, |, &&, , or similar yet. If you need help, get it from

Çınar Yılmaz 1 Dec 10, 2021
WA Terminal is a CLI application that allows us to login and send message with WhatsApp with a single command.

WA Terminal is a CLI application that allows us to login and send message with WhatsApp with a single command.

Aziz Fikri 15 Apr 15, 2022
Juniper Command System is a Micro CLI Tool that allows you to manage your files, launch applications, as well as providing extra tools for OS Management.

Juniper Command System is a Micro CLI Tool that allows you to manage your files, launch applications, as well as providing extra tools for OS Management.

Juan Carlos Juárez 1 Feb 02, 2022
Simple Tool To Grab Like-Card Coupon

Simple Tool To Grab Like-Card Coupon

Soud 10 Jan 30, 2022
This is the public repo for the VS Code Extension AT&T i386/IA32 UIUC-ECE391 Syntax Highlighting

AT&T i386 IA32 UIUC ECE391 GCC Highlighter & Snippet & Linter This is the VS Code Extension for UIUC ECE 391, MIT 6.828, and all other AT&T-based i386

Jackgetup 1 Feb 05, 2022
CLI tool for one-line installation of C++/CMake projects.

cmakip When working on virtual environments, Python projects can be installed with a single command invocation, for example pip install --no-deps . .

Artificial and Mechanical Intelligence 4 Feb 15, 2022
Python package with library and CLI tool for analyzing SeaFlow data

Seaflowpy A Python package for SeaFlow flow cytometer data. Table of Contents Install Read EVT/OPP/VCT Files Command-line Interface Configuration Inte

<a href=[email protected]"> 3 Nov 03, 2021
Detect secret in source code, scan your repo for leaks. Find secrets with GitGuardian and prevent leaked credentials. GitGuardian is an automated secrets detection & remediation service.

GitGuardian Shield: protect your secrets with GitGuardian GitGuardian shield (ggshield) is a CLI application that runs in your local environment or in

GitGuardian 1.2k Jan 06, 2023
CLI client for RFC 4226's HOTP and RFC 6238's TOTP.

One Time Password (OTP, TOTP/HOTP) OTP serves as additional protection in case of password leaks. onetimepass allows you to manage OTP codes and gener

Apptension 4 Jan 05, 2022
A simple script to make the operation of AltServer-Linux more easier with cli

A simple script to make the operation of AltServer-Linux more easier with cli

powen 23 Dec 13, 2022
A command line utility to export Google Keep notes to markdown.

Keep-Exporter A command line utility to export Google Keep notes to markdown files with metadata stored as a frontmatter header. Supports exporting: S

Nathan Beals 85 Dec 17, 2022