Configure VS Code for Python — extensions, settings and the run button explained (and the typo Pylance spotted before I did)

My first program in VS Code was three lines long and it still failed. I pressed the run button, the terminal filled with red and at the bottom it said NameError: name 'pirnt' is not defined. I’d typed pirnt instead of print. The funny thing is that VS Code had already warned me: the word had been underlined with a wavy line for a while and I hadn’t looked. That day I understood that a well-configured editor isn’t a luxury: it’s a teammate that checks what you write while you write it.

Configuring VS Code for Python properly is the first thing you should do before writing a single line of code on your computer. When I started practising on my own, it took me days to get it comfortable because nobody told me which extensions to install or which settings to change. Here’s what I use, no fluff, with what each piece does and how to fix what usually goes wrong.

What is VS Code and why don’t we just use Python’s IDLE?

Python comes with its own editor, IDLE, but it’s basic: it colours the code and not much more. VS Code (Visual Studio Code) is a free, professional editor from Microsoft used by everyone from students to company teams. With the right extensions it autocompletes, flags errors before you run, debugs step by step, opens Jupyter notebooks and works with Git. During the degree you’ll use it for Python, C and Java, so learning it properly from the start is time well spent.

IDLEVS Code
AutocompleteVery basicComplete (Pylance)
Errors before runningNoYes, underlined
DebuggerSimpleBreakpoints, variables, call stack
.ipynb notebooksNoYes (Jupyter extension)
Other languages (C, Java)NoYes

VS Code and Visual Studio are two different programs. Visual Studio (without “Code”) is a huge environment for C# and C++; for your degree you want VS Code.

Before you start: what you need

Make sure you have:

  • Python installed and working in the terminal (python --version answers). If not, follow the guide on how to install Python on Windows first.
  • Windows 10 or 11 (VS Code also exists for macOS and Linux; the steps are almost identical).
  • A folder for your degree, for example C:\Users\YourName\GCID\FP1. In step 4 you’ll see why it matters so much.

If any of these sounds doubtful, sort it out before going on: 90% of VS Code problems come from a badly installed Python.

Anything that depends on Windows (installer, menus, terminal messages) is described as it appears in the official VS Code documentation and on my machine; the two Python programs in this article have been run and their output is real.

Step 1 — Download and install

Go to code.visualstudio.com and click the download button for Windows.

Visual Studio Code download page with the button to download the Windows version circled in red

Download the User Installer (the default one): it installs just for your user and doesn’t ask for administrator rights. The installation is next, next, next, and there are no tricks here like with Python, but on the additional tasks screen I recommend ticking:

  1. Add “Open with Code” action to the context menu for files and folders: you’ll be able to right-click a subject folder and open it directly.
  2. Add to PATH (ticked by default): lets you open VS Code from the terminal with code ..

With “Add to PATH” ticked, go into an assignment folder from the terminal and type code . (with the dot): VS Code opens with that folder. The dot means “the folder I’m in”.

Step 2 — Essential extensions to configure VS Code for Python

This is what really matters. VS Code without extensions is a tasteful text editor; with them, a programming environment. Press Ctrl + Shift + X (or the squares icon in the left bar), search for each one and install it:

VS Code extensions view with the search Python and Microsoft's Python extension ready to install

Python (by Microsoft). Essential. Check that the author is Microsoft: there are many with similar names. Installing it also installs three more by themselves:

  • Pylance: autocomplete, documentation on hover and errors marked as you type (the underline I ignored).
  • Python Debugger: step-by-step debugging with breakpoints (covered in depth in Python debugging).
  • Python Environments: creating and choosing virtual environments (.venv). According to its Marketplace page it’s still rolling out, so you may not see its panel yet; everything in this guide works the same either way.
What the VS Code Python extension brings Installing the Microsoft Python extension also installs Pylance, Python Debugger and Python Environments. Jupyter, Rainbow CSV and Black Formatter are installed separately. What the Python extension bringsPython(Microsoft)Pylanceautocomplete and errorsas you typePython Debuggerstep-by-step debuggingbreakpoints, F5Python Environmentsvirtual environmentscreate and pick .venvThey install themselves with it ↑ · You add these ↓Jupyter.ipynb notebooksRainbow CSVcoloured columnsBlack Formatterformat on save
One search (“Python”) gets you three extensions; the ones below are your choice.

As the diagram shows, a single search (“Python”) gives you three tools. The ones at the bottom are your choice:

  • Jupyter (by Microsoft): opens and runs .ipynb notebooks cell by cell without leaving VS Code. In statistics and data science you’ll use them constantly.
  • Rainbow CSV: colours each column of a .csv file. It seems trivial until you have a 30-column CSV in front of you.
  • Black Formatter (by Microsoft): tidies your code on save (spaces, line breaks, quotes). It’s what makes the “Format On Save” setting in step 3 work.
  • indent-rainbow (optional): colours indentation levels. In Python indentation is part of the code, and this extension makes it very obvious when a line is one level off.

Installing extensions from unknown authors that promise “all-in-one Python”. They can clash with Pylance (two autocompletes at once) or ask for odd permissions. Microsoft’s ones plus this list are more than enough.

