lsp-pyright icon indicating copy to clipboard operation
lsp-pyright copied to clipboard

[WIP] Add support for detecting venvs from pipenv and poetry projects

Open truls opened this issue 4 years ago • 7 comments

This PR adds support for detecting if a project is managed using either poetry or pipenv and configuring pyright to use the venv and python interpreter created by those tools.

I have been using this code for a while without problems and I think it's extremely useful. However, there are a couple of problems with the code, thus the WIP tag:

  • The synchronous subprocess invocation causes emacs to freeze briefly when opening a file from a python project for the first time. Is there a way to do this asynchronously?
  • The code currently detects a pipenv project if Pipfile exists in the project root and pipenv is found in PATH. Likewise, poetry is assumed if pyproject.toml is found in the project root and poetry is found in PATH. This naive approach misses several obvious cases, but I wonder if there is a method or even need to make the detection mechanism more accurate or if it's better to add a customization variable that overrides the detection?

I'm looking forward to your feedback!

truls avatar Mar 17 '21 13:03 truls

Thanks for the PR man. I've trying to sort this out and it's been driving me nuts, lol. Does this load the packages from the venv itself? Following the configuration of pyright, If my understanding it correct, setting "python.venvPath" alone is not enough. We need to set the "python.venv" as well?

I tried doing that with something along the lines of the following and I am still unable to get the packages from the venv loaded:

(lsp-register-custom-settings
  '(("python.venvPath" "/")
    ("python.venv" (lambda () (file-relative-name (or (lsp-pyright-locate-venv) (poetry-get-virtualenv)) "/")))))))

ahmed-shariff avatar May 24 '21 00:05 ahmed-shariff

Thanks for looking at the PR!

So, if I understand you correctly, pyright isn't loading packages from the project venv when you use the current code? Which platform are you on?

I think the python.venv setting overrides the load paths that pyright gets from sys.path of the python interpreter used by the project. I'm not sure that you want to do that?

truls avatar May 25 '21 09:05 truls

So, if I understand you correctly, pyright isn't loading packages from the project venv when you use the current code? Which platform are you on?

yes, it doesn't load the packages from the project env. I am not sure if this is the correct way to test this, but I am checking this by running lsp-find-definition on one of the imports.

Looking at the pyright implementation itself:

    if (configOptions.venvPath !== undefined && configOptions.venv) {
    .
    .
    .
            const sitePackagesPath = findSitePackagesPath(fs, combinePaths(venvPath, libPath), importFailureInfo);

I thought pyright looks for the site-packge when both venv and venvPath are provided, right?

ahmed-shariff avatar May 26 '21 17:05 ahmed-shariff

@truls this looks interesting. Have you had issues like #66? Your PR might actually resolve that.

vakker avatar Mar 18 '22 12:03 vakker

It'd also be good to look at the $VIRTUAL_ENV environment variable if that's set; my direnv + pyenv setup sets that.

I think in general the way lsp-pyright finds the venv and python should be rethought a bit, since right now it silently assumes a default that's unlikely to be correct for most people.

deifactor avatar Jun 01 '22 21:06 deifactor

Hi, is this PR stale? I really need this feature.

flexagoon avatar Jan 15 '24 22:01 flexagoon

I created a PR to allow us to more easily configure how we choose the python executable - https://github.com/emacs-lsp/lsp-pyright/pull/89 - I wonder if we would be able to build on that to better support pipenv and poetry. For me personally all I need is for the default to be the python executable and ignore any .venv directory

jtamagnan-delphix avatar Feb 20 '24 23:02 jtamagnan-delphix