[PYTHON] Use pyright with Spacemacs

Of lsp-mode which is the key to LSP in Spacemacs lsp-pyright allows you to use the Python type checker pyright I recently learned about it, so I use it

(There is a theory that the conventional pyls is fine, but recently I felt something heavy, so I'm trying pyrit, which has a reputation for being fast)

pyright installation

If you are using node, install pyright with npm

$ npm install -g pyright

Specify Python Layer LSP server as pyrit in Spacemacs

In the layer setting of .spacemacs, it says to write like this, so write it. It seems that it can be specified as an LSP server

.spacemacs(Excerpt)


(defun dotspacemacs/layers ()
  (setq-default
   dotspacemacs-configuration-layers
   '(
     (python :variables 
      python-backend 'lsp 
      python-lsp-server 'pyright)
    )
  )
)

cf. https://github.com/syl20bnr/spacemacs/tree/develop/layers/%2Blang/python

There seems to be no need to set up jedi on the Spacemacs side

Create a configuration file under the root of the LSP project

When you open the Python file, you will be asked to decide the project root of LSP, but create pyrightconfig.json in the project root.

I did it on Ubuntu18.04LTS on WSL2 on Windows10, but if you put it in the project root v2project / with the user name v2okimochi, it looks like this

pyrightconfig.json


{
  "venvPath": "/home/v2okimochi/.local/share/virtualenvs/v2project-ABCDEFGH",
  "stubPath": "",
  "executionEnvironments": [
    {
      "root": "src"
    },
    {
      "root": "tests",
      "extraPaths": [
        "src"
      ]
    }
  ]
}

--Specify venvPath if you are using pipenv or pyenv --It seems to be around .local/share/virtualenvs/ under the home directory --This may also be useful [Use Spacemacs for Python auto-completion, linter, etc .-- Make Spacemacs recognize the python virtual environment](https://qiita.com/v2okimochi/items/db178969165e72d2032f#spacemacs%E3%81%A7python % E4% BB% AE% E6% 83% B3% E7% 92% B0% E5% A2% 83% E3% 82% 92% E8% AA% 8D% E8% AD% 98% E3% 81% 95% E3 % 81% 9B% E3% 82% 8B) --Specify stubPath empty to eliminate unnecessary errors --typings is not a valid directory I get an error and it's annoying (although it seems harmless) cf. [typings.pyfile inside a project causes pyright to report that this is not a valid directory. ](https://github.com/microsoft/pyright/issues/777) --Specify the root directory to be read by pyright --The above example is when the project root hassrc /andtests / --If you do not specifysrc in tests / , you will not be able to refer to the source code of src / in the source code of tests / `.

You may not be able to read the 3rd party library

When I tried to use boto3, I got an import unresolved error.

A similar error in VS Code seems to have been fixed. .. .. cf. all third party imports not resolved in VS Code

For the time being, I installed it secretly with pipenv run pip install boto3 and it solved it, but I feel something different (if it is written in the Pipfile but it is not installed, that is annoying)

Recommended Posts

Use pyright with Spacemacs
Use pyright with CentOS7, emacs lsp-mode
Use mecab-ipadic-neologd with igo-python
Use RTX 3090 with PyTorch
Use ansible with cygwin
Use pipdeptree with virtualenv
[Python] Use JSON with Python
Use Mock with pytest
Use indicator with pd.merge
Use Gentelella with django
Use mecab with Python3
Use tensorboard with Chainer
Use DynamoDB with Python
Use Python 3.8 with Anaconda
Use python with docker
Use TypeScript with django-compressor
Use LESS with Django
Use MySQL with Django
Use Enums with SQLAlchemy
Use tensorboard with NNabla
Use GPS with Edison
Use nim with Jupyter
Use Trello API with python
Use "$ in" operator with mongo-go-driver
Use custom tags with PyYAML
Use TensorFlow with Intellij IDEA
Use Twitter API with Python
Use pip with Jupyter Notebook
Use DATE_FORMAT with SQLAlchemy filter
Use TUN / TAP with Python
Use sqlite3 with NAO (Pepper)
Use sqlite load_extensions with Pyramid
Use Windows 10 fonts with WSL
Use chainer with Jetson TK1
Use SSL with Celery + Redis
Use Cython with Jupyter Notebook
Use Maxout + CNN with Pylearn2
Use WDC-433SU2M2 with Manjaro Linux
Use OpenBLAS with numpy, scipy
Use subsonic API with python3
Use Sonicwall NetExtener with Systemd
Use prefetch_related conveniently with Django
Use AWS interpreter with Pycharm
Use Bokeh with IPython Notebook
Use Python-like range with Rust
Python: How to use async with
Use Azure SQL Database with SQLAlchemy
Use PointGrey camera with Python (PyCapture2)
Use vl53l0x with Raspberry Pi (python)
Use PX-S1UD / PX-Q1UD with Jetson nano
Use the preview feature with aws-cli
How to use virtualenv with PowerShell
[Python] Use Basic/Digest authentication with Flask
Use NAIF SPICE TOOLKIT with Python
Use rospy with virtualenv in Python3
Use markdown with jupyter notebook (with shortcut)
Use Python in pyenv with NeoVim
Use Tensorflow 2.1.0 with Anaconda on Windows 10!
How to use FTP with Python
Use Windows 10 speech synthesis with Python
Use curl / jq library with Go