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 flake8When you write code after installing flake8, the editor will prompt you where there are errors, and it 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 and find the two settings for static code prompts in settings.json. And modify it as follows:
"python.linting.pylintEnabled": false "python.linting.flake8Enabled": true
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",
Automatically insert a new line at the end when saving the file, 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.
The above is the detailed content of A guide to writing Python correctly using VSCode. For more information, please follow other related articles on the PHP Chinese website!