🚀 *Automatically Format Python Code on Save in VS Code!*
Tired of fixing code formatting manually? In this quick and easy tutorial, I’ll show you how to *automatically format Python code on save* in *Visual Studio Code (VS Code)* using tools like **Black**, **Pylance**, and **Prettier**. 🛠️
Whether you’re working on a big project or just starting out, this guide will help keep your code clean and consistent every time you hit **Save**! 📝
---
🔹 *What You’ll Learn:*
✅ How to set up *Python auto-formatting* in VS Code
✅ Installing popular formatters like *Black* and *Autopep8*
✅ Configuring *VS Code settings* for formatting on save
✅ Customizing formatting rules (line length, indentation, etc.)
✅ Troubleshooting formatting issues
---
🔹 *Prerequisites:*
✔️ Visual Studio Code installed ([Download Here](https://code.visualstudio.com/))
✔️ Python extension installed in VS Code
✔️ Basic understanding of Python syntax
---
🔹 *Step 1: Install Required Extensions*
1️⃣ Open VS Code
2️⃣ Go to *Extensions* (`Ctrl + Shift + X`)
3️⃣ Search and install the following:
*Python* (by Microsoft)
*Pylance*
*Black Formatter* or *Autopep8*
---
🔹 *Step 2: Configure Auto-Formatting on Save*
1️⃣ Open VS Code *Settings* (`Ctrl + ,`)
2️⃣ Search for `Format On Save`
3️⃣ Enable the checkbox ✅
---
🔹 *Step 3: Set a Default Python Formatter*
1️⃣ Open *Command Palette* (`Ctrl + Shift + P`)
2️⃣ Search for `Preferences: Open Settings (JSON)`
3️⃣ Add the following settings:
```json
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter",
"editor.formatOnSave": true
}
```
---
🔹 *Step 4: Customize Formatter Settings (Optional)*
For *Black* formatter, you can add specific rules:
```json
"python.formatting.blackArgs": ["--line-length", "88"]
```
For *Autopep8* users:
```json
"python.formatting.provider": "autopep8"
```
---
🔹 *Step 5: Test Your Auto-Formatting*
1️⃣ Write some unformatted code:
```python
def hello(): print("Hello, World!")
```
2️⃣ Save the file (`Ctrl + S`)
3️⃣ VS Code will automatically format the code to:
```python
def hello():
print("Hello, World!")
```
---
🔹 *Troubleshooting Common Issues:*
❌ *Formatting not working?*
✔️ Make sure the correct Python interpreter is selected
❌ *Error: Formatter not installed?*
✔️ Run `pip install black` or `pip install autopep8`
---
🔹 *More VS Code Tutorials:*
📌 How to Set Up Python Debugging in VS Code → [Watch Now]
📌 Create a Flask App in VS Code → [Watch Now]
📌 Connect MySQL in VS Code → [Watch Now]
---
👍 *Like, Share & Subscribe!*
If this video helped you, *LIKE**, **SHARE**, and **SUBSCRIBE* for more Python tips, VS Code tutorials, and developer hacks! 💻
💬 *Questions?* Drop them in the comments—I’ll help you troubleshoot!
---
🔹 *Hashtags:*
#Python #VSCode #AutoFormat #PythonFormatting #BlackFormatter #Autopep8 #VisualStudioCode #PythonTips #CodeFormatting #PythonVSCode
На этой странице сайта вы можете посмотреть видео онлайн How to Format Python Code on Save in Visual Studio Code | Auto-Format Python in VS Code длительностью часов минут секунд в хорошем качестве, которое загрузил пользователь ProgrammingKnowledge 21 Февраль 2025, поделитесь ссылкой с друзьями и знакомыми, на youtube это видео уже посмотрели 14,398 раз и оно понравилось 125 зрителям. Приятного просмотра!