Week 2 · Python for real work and SQL
Setting up VS Code for Python
Install the Python, Ruff and mypy extensions, and set VS Code to tidy your Python code every time you save.
A few VS Code extensions catch mistakes while you type and keep your code neat without any effort from you. Set them up once and they work in every project.
Step 1: Install the extensions
Open the Extensions view (⌘⇧X on macOS, Ctrl Shift X on Windows). Search for each of these and click Install. Check the publisher name, because there are lookalikes:
| Extension | Publisher | What it does |
|---|---|---|
| Python | Microsoft | Runs, explains and autocompletes Python. You installed it in the setup guide, so just check it's there. |
| Ruff | Astral Software | Points out mistakes and tidies your code's layout |
| Mypy Type Checker | Microsoft | Checks that values are used as the right type, for example that you don't add text to a number |
Step 2: Turn on Ruff when you save
Ruff catches things like:
- imports you never use
- variables you created but forgot to use
- messy spacing and layout
- a handful of common mistakes
To have Ruff fix what it can every time you save, add some settings to VS Code:
- Open the Command Palette (⌘⇧P on macOS, Ctrl Shift P on Windows).
- Type
user settings jsonand choose Preferences: Open User Settings (JSON). - Add the
"[python]"block below, then save the file.
{
"[python]": {
"editor.formatOnSave": true,
"editor.defaultFormatter": "charliermarsh.ruff",
"editor.codeActionsOnSave": {
"source.fixAll.ruff": "explicit",
"source.organizeImports.ruff": "explicit"
}
}
}
If the file already has settings in it, don't paste a second pair of outer curly braces { }. Add a comma after the last existing setting, then paste only the "[python]": { ... } part inside the existing braces.
These are your user settings, so they apply to every project you open. Don't put them in a project's settings instead: projects can come with their own settings, which would replace yours.
Step 3: Try it
In your learn-python project, replace the contents of main.py with this messy code and save it:
import os
import json
def main():
total=4500+12800
unused = 5
print( "Total:",total )
main()
When you save, Ruff:
- deletes the two imports you don't use
- fixes the spacing, so
total=4500+12800becomestotal = 4500 + 12800 - underlines
unusedto warn you it's never used, but leaves it for you to decide
Run it with uv run main.py to check it still prints Total: 17300.
Stuck?Show hints
- Nothing changes when you save: check there's no red underline in your settings file. A missing comma or brace stops VS Code reading it. Also check the Ruff extension is installed and enabled.
- VS Code asks which formatter to use: choose Ruff.