emacs-lsp / lsp-pyright

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

How specify Python interpreter with elisp #67

Closed dev590t closed 2 years ago

dev590t commented 2 years ago

I'm trying to integrate lsp-pyright with PDM python package manager and doom-emacs. But I fails to make pyright to use my PDM python interpreter. It still use my system interpreter.

I have tried to use lsp-pyright-auto-search-paths and lsp-pyright-venv-path to specify python interpreter, but that doesn't work

(defun pdm-get-python-executable (&optional dir)
    (let ((pdm-get-python-cmd "pdm info --python"))
      (string-trim
       (shell-command-to-string
        (if dir
            (concat "cd "
                    dir
                    " && "
                    pdm-get-python-cmd)
          pdm-get-python-cmd)))))

(defun pdm-get-packages-path (&optional dir)
  (let ((pdm-get-packages-cmd "pdm run bash -c 'printenv PEP582_PACKAGES'"))
    (concat (string-trim
             (shell-command-to-string
              (if dir
                  (concat "cd "
                          dir
                          " && "
                          pdm-get-packages-cmd)
                pdm-get-packages-cmd)))
            "/lib")))

(add-hook! python-mode
  (setq lsp-pyright-extra-paths (vector (pdm-get-packages-path)))
  (setq lsp-pyright-auto-search-paths (pdm-get-python-executable))
  (setq lsp-pyright-venv-path (pdm-get-python-executable))
  (require 'lsp-pyright)
  (lsp)) ; or lsp-deferred
zealinux commented 2 years ago
;; (add-hook! python-mode
;;   (setq lsp-pyright-extra-paths (vector (pdm-get-packages-path)))
;;   (setq lsp-pyright-auto-search-paths (pdm-get-python-executable))
;;   (setq lsp-pyright-venv-path (pdm-get-python-executable))
;;   (require 'lsp-pyright)
;;   (lsp)) ; or lsp-deferred

(use-package lsp-pyright
  :ensure t
  :hook (python-mode . (lambda ()
                         (setq lsp-pyright-extra-paths (vector (pdm-get-packages-path)))
                         (setq lsp-pyright-auto-search-paths (pdm-get-python-executable))
                         (setq lsp-pyright-venv-path (pdm-get-python-executable))
                          (require 'lsp-pyright)
                          (lsp))))  ; or lsp-deferred
dev590t commented 2 years ago

I use doom emacs. It use a different mecanism to add package than classic emacs.

After reconfigure ~/.doom.d/init.el with (python +lsp +pyright +pyenv +poetry +cython). And install the packages in .venv. lsp-pyright can automatically find the package and the interpreter in .venv without any additional configuration.

Thanks you for your answer

worldofgeese commented 1 year ago

@dev590t and others who stumble upon this, @zealinux's code sample is correct for any Emacs distro other than Doom. To make it compatible with Doom, one just needs to add a ! after use-package. Here's what a complete, working example looks like with a very lightly altered function from @lin1995 incorporating a change from @tihuang02 given in this discussion:

(defun linw1995/pdm-get-python-executable (&optional dir)
    (let ((pdm-get-python-cmd "pdm info --python"))
      (string-trim
       (shell-command-to-string
        (if dir
            (concat "cd "
                    dir
                    " && "
                    pdm-get-python-cmd)
          pdm-get-python-cmd)))))

(defun linw1995/pdm-get-packages-path (&optional dir)
  (let ((pdm-get-packages-cmd "pdm info --packages"))
    (concat (string-trim
             (shell-command-to-string
              (if dir
                  (concat "cd "
                          dir
                          " && "
                          pdm-get-packages-cmd)
                pdm-get-packages-cmd)))
            "/lib")))

(use-package lsp-pyright
  :ensure t
  :hook (python-mode . (lambda ()
                         (setq lsp-pyright-extra-paths (vector (pdm-get-packages-path)))
                         (setq lsp-pyright-auto-search-paths (pdm-get-python-executable))
                         (setq lsp-pyright-venv-path (pdm-get-python-executable))
                          (require 'lsp-pyright)
                          (lsp)))) 

@frostming, @zealinux's solution is a great code sample that gets PDM using PEP 582 working with Pyright in Emacs.

Many thanks to @zealinux for his generous offering :heart: