Install the below python packages for Python Code Format supporting in Windows
pip install autopep8
pip install pylint
Once you have successfully installed the packages, Open the code in vs code -> Press Ctrl + A and Ctrl + K, it will format the code.
Answer from Dhandapani Sudhakar on Stack OverflowInstall the below python packages for Python Code Format supporting in Windows
pip install autopep8
pip install pylint
Once you have successfully installed the packages, Open the code in vs code -> Press Ctrl + A and Ctrl + K, it will format the code.
autopep8 is listed as a requirement. On mac, using brew, you can install it as follows:
brew install autopep8
Videos
Or simply install Black Formatter from VSC extension menu:
https://marketplace.visualstudio.com/items?itemName=ms-python.black-formatter

:)
Based on the comments by @starball, @jarmod and additional googling I found that you need to follow those steps:
Step 1. Install Python extension from marketplace: https://marketplace.visualstudio.com/items?itemName=ms-python.python
Step 2. Install one of the formatter packages for Python.
The Python extension supports source code formatting using either autopep8 (the default), black, or yapf.
from and More about it here: https://code.visualstudio.com/docs/python/editing#_formatting
Step 3. Select which code formatter you want to use in python.formatting.provider which is in settings>Extensions>Python (this maybe automatically set after step 1 and step 2). Also, in settings>Extensions>Python there are more options to select.
How to use formatting:
The code formatting is available in Visual Studio Code (VSCode) through the following shortcuts or key combinations:
On Windows Shift + Alt + F
On macOS Shift + Option + F
On Linux Ctrl + Shift + I
Format Selection (Ctrl+K Ctrl+F) - Format the selected text.
Or you can use right click menu:

from: https://mkyong.com/vscode/how-to-format-source-code-in-visual-studio-code-vscode/
and from: https://code.visualstudio.com/docs/editor/codebasics#_formatting
Once VSCode Python's format feature came from the python package, VSCode now offers smarter and more customizable extensions.
autopep8 and Black formatter were provided by Microsoft.
Ruff and yapf were provided by community.
Once you install a formatter extension, you can select it as the default formatter for Python files in VS Code by following the steps below:
- Open a Python file in VS Code.
- Right-click on the editor to display the context menu.
- Select Format Document With....
- Select Configure Default Formatter... from the drop-down menu.
- Select your preferred formatter extension from the list.
You could read document about Formatting in vscode-python for more details.
"[python]": {
"editor.codeActionsOnSave": {
"source.fixAll": "explicit"
},
"editor.defaultFormatter": "charliermarsh.ruff",
"editor.formatOnSave": true
}
I use Ruff and those are the settings for python files. For me the editor.codeActionsOnSave did the trick.
As said there is the Python extension which now do it out of the box, but still don't do a great job, and an example is when you copy and past a whole block into a function or so. It just indents the first line, and that's not a good behavior. Here are two good helpful solutions:
- indent a whole block manually: select the whole block, and then click Tab. If you want to indent backward, you do it with Shift+Tab. That's it, and I think that can be useful in several places.
- Python auto indent extension (https://marketplace.visualstudio.com/items?itemName=hyesun.py-paste-indent). It solves the problem when pasting. Just see how it works in the link. Now about setting it up: You need to set just one keybinding for the command "pyPasteIndent.pasteAndIndent" provided by the extension. Once done, you will have your own shortcut to paste and indent automatically (I have set it to Alt + P)
Here is how: - Ctrl+SHIFT+P to open the command palette, then write "key"*, choose *"open keyboard shortcut", and then the keybinding page open, which it's the nice interface for the **keybindings.json. You can open keybindings.json the same way and by choosing "open keyboard shortcut file" (in place of just "open keyboard shortcut"). Give it a look if never have. But here I will go with the nice interface. Know also that you can open that going menu File โ Preference โ Keyboard Shortcut.
In the keybinding window, in the search bar, paste pyPasteIndent.pasteAndIndent, and then click the + button to add the shortcut and create the keybinding.
The image below shows well how it's done:

You can install the Visual Studio Code Python extension which will provide intellisense, auto-completion, code formatting, and debugging.
Here is more information on the Python extension, here.