Home > System Tutorial > LINUX > A guide to writing Python correctly using VSCode

A guide to writing Python correctly using VSCode

WBOY
Release: 2023-12-30 20:05:33
forward
850 people have browsed it

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

When 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
Copy after login

A guide to writing Python correctly using VSCode

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",
A guide to writing Python correctly using VSCode

3. Several simple configurations

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
Copy after login

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

"files.autoSave": "afterDelay"
"files.autoSaveDelay": 1000
Copy after login

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!

source:linuxprobe.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template