Coding

Python Code Syntax Highlighting in VS Code: Setup and Fixes

To get Python code syntax highlighting in VS Code, open a file that ends in .py, install the Python extension (ms-python.python) and check that the Status Bar says Python. If the colors are missing or look wrong, the cause is almost always the language mode or the active color theme.

This article covers each of those in order: how VS Code decides a file is Python, how to force it, how themes and semantic highlighting change the colors, and how to override individual colors yourself. The VS Code behavior described here is taken from the official documentation as of November 2026.

Install the Python extension

The VS Code Python quick start tells you to install VS Code, install a Python interpreter, then install the Python extension from the Visual Studio Marketplace. The extension ID is ms-python.python. The documentation says you need the extension "to get Python-specific features and UI."

To install it from inside the editor, open the Extensions view with Ctrl+Shift+X, search for ms-python.python, and select Install. If you also need the interpreter itself, see how to install Python on Windows.

Syntax highlighting is listed by the VS Code overview as a basic language feature ("Syntax highlighting and bracket matching"), so it does not depend on the extension in the same way that Quick Fixes or the Python commands do. Install the extension anyway. Most of the other things people expect from a Python editor sit behind it, and some themes and settings interact with the language features it provides.

To check that it is active, open the Command Palette with Ctrl+Shift+P and type Python: . The documentation says this lists the commands that the Python extension adds. If nothing appears, the extension is not installed or is disabled for this workspace.

Check the language mode in the Status Bar

VS Code does not look inside a file to decide how to color it. According to the language support overview, "we default the language support for a file based on its filename extension." Each language has an identifier, and for Python that is python. A file it cannot match gets the plaintext identifier, which has no coloring at all.

This is the usual reason for a file that is all one color. It happens when you:

  • open a new untitled tab and paste code before saving it;
  • save a script without an extension, or with an extension such as .txt;
  • open a Python file whose extension VS Code does not associate with Python.

Look at the right side of the Status Bar. If it says Plain Text, click it. The Select Language Mode dropdown opens. Type Python and pick it. The keyboard route is the Change Language Mode command, bound to Ctrl+K M on Windows and Linux (Cmd+K M on macOS). The colors appear immediately, and the choice lasts until you close the tab. Saving the file as something.py makes it permanent.

Identifiers are case sensitive. The documentation's own example is that Markdown is not the same as markdown, so use the lowercase python wherever a setting asks for an identifier.

Map unusual file extensions to Python

Some projects use their own extension for Python files, for example .pyw variants, .pyi stubs with a custom suffix, or build-system files with no meaningful extension. The files.associations setting maps a pattern to a language identifier. The documentation shows the shape with PHP:

"files.associations": {
    "*.myphp": "php"
}

The same setting for Python looks like this. The *.myscript pattern is a made-up example, so use your own:

"files.associations": {
    "*.myscript": "python"
}

Glob patterns work too. The overview page notes that "**/somefolder/*.*": "php" applies to every file in a folder, so "**/scripts/*.*": "python" would color everything in a scripts folder as Python. Open settings with Ctrl+,, search for files.associations, and use Add Item, or edit settings.json directly through Preferences: Open User Settings (JSON) in the Command Palette.

Change the color theme

If Python is colored but you dislike the colors, the theme decides them. Run Preferences: Color Theme from the Command Palette, or press Ctrl+K Ctrl+T (Cmd+K Cmd+T on macOS). You can also use File > Preferences > Theme > Color Theme. Arrow keys preview each theme live in the editor before you commit to one.

The choice is stored in your user settings as workbench.colorTheme. For example, "workbench.colorTheme": "Solarized Dark". For a walk through of popular themes and installing more from the Marketplace, see the VS Code themes guide.

A theme can look as if it has broken highlighting when it is simply low contrast. Switch to a built-in theme such as Dark+ first. If the problem disappears, the other theme is the cause and you have not misconfigured anything.

Syntax highlighting versus semantic highlighting

VS Code colors code in two layers, and knowing which one you are looking at explains many "wrong color" reports.

The first layer is grammar-based. TextMate grammars are the main tokenization engine, and the Semantic Highlight Guide states that they work "on a single file as input." They recognize keywords, strings, numbers and comments by pattern, without knowing what a name refers to.

The second layer is semantic highlighting. A language server supplies token information based on how symbols resolve across the project, and the editor applies those tokens on top of the grammar colors. The guide's example is a class name, a function name and a variable each getting its own color because the server knows what they are. This is why a name can change color a moment after you open a file: the language server has finished analysing it.

The setting that controls this layer is editor.semanticHighlighting.enabled. It accepts three values:

ValueEffect
trueSemantic highlighting is on for all themes.
falseSemantic highlighting is off for all themes.
configuredByThemeThe default. Each theme decides. Built-in themes such as Dark+ have it enabled.

If a theme looks flat compared with a screenshot you saw, the theme may have semantic highlighting disabled. Setting the value to true forces it on. If the extra colors distract you, false returns to grammar-only coloring.

"editor.semanticHighlighting.enabled": true

To override this for a single theme, the themes documentation says to use editor.semanticTokenColorCustomizations with "enabled": true.

Change individual colors for Python tokens

You can adjust colors without writing a theme. The editor.tokenColorCustomizations setting in your user settings.json accepts a preset list of token groups such as comments and strings. The documentation example scopes the change to one theme by putting the theme name in square brackets:

"editor.tokenColorCustomizations": {
    "[Monokai]": {
        "comments": "#229977"
    }
}

The same pattern works for Python files with any theme name you actually use. For finer control, you need to know which scope a piece of text belongs to. Put the cursor on the text and run Developer: Inspect Editor Tokens and Scopes. It shows information about the text at the cursor, and when semantic tokens are available and enabled by the theme, it also shows a semantic token type section. That tells you whether a color is coming from the grammar or from the language server, which decides which setting you need to change.

If highlighting is still wrong

  1. Confirm the language mode. The Status Bar should say Python. This fixes most cases.
  2. Switch to Dark+ temporarily. This rules out a theme or a token color override of your own.
  3. Remove your overrides. Comment out editor.tokenColorCustomizations and editor.semanticHighlighting.enabled in settings.json and see whether the colors return.
  4. Check the Python extension is enabled in the Extensions view for the current workspace, and reload the window with Developer: Reload Window.
  5. Look for real syntax errors. An unclosed string or bracket changes how everything after it is colored, and it is a genuine bug in the file. Python syntax error examples lists the common ones with the exact messages.

If you are new to the editor and want the wider setup, the VS Code tutorial for a first project covers creating and running a file from scratch.

Frequently asked questions

Why is my Python code all one color in VS Code?
The file is most likely in Plain Text mode. VS Code picks the language from the file extension, and a file with no .py extension (or an unsaved new tab) gets the plaintext identifier. Click the language name in the Status Bar and choose Python, or save the file as .py.
Do I need the Python extension for syntax highlighting?
Basic Python highlighting comes from the language support that ships with VS Code, so colors can appear without extra installs. The Python extension (ms-python.python) is what adds Python-specific features and UI, such as the Python commands in the Command Palette and Quick Fixes.
How do I change the colors of Python comments or strings?
Add editor.tokenColorCustomizations to your user settings.json, for example a comments color scoped to one theme. Run Developer: Inspect Editor Tokens and Scopes with the cursor on the text to see which scope it belongs to.
What is semantic highlighting in VS Code?
It is a second layer of coloring supplied by a language server, which understands how symbols resolve across the project. It is applied on top of the grammar-based highlighting. The editor.semanticHighlighting.enabled setting accepts true, false or configuredByTheme, which is the default.

Sources