emacs-lsp / lsp-pyright

lsp-mode :heart: pyright
https://emacs-lsp.github.io/lsp-pyright
GNU General Public License v3.0
284 stars 24 forks source link

Configuring lsp-pyright for anaconda #52

Open SreenivasVRao opened 3 years ago

SreenivasVRao commented 3 years ago

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"
seagle0128 commented 3 years ago

I think you just need this:

(setq lsp-pyright-venv-path "/home/sreenivas/anaconda3/envs/colorize")
tranner commented 3 years ago

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 commented 3 years ago

@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 commented 1 year ago

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"))))
                           )))))