Step 3 — Key settings to configure VS Code for Python

Open settings with Ctrl + , and search for each setting by name:

  • Auto Save → afterDelay. VS Code saves by itself every second; you’ll never again run an old version because you forgot to save.
  • Font Size → 14, 15 or 16. The default is small and after two hours your eyes notice.
  • Format On Save → on. Every time you save, Black tidies your code.
  • Default Formatter for Python → Black Formatter. Without this step, “Format On Save” does nothing with .py files, because VS Code doesn’t know what to format with.

If you prefer, the same settings can be written directly in settings.json (Ctrl + Shift + P → Preferences: Open User Settings (JSON)):

{
    "files.autoSave": "afterDelay",
    "editor.fontSize": 15,
    "editor.formatOnSave": true,
    "[python]": {
        "editor.defaultFormatter": "ms-python.black-formatter"
    }
}

The "[python]" section means “for Python files only”: that way, when you program in C or Java, that formatter stays out of it. Watch the commas: in JSON they go between items, never after the last one (I checked this block with a JSON parser so it has no extra ones).

With afterDelay the file saves itself, but “Format On Save” only kicks in when you save yourself with Ctrl + S. Get used to pressing it now and then and you’ll see the code rearrange itself.

Step 4 — Connect it to Python

Here’s the step most people skip: open the folder, not the file. Go to File > Open Folder and choose the subject folder, for example GCID\FP1. With a folder open, VS Code has a “workspace”: it remembers which Python to use, keeps its own settings and detects any virtual environments inside.

A subject folder opened in VS Code The FP1 folder contains .venv with its own Python, .vscode with settings.json and the topic folder with test.py. When you open the folder, VS Code detects the environment and saves the settings; with a loose file it does not. Open the folder, not the fileFP1/← File > Open Folder.venv/its own python.exe and pip.vscode/settings.jsonsettings for this foldertopic_variables/test.pyWith the folder openVS Code sees .venv andoffers it as interpreter.Settings are saved here.With a loose fileThere is no workspace:the environment and settingsare not remembered.
One folder per subject with its .venv: that way every assignment uses the right Python.

Create the virtual environment (.venv)

A virtual environment is a lightweight copy of Python just for that folder, with its own pip. That way, one subject’s libraries don’t mix with another’s. To create it:

  1. Ctrl + Shift + P → Python: Create Environment.
  2. Choose Venv and then the Python you installed (for example 3.15.2).
  3. Wait a few seconds: the .venv folder appears in the explorer.

When you open a terminal in VS Code, PowerShell answers that “running scripts is disabled on this system” and doesn’t activate the environment. It’s a Windows protection. Fix it for your user only with Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser in PowerShell, or choose Command Prompt as the default terminal (Ctrl + Shift + P → Terminal: Select Default Profile).

Choose the interpreter

Inside the folder, create a subfolder for the topic and the file test.py. At the bottom right, in the status bar, you’ll see the Python version VS Code is using. If it isn’t yours (or it says “Select Interpreter”), click there or use Ctrl + Shift + P → Python: Select Interpreter and pick the .venv one.

Python interpreter picker in VS Code with the installed version marked by an arrow and the status bar highlighted at the bottom right

Run your first program

Write this in test.py:

# test.py: the program that checks VS Code is running your Python
name = "Sergio"
subjects = ["FP1", "Calculus", "Algebra"]          # a list with three strings

print(f"Hello, {name}. Welcome to VS Code.")
print(f"This term you have {len(subjects)} subjects:")
for s in subjects:                                 # the indentation (4 spaces) marks what repeats
    print("  -", s)

Press the run button, the triangle at the top right (Run Python File). The terminal opens at the bottom and shows:

Hello, Sergio. Welcome to VS Code.
This term you have 3 subjects:
  - FP1
  - Calculus
  - Algebra

Quick check: the list holds three strings, so len(subjects) is 3, and the for prints one line per subject, each with two spaces and a dash in front. If you get this, you’re all set.

What exactly happened when you pressed the run button? It’s in the diagram:

What happens when you press Run Python File When you press the run button, VS Code takes the file in the editor, uses the interpreter shown in the status bar and types the command in the terminal, where the program output appears. What happens when you press  1 · Editortest.pythe code you see2 · Interpreter3.15.2 (‘.venv’)the one in the status bar3 · Terminalpython test.pyVS Code types it for youHello, Sergio. Welcome to VS Code.This term you have 3 subjects: – FP1 – Calculus – AlgebraIf the interpreter in step 2 is not yours, step 3 runs a different Python (and imports fail).
The run button is not magic: it is “python file.py” with the interpreter you picked at the bottom right.

VS Code doesn’t run anything “by magic”: it types python test.py in the terminal using the interpreter you chose in the status bar. On Windows you’ll see something like & C:/Users/YourName/GCID/FP1/.venv/Scripts/python.exe c:/Users/YourName/GCID/FP1/topic_variables/test.py: the full path of the environment’s python.exe and of the file. If the interpreter in the status bar isn’t yours, the program runs with another Python and the imports of the libraries you installed fail.

Check that Pylance warns you

