Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configuring lsp-pyright for anaconda #52

Open
SreenivasVRao opened this issue Apr 30, 2021 · 4 comments
Open

Configuring lsp-pyright for anaconda #52

SreenivasVRao opened this issue Apr 30, 2021 · 4 comments

Comments

@SreenivasVRao
Copy link

This is my pyrightconfig.json

{
  "exclude": ["images/**", "**/__pycache__"],

  "ignore": ["images/**", "**/__pycache__"],

  "venvPath": "/home/sreenivas/anaconda3/envs",
  "venv": "colorize",

  "reportMissingImports": true,
  "reportMissingTypeStubs": false,

  "pythonVersion": "3.7",
  "pythonPlatform": "Linux"
}

With doom emacs (lsp, pyright, conda, direnv), I'm running into issues getting my pyright path to work.

I think I traced this down to https://github.com/emacs-lsp/lsp-pyright/blob/master/lsp-pyright.el#L159-L176 these two functions. I don't have a venv in my path, I'm using anaconda3. It's on a separate partition from my project. (Project is on /media/data/.... while virtualenv is on /home/sreenivas/anaconda3)

I can override the two functions to do the job, but I would like some advice on how to do it in a neater manner.

(defun lsp-pyright-locate-venv ()
  "Look for virtual environments local to the workspace."
  (concat "/home/sreenivas/anaconda3/" "envs"))

(defun lsp-pyright-locate-python ()
  "Look for python executable cmd to the workspace."
  (or (executable-find (f-expand "bin/python" (concat (lsp-pyright-locate-venv) "/colorize")))
      (with-no-warnings
        (if (>= emacs-major-version 27)
            (executable-find lsp-pyright-python-executable-cmd lsp-pyright-prefer-remote-env)
          (executable-find lsp-pyright-python-executable-cmd)))))

Now it gives:

Setting pythonPath for service "CH": "/home/sreenivas/anaconda3/envs/colorize/bin/python"
@SreenivasVRao SreenivasVRao changed the title Configuring pyright for anaconda Configuring lsp-pyright for anaconda Apr 30, 2021
@seagle0128
Copy link
Collaborator

I think you just need this:

(setq lsp-pyright-venv-path "/home/sreenivas/anaconda3/envs/colorize")

@tranner
Copy link

tranner commented May 28, 2021

I have been thinking about how this might work for some time too. I cooked up this little script that helps me (I'm not a emacs-lisp expect btw but works for me!)

(with-eval-after-load 'conda
  (with-eval-after-load 'lsp-pyright
    (progn
      (defun autoenv-for-lsp-pyright ()
	(let* ((env-name (conda--infer-env-from-buffer))
	       (env-path (concat conda-env-home-directory "/envs/" env-name)))
	  (setq-local lsp-pyright-venv-path env-path)
	  (setq-local mode-line-process (concat "(" env-name ")"))
	  (message "setting lsp-pyright-venv-path to %s" env-path))
	)
      (push 'autoenv-for-lsp-pyright python-mode-hook)
      )))

@dsyzling
Copy link
Member

@SreenivasVRao Have you tried the pyvenv Emacs package? Then M-x pyvenv-workon to select the conda virtual directory - this is typically how I work.

@kevindragon
Copy link

Have you tried to set python.pythonPath

(with-eval-after-load 'lsp-pyright
 (progn
   (lsp-register-custom-settings
     `(("python.pythonPath" "D:/path/to/miniconda3/envs/stringle_pro/python.exe"))))
  )

I put above code to .dir-locals.el of my project, it's working, the content of .dir-locals.el like below:

((python-mode . ((eval . (with-eval-after-load 'lsp-pyright
                           (progn
                             (lsp-register-custom-settings
                              `(("python.pythonPath" "D:/path/to/miniconda3/envs/stringle_pro/python.exe"))))
                           )))))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants