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
Container images for portable development environments

Docker Dev Spin up a container to develop from anywhere! To run, just: docker run -ti aghost7/nodejs-dev:boron tmux new Alternatively, if on Linux: p

Jonathan Boudreau 163 Dec 22, 2022
A CLI tool for creating disposable environments.

dispenv - Disposable Python Environments ⚠️ WIP Need to make an environment to work on a GitHub issue? Want to try out a new package and not leave the

Peter Baumgartner 3 Mar 14, 2022
A Python module and command line utility for working with web archive data using the WACZ format specification

py-wacz The py-wacz repository contains a Python module and command line utility for working with web archive data using the WACZ format specification

Webrecorder 14 Oct 24, 2022
Kubernetes shell: An integrated shell for working with the Kubernetes

kube-shell Kube-shell: An integrated shell for working with the Kubernetes CLI Under the hood kube-shell still calls kubectl. Kube-shell aims to provi

CloudNative Labs 2.2k Jan 8, 2023
commandpack - A package of modules for working with commands, command packages, files with command packages.

commandpack Help the project financially: Donate: https://smartlegion.github.io/donate/ Yandex Money: https://yoomoney.ru/to/4100115206129186 PayPal:

null 4 Sep 4, 2021
git-partial-submodule is a command-line script for setting up and working with submodules while enabling them to use git's partial clone and sparse checkout features.

Partial Submodules for Git git-partial-submodule is a command-line script for setting up and working with submodules while enabling them to use git's

Nathan Reed 15 Sep 22, 2022
🐍The nx-python plugin allows users to create a basic python application using nx commands.

?? NxPy: Nx Python plugin This project was generated using Nx. The nx-python plugin allows users to create a basic python application using nx command

StandUP Communications 74 Aug 31, 2022
A CLI Spigot plugin manager that adheres to Unix conventions and Python best practices.

Spud A cross-platform, Spigot plugin manager that adheres to the Unix philosophy and Python best practices. Some focuses of the project are: Easy and

Tommy Dougiamas 9 Dec 2, 2022
Magma is a NeoVim plugin for running code interactively with Jupyter.

Magma Magma is a NeoVim plugin for running code interactively with Jupyter. Requirements NeoVim 0.5+ Python 3.8+ Required Python packages: pynvim (for

Daniel Csillag 372 Dec 26, 2022
A ZSH plugin that enables you to use OpenAI's powerful Codex AI in the command line.

A ZSH plugin that enables you to use OpenAI's powerful Codex AI in the command line.

Tom Dörr 976 Jan 3, 2023
A minimalist Vim plugin manager.

A minimalist Vim plugin manager. Pros. Easy to set up: Single file. No boilerplate code required. Easy to use: Concise, intuitive syntax Super-fast pa

Junegunn Choi 30.2k Jan 8, 2023
🦎 A NeoVim plugin for highlighting visual selections like in a normal document editor!

?? HighStr.nvim A NeoVim plugin for highlighting visual selections like in a normal document editor! Demo TL;DR HighStr.nvim is a NeoVim plugin writte

Pocco81 222 Jan 3, 2023
Simple Python Library to display text with color in Python Terminal

pyTextColor v1.0 Introduction pyTextColor is a simple Python Library to display colorful outputs in Terminal, etc. Note: Your Terminal or any software

Siddhesh Chavan 1 Jan 23, 2022
cli simple python script to interact with iphone afc api based on python library( tidevice )

afcclient cli simple python script to interact with iphone afc api based on python library( tidevice ) installation pip3 install -U tidevice cp afccli

fyst_14 2 Jul 15, 2022
Zecwallet-Python is a simple wrapper around the Zecwallet Command Line LightClient written in Python

A wrapper around Zecwallet Command Line LightClient, written in Python Table of Contents About Installation Usage Examples About Zecw

Priveasy 2 Sep 6, 2022
Python command line tool and python engine to label table fields and fields in data files.

Python command line tool and python engine to label table fields and fields in data files. It could help to find meaningful data in your tables and data files or to find Personal identifable information (PII).

APICrafter 22 Dec 5, 2022
xonsh is a Python-powered, cross-platform, Unix-gazing shell

xonsh is a Python-powered, cross-platform, Unix-gazing shell language and command prompt.

xonsh 6.7k Dec 31, 2022
A command-line utility that creates projects from cookiecutters (project templates), e.g. Python package projects, VueJS projects.

Cookiecutter A command-line utility that creates projects from cookiecutters (project templates), e.g. creating a Python package project from a Python

null 18.6k Dec 30, 2022
Dead simple CLI tool to try Python packages - It's never been easier! :package:

try - It's never been easier to try Python packages try is an easy-to-use cli tool to try out Python packages. Features Install specific package versi

Timo Furrer 659 Dec 28, 2022