python - Unable to setup lsp-mode with lsp-pyright - Emacs Stack Exchange
python language server - How to use lsp-pyright in emacs? - Stack Overflow
python lsp server - lsp-pyright with portable setup - Emacs Stack Exchange
python - How to set up pyrightconfig in lsp-pyright? - Emacs Stack Exchange
Videos
You can include additional Python source paths with lsp-pyright-extra-paths. Here is my pyrightconfig.json for a project.
{
"exclude": ["**/node_modules", "**/__pycache__"],
"ignore": ["**/node_modules", "**/__pycache__"],
"include": ["flextensor", "tvm"]
"pythonPlatform": "Linux",
"pythonVersion": "3.6",
"reportMissingImports": true,
"reportMissingTypeStubs": false,
"stubPath": "typings",
"typeCheckingMode": "basic",
"venvPath": "/home/swarnendu/tmp/virtualenvs",
"venv": "flextensor-venv"
}
Here is my Emacs setup (init.el) for pyright (ignoring remote Tramp setup). You could also use use-package for this.
(when (executable-find "pyright")
(unless (fboundp 'lsp-pyright-locate-python)
(autoload #'lsp-pyright-locate-python "lsp-pyright" nil t))
(unless (fboundp 'lsp-pyright-locate-venv)
(autoload #'lsp-pyright-locate-venv "lsp-pyright" nil t))
(add-hook 'python-mode-hook (lambda ()
(require 'lsp-pyright)
(lsp-deferred)))
(defvar lsp-pyright-python-executable-cmd)
(setq lsp-pyright-python-executable-cmd "python3")))
Here is the .dir-locals.el for the same project.
(
(python-mode . (
(pyvenv-activate . "/home/swarnendu/tmp/virtualenvs/flextensor-venv")
(eval . (let (
(paths
(vconcat (list
(expand-file-name "flextensor" (projectile-project-root))
(expand-file-name "tvm" (projectile-project-root))
))))
(setq lsp-pyright-extra-paths paths)))
))
)
Please check whether a similar setup works for you.
Have you checked if your pyrightconf.json file is actually found ?
You can check by skimming the log buffer for the lsp session
C-x C-b should give you a list of buffers
You can look for "window" as a keyword. In fact if the file is found or not is sent by the server in a "window" type message
- If your conf file is found, than it's a matter of properly filling it in
- If it's not, it's a matter of adding/removing roots from your lsp session (
lsp-workspace-folder-add/remove)
A new to me LSP server for Python has appeared: basedpyright. This is a fork of the fast pyright langserver which Microsoft develops, with a more OSS philosophy. It has lots of improvements bringing it close to (and in some cases surpassing) the proprietary, MS VSCode-only LSP server pylance which wraps pyright:
Basedpyright is a fork of pyright with various type checking improvements, improved vscode support and pylance features built into the language server.
You can read about all the improvements over pyright. The one most meaningful to me is "docstrings for compiled builtin modules". E.g. docs with eglot go from:
pyright:
class range(
stop: SupportsIndex,
/
)to
basedpyright:
class range(
stop: SupportsIndex,
/
)
range(stop) -> range object
range(start, stop[, step]) -> range object
Return an object that produces a sequence of integers from start (inclusive)
to stop (exclusive) by step. range(i, j) produces i, i+1, i+2, ..., j-1.
start defaults to 0, and stop is omitted! range(4) produces 0, 1, 2, 3.
These are exactly the valid indices for a list of 4 elements.
When step is given, it specifies the increment (or decrement).
Recent eglot versions already support it: just pip install basedpyright. Works fine with lsp-booster and all the normal settings.
I've used pyright for the last couple of month, but maybe there is a better options? Share your opinions
