Languages
Topics Overview JavaScript JSON HTML Emmet CSS, SCSS and Less TypeScript Markdown PowerShell C++ Java PHP Python Julia R Ruby Rust Go T-SQL C# .NET Swift Working with JavaScript Node.js Tutorial Node.js Debugging Deploy Node.js Apps Browser Debugging Angular Tutorial React Tutorial Vue Tutorial Debugging Recipes Performance Profiling Extensions Tutorial Transpiling Editing Refactoring Debugging Quick Start Tutorial Run Python Code Editing Linting Formatting Debugging Environments Testing Python Interactive Django Tutorial FastAPI Tutorial Flask Tutorial Create Containers Deploy Python Apps Python in the Web Settings Reference Getting Started Navigate and Edit Refactoring Formatting and Linting Project Management Build Tools Run and Debug Testing Spring Boot Modernizing Java Apps Application Servers Deploy Java Apps GUI Applications Extensions FAQ Intro Videos GCC on Linux GCC on Windows GCC on Windows Subsystem for Linux Clang on macOS Microsoft C++ on Windows Build with CMake CMake Tools on Linux CMake Quick Start C++ Dev Tools for Copilot Editing and Navigating Debugging Configure Debugging Refactoring Settings Reference Configure IntelliSense Configure IntelliSense for Cross-Compiling FAQ Intro Videos Get Started Navigate and Edit IntelliCode Refactoring Formatting and Linting Project Management Build Tools Package Management Run and Debug Testing FAQOn this page there are 6 sections
Formatting Python in VS Code
Formatting makes source code easier to read by human beings. By enforcing particular rules and conventions such as line spacing, indents, and spacing around operators, the code becomes more visually organized and comprehensible. You can view an example on the autopep8 page. Keep in mind, formatting doesn't affect the functionality of the code itself.
Linting helps to prevent errors by analyzing code for common syntactical, stylistic, and functional errors and unconventional programming practices. Although there is a little overlap between formatting and linting, the two capabilities are complementary.
Choose a formatter
Search the VS Code Marketplace for the formatter extension of your choice.
Microsoft publishes the following formatting extensions:
| autopep8 | https://marketplace.visualstudio.com/items?itemName=ms-python.autopep8 |
| Black formatter | https://marketplace.visualstudio.com/items?itemName=ms-python.black-formatter |
Formatter extensions offered by the community:
| Ruff | https://marketplace.visualstudio.com/items?itemName=charliermarsh.ruff |
| yapf | https://marketplace.visualstudio.com/items?itemName=eeyore.yapf |
Furthermore, below are formatter extensions that support import sorting:
| Ruff | https://marketplace.visualstudio.com/items?itemName=charliermarsh.ruff |
| isort | https://marketplace.visualstudio.com/items?itemName=ms-python.isort |
Note: If you don't find your preferred formatter in the table above or in the Marketplace, you can add support for it via an extension. You can use the Python Extension Template to integrate new Python tools into VS Code.
Set a default formatter
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.
Alternatively, you can set it as the default formatter for all Python files by setting "editor.defaultFormatter" in your User settings.json file, under a [python] scope. You can open settings.json with the Preferences: Open User Settings (JSON) command.
For example, to set Black Formatter as the default formatter, add the following setting to your User settings.json file:
{ "[python]": { "editor.codeActionsOnSave": { "source.organizeImports.ruff": "explicit" } } }Format your code
You can format your code by right-clicking on the editor and selecting Format Document, or by using the ⇧⌥F (Windows Shift+Alt+F, Linux Ctrl+Shift+I) keyboard shortcut.
You can also add the following setting to your User settings.json file to enable formatting on save for your code: