Teach you how to use VSCode correctly to write Python

We will find that there will be many warnings about formatting issues in the code when writing. So how do you automatically format your code? This is what will be introduced in this issue.

1. Configure flake8

After installing flake8, when writing code, the editor will prompt you with errors, and will also prompt if the code format is not standardized. Open the command line, VSCode can run the terminal directly, press the shortcut key Ctrl + ` and enter "pipinstallflake8". After successfully installing flake8, open File -> Preferences -> Settings, find the two settings for the static code prompt in settings.json, and Modify as follows:

"python.linting.pylintEnabled": false
"python.linting.flake8Enabled": true

Teach you the correct use of VSCode to write Python. Teach you the correct use of VSCode to write Python.

2. Configure yapf

After installing yapf, press Alt+Shift+F in VScode to automatically format the code. Open the command line as above. After successfully installing yapf, enter "pip install yapf", open File->Preferences->Settings, find this setting in settings.json, and modify it as follows: "python.formatting.provider": "yapf",

Teach you the correct use of VSCode to write Python. Teach you the correct use of VSCode to write Python.

3. Several simple configurations

When saving the file, a new line is automatically inserted at the end, because Python's format has a new line at the end of the program.

"files.insertFinalNewline": true

The file is automatically saved and the delay time can be set.

"files.autoSave": "afterDelay"
"files.autoSaveDelay": 1000

The latter one is to set the delay time, here it is set to 1000ms.

Guess you like

Origin blog.csdn.net/yaxuan88521/article/details/133430878