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 Overflow
๐ŸŒ
Visual Studio Code
code.visualstudio.com โ€บ docs โ€บ python โ€บ formatting
Formatting Python in VS Code
November 3, 2021 - 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...
๐ŸŒ
Visual Studio Code
code.visualstudio.com โ€บ docs โ€บ python โ€บ editing
Editing Python in Visual Studio Code
November 3, 2021 - Tip: you can assign a keyboard shortcut to the editor.action.organizeImports command. For help with common IntelliSense and Python editing issues, check the table below:
๐ŸŒ
Mkyong
mkyong.com โ€บ home โ€บ vscode โ€บ how to format source code in visual studio code (vscode)
How to format source code in Visual Studio Code (VSCode) - Mkyong.com
November 4, 2021 - formatting vscode, shortcuts, Windows `Shift + Alt + F`, macOS `Shift + Option + F` and Linux `Ctrl + Shift + I`
๐ŸŒ
Dave Gray
davegray.codes โ€บ posts โ€บ how-to-auto-format-unwanted-python-line-indents
How to Auto-Format Unwanted Python Line Indentations in VS Code | Dave Gray
February 10, 2024 - I then choose "Python" and the file auto-formats. Then I tab twice again and save the file with Ctrl+S. The file once again formats because I have Format On Save selected in my settings. Shortly after publishing my course, VS Code changed how it handles auto-formatting.
Find elsewhere
๐ŸŒ
DEV Community
dev.to โ€บ adamlombard โ€บ how-to-use-the-black-python-code-formatter-in-vscode-3lo0
VSCode: Using Black to automatically format Python - DEV Community
April 4, 2024 - Images from VSCode using Hyper Term Theme. ... ๐Ÿ‘‹ Hey there, I am Waylon Walker I am a Husband, Father of two beautiful children, Senior Python Developer currently working in the Data Engineering platform space. I am a continuous learner, and sha ... I generally need to pass in the black path as well. This might be compatibility issues with wsl. ... "python.formatting.provider": "black", "python.formatting.blackPath": "/home/<user>/.local/bin/black",
Top answer
1 of 8
22

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:

2 of 8
19

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.

๐ŸŒ
Visual Studio Marketplace
marketplace.visualstudio.com โ€บ items
Formatter extension for Visual Studio Code using autopep8
January 8, 2025 - Extension for Visual Studio Code - Formatting support for Python files using the autopep8 formatter.
๐ŸŒ
codestudy
codestudy.net โ€บ blog โ€บ how-do-you-format-code-in-visual-studio-code-vscode
How to Format Code in Visual Studio Code (VSCode): Windows Equivalent of Ctrl+K+D and Ctrl+K+F โ€” codestudy.net
Click the pencil icon and press Ctrl+K, then F to set the shortcut. Save your changes. If VSCode shows โ€œNo formatter installed for [language]โ€: Install a formatter extension for the language (e.g., Prettier for JavaScript, Black for Python).
๐ŸŒ
DEV Community
dev.to โ€บ eegli โ€บ quick-guide-to-python-formatting-in-vs-code-2040
Quick Guide to Python Formatting in VS Code (2025) - DEV Community
February 1, 2025 - I prefer applying this configuration to my user settings - this way, I can easily start a Python project and will get formatting out of the box based on my user settings. No need to create dedicated workspace settings for every single project and override the formatter over and over. Here's the relevant config from my settings: { "editor.defaultFormatter": "esbenp.prettier-vscode", "[python]": { "editor.defaultFormatter": null }, "python.formatting.blackArgs": ["--line-length", "120"], "python.formatting.provider": "black", } It's been 5 years since this was originally posted.
๐ŸŒ
GitHub
github.com โ€บ microsoft โ€บ vscode-black-formatter
GitHub - microsoft/vscode-black-formatter: Formatting support for Python using the Black formatter ยท GitHub
Integrated formatting: Once this extension is installed in VS Code, Black will be automatically available as a formatter for Python. This is because the extension ships with a Black binary. You can ensure VS Code uses Black by default for all your Python files by setting the following in your User settings (View > Command Palette...
Starred by 190 users
Forked by 45 users
Languages ย  Python 88.5% | TypeScript 11.1% | JavaScript 0.4%
๐ŸŒ
Visual Studio Marketplace
marketplace.visualstudio.com โ€บ items
Black Formatter - Visual Studio Marketplace
Extension for Visual Studio Code - Formatting support for Python files using the Black formatter.