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.
Enable "Format On Save" by setting
"editor.formatOnSave": true
And since version 1.49.0 editor.formatOnSaveMode has the option modifications that will just format the code you modified. Great when you change someone else code.
You can also set it just for one specific language:
"[python]": {
"editor.tabSize": 4,
"editor.insertSpaces": true,
"editor.formatOnSave": true #
},
Since version 1.6.1, Vscode supports "Format On Save". It will automatically use a relevant installed formatter extension to format the whole document.
If you are modifying other users code and your team don't standardize a formatter, a nice option also is "editor.formatOnSaveMode": "modifications",. Unfortunately, the excellent black formatter does not support this feature.
Below are the steps to change the VS Code auto format on save settings:
- Use [Ctrl]+[Shift]+[p]
- Type "Preferences"
- Select "Preferences: Open User Settings"
- Search for "format"
- Change "Editor: Format On Save" or "Editor: Format On Paste".
There are also Keyboard Shortcuts for formatting in VS Code. For instance, the default to format selected code should be [Ctrl]+K [Ctrl]+F (type both hotkeys in succession).
Below are the steps to change the auto format hotkey settings:
- Use [Ctrl]+[Shift]+[p]
- Type "Keyboard"
- Select "Preferences: Open Keyboard Shortcuts"
- Search for "format"
- Change "Format Selection" or "Format Document".
There are only a few settings you need to setup black as a formatter on VS Code. It seems you got most of it right, but I am doubtful about using relative paths for blackPath (VS Code should have shown an error if the path is indeed incorrect).
I suggest switching to absolute paths.
Here are my settings:
// User Settings
"editor.defaultFormatter": null,
"editor.formatOnSave": false, // enable per language
"[python]": {
"editor.formatOnSave": true
},
"python.formatting.provider": "black",
"python.formatting.blackPath": "/usr/local/bin/black"
// Workspace Settings
"python.formatting.blackPath": "/absolute/path/to/venv/with/black",
"python.formatting.blackArgs": [
"-l 120"
],
First of all, I suggest getting rid of the editor.defaultFormatter setting (or just set it back to the default null). Instead of setting a default for everything, configure your formatter for each language and for each extension. Here, it's null and then I configure python-specific settings then I have separate ones for other languages (ex. JS and C++). You mentioned something about Prettier, and that could be interfering with VS Code using black.
Second, make sure you are modifying the correct settings. VS Code has 3 sets of settings: User, Workspace, and Folder. I normally have the formatOnSave enabled for Python on the User settings, and provider set to black (using system-wide installed black). On a specific workspace, I have a virtual environment and I override the blackPath to the black specifically installed on that virtual environment. You can also just put all the settings in the User settings or use the same system-wide-installed black. But the main point here is to use absolute paths for both (basically copying the output of which black from the console).
Note that, if you specified blackPath to point to a particular virtual environment, make sure to select that same virtual environment on your workspace.
Lastly, you can check for any issues from the Output tab > Python:

I was also having the issue. What solved the issue for me was after downloading the Black formatter extension and once the document was open I did a right-click with the mouse and then press "format document". Choose black. I was good to go and had no more issues after that.