Now repeat my anecdote on purpose. Create typo.py with this code:

# typo.py: a misspelt name that Pylance underlines BEFORE you run it
name = "Sergio"
pirnt(f"Hello, {name}")                             # print misspelt: pirnt

Before running it, look at line 3: pirnt is underlined and, when you hover over it, Pylance says "pirnt" is not defined. That’s the sign Pylance is working. If you run it with the run button anyway, this is what appears in the terminal:

Traceback (most recent call last):
  File "typo.py", line 3, in <module>
    pirnt(f"Hello, {name}")                             # print misspelt: pirnt
    ^^^^^
NameError: name 'pirnt' is not defined. Did you mean: 'print'?

Read the traceback from the bottom up: the last line says what happened (NameError: that name doesn’t exist) and even suggests the fix (Did you mean: 'print'?); the line above says where (line 3) and the little arrows ^^^^^ point at the exact word. If you don’t see the underline before running, check that the Python extension is installed and that the status bar shows an interpreter.

Ctrl + Shift + M opens the Problems panel with all of Pylance’s warnings for the project. Before submitting an assignment, take a look: if it’s empty, even better.

Notebooks (.ipynb) in VS Code

In statistics and the data subjects you’ll work with notebooks (.ipynb): documents that mix code cells, their output and text. With the Jupyter extension you open them directly in VS Code:

  1. Ctrl + Shift + P → Create: New Jupyter Notebook (or open an .ipynb someone gives you).
  2. At the top right click Select Kernel and choose the Python in your .venv. A notebook’s “kernel” is the Python that runs its cells (nothing to do with the Linux kernel).
  3. The first time, VS Code will ask to install the ipykernel package in that environment: accept.
  4. Type in a cell and press Shift + Enter to run it and move to the next one.

A notebook’s variables stay alive between cells while the kernel is running. If something gives strange results, Restart the kernel and run the cells in order from top to bottom: often the problem is having run an old cell.

Keyboard shortcuts that save you hours

You don’t need to learn them all on day one. With these nine (Windows, US layout; the Spanish layout equivalents are in the note below) you’ll already work twice as fast:

ShortcutWhat it does
Ctrl + Shift + PCommand palette: search any action by name
Ctrl + POpen a file in the folder by typing part of its name
Ctrl + backtickShow or hide the integrated terminal
Ctrl + SSave (and format, if you turned on Format On Save)
Ctrl + /Comment or uncomment the selected lines
Alt + ↑ / Alt + ↓Move the current line up or down
F2Rename a variable across the file
F5Start debugging
Ctrl + Shift + MProblems panel with Pylance’s warnings

On a Spanish keyboard the backtick and slash keys are elsewhere, so VS Code maps the terminal to Ctrl + ñ and commenting to Ctrl + }. If one doesn’t work for you, look it up in File > Preferences > Keyboard Shortcuts.

Final check: is everything ready?

Before calling the setup done, go through this list:

  1. Does VS Code open the subject folder (not a loose file)?
  2. Do you have Python, Pylance, Python Debugger and, if you’ll use them, Jupyter and Black Formatter installed?
  3. Does the status bar show the Python in your .venv?
  4. Does the run button run test.py and print the output above?
  5. Does Pylance underline pirnt in typo.py before you run it?
  6. When you press Ctrl + S, does the code get reformatted?

If you said yes to all six, your environment is just like mine.

Common problems and how to fix them

No interpreter appears in the list. VS Code can’t find Python. Check in a terminal outside VS Code that python --version works, close VS Code completely and open it again.

The run button uses a different Python. Look at the status bar: the chosen interpreter is in charge. Change it with Python: Select Interpreter.

ModuleNotFoundError: No module named 'numpy' even though you installed it. You installed it into another Python. Open a terminal inside VS Code, which already has the environment activated, and run python -m pip install numpy.

“Format On Save” does nothing. The default formatter for Python (Black Formatter) is missing or the extension isn’t installed.

The imports are underlined in yellow (“could not be resolved”). Same root cause: Pylance looks at the chosen interpreter and that library isn’t in it.

The terminal doesn’t recognise python. If you’ve just installed Python with VS Code open, the integrated terminal keeps the old PATH. Close VS Code entirely and open it again.

SymptomLikely cause
No interpreters in the listPython badly installed or VS Code opened before installing
ModuleNotFoundErrorLibrary installed in another Python
Format On Save does nothingNo default formatter
pirnt not underlinedPython extension not installed or no interpreter
Scripts disabled in PowerShellWindows execution policy

What’s next?

With Python installed and VS Code configured you have the environment ready for your entire degree. Three ideas to take away:

  1. Open folders, not loose files, and one folder per subject with its own .venv.
  2. The interpreter in the status bar is in charge: it’s the one the run button, Pylance and pip use.
  3. Read the underlines before running: Pylance saves you errors the terminal only shows afterwards.

In the next article we get into the interesting stuff: variables and data types in Python, what each variable stores and why Python thinks differently from other languages. And whenever you want to watch your program line by line, the debugger you just installed is explained in Python debugging.

Keep goingAll Tools articles

Did this article help you? Here is how you can support SergioLearns →