Skip to content

Instantly share code, notes, and snippets.

@linw1995
Last active April 2, 2021 06:49
Show Gist options
  • Save linw1995/4b568b29c02ca645f92c196ccfc1cdc1 to your computer and use it in GitHub Desktop.
Save linw1995/4b568b29c02ca645f92c196ccfc1cdc1 to your computer and use it in GitHub Desktop.
Get PDM to work with lsp-python-ms in Emacs

Get PDM to work with lsp-python-ms in Emacs

A code snippet to show how to use PDM to work with lsp-python-ms in Emacs.

;; TODO: Cache result
(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 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")))

(use-package lsp-python-ms
  :ensure t
  :init (setq lsp-python-ms-auto-install-server t)
  :hook (python-mode
         . (lambda ()
             (setq lsp-python-ms-python-executable (linw1995/pdm-get-python-executable))
             (setq lsp-python-ms-extra-paths (vector (linw1995/pdm-get-packages-path)))
             (require 'lsp-python-ms)
             (lsp))))  ; or lsp-deferred
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment