[Python] I immediately tried using Pylance's VS Code extension.

An extension called Pylance that can be used with VS Code etc. was released, so I tried using it immediately.

Reference: Accelerate Python development with type hints-Microsoft announces extension "Pylance" for VS Code

What kind of extension (roughly)?

--It is an extension of Python made by Microsoft. --Python input completion, which is included in Python extensions, is enabled. This doesn't change much because most people who write Python code using VS Code will have Python extensions. --Similar to the Python type check Pyright I wrote earlier, the completion will work by writing the code and annotating the type in the place where the type mistake or completion is difficult to work (Pyright internally). It is used). --Pyright's article: Type annotations for richer input completion and more robust Python code and an introduction to Pyright --It will check for errors where imports are missing and give you the option to insert the missing imports into your code. --As of 2020-07-02, when this article is written, it is still in the Preview version.

Installation method

Just search for pylance etc. on the VS Code extension page and install it.

image.png

However, in my case, I had the Pyright extension installed in advance, but I got an error when I installed Pylance, probably because of a conflict. After disabling the Pyright extension and restarting VS Code, the error went away.

Enable type checking

By default, the type checking feature is disabled. To enable it, open the VS Code settings screen, search for "python.analysis.typeCheckingMode" and switch the settings to basic or strict.

image.png

basic seems to be a slightly loose check, such as checking whether it is correct at the place where the type annotation is done.

In strict, it seems that the part itself that does not have type annotation etc. also causes an error. If it is strict from the beginning, there is no problem, but if you start type annotation from the middle and set strict, a lot of errors will appear in the existing code and it will be a bit painful: sweat:

Insert the corresponding import when there is an import error

Wasn't it in Pyright? As a function, a function that imports when you forget to import in Python code has been added to Pylance.

When I was working with this feature in a language other than Python, I used to use something similar, so I'm happy to be able to use it!

In cases where import is insufficient, an error will be displayed underlined in red on VS Code as shown below.

image.png

In that state, if you move the cursor position to the part where the error occurs (np in the image), an icon that looks like a light bulb will appear.

image.png

In this state, press Ctrl + . or click the light bulb icon to display a list of imports to insert. It's wise to recognize np without doing numpy ...!

image.png

If you select the appropriate one, the import statement will be inserted.

image.png

You can also use this function by clicking "Quick Fix ..." on the pop-up that appears when you mouse over.

image.png

Recommended Posts

[Python] I immediately tried using Pylance's VS Code extension.
I tried Python C extension
[Python] I tried using OpenPose
I tried input interpolation on UE4 Python VS Code
I tried using Thonny (Python / IDE)
[VS Code] ~ Tips when using python ~
[Python] I tried using YOLO v3
I tried using Bayesian Optimization in Python
I tried using UnityCloudBuild API from Python
vprof --I tried using the profiler for Python
I tried web scraping using python and selenium
I tried object detection using Python and OpenCV
I tried Flask with Remote-Containers of VS Code
Debug with VS Code using boost python numpy
I tried using mecab with python2.7, ruby2.3, php7
I tried reading a CSV file using Python
I tried using the Datetime module by Python
I tried using parameterized
I tried using mimesis
I tried using anytree
I tried using aiomysql
I tried using Summpy
I tried Python> autopep8
I tried using coturn
I tried using Pipenv
I tried using matplotlib
I tried using "Anvil".
I tried using Hubot
I tried using ESPCN
I tried using openpyxl
I tried using Ipython
I tried using PyCaret
I tried using cron
I tried using ngrok
I tried using face_recognition
I tried using Jupyter
I tried using PyCaret
I tried using Heapq
I tried using doctest
I tried Python> decorator
I tried using folium
I tried using jinja2
I tried using folium
I tried using time-window
I tried to execute Python code from .Net using Pythonnet (Hallo World edition)
I tried using TradeWave (BitCoin system trading in Python)
How to make a Python package using VS Code
[Python] I tried running a local server using flask
I tried drawing a pseudo fractal figure using Python
I tried using Python (3) instead of a scientific calculator
I tried to access Google Spread Sheets using Python
I tried fp-growth with python
I tried scraping with Python
[I tried using Pythonista 3] Introduction
I tried using easydict (memo).
I tried face recognition using Face ++
I tried using Random Forest
I tried using BigQuery ML
I tried using Amazon Glacier
Debug Python with VS Code
I tried using git inspector