How to use Python in VS Code

This article walks you through writing Python code for the first time in VS Code, including how to use the debugger. This assumes you already have VS Code and Python installed.

Note: My project path is C:/Projects/Python/python-in-vscode. Substitute your own project path where it makes sense.

1 – Install the Python extension

You can use the VS Code UI or CLI to install extensions. I’m going to show how to install it using the command line.

From a terminal, execute the following to install the Python extension:

code --install-extension ms-python.python
Code language: Bash (bash)

Verify it installed by executing this:

code --list-extensions
Code language: Bash (bash)

This list should have the Python extension:

... ms-python.python ...
Code language: plaintext (plaintext)

If you already had VS Code open, restart it.

2 – Create a new repo, clone it, and open it as a workspace

I like to start new projects by creating a new repository on the server first (Note: I’m using GitHub).

  • Create a new repository. Mine is called makolyte/python-in-vscode.
  • Clone the repository locally.

Note: My Python project is C:/Projects/Python. Change this to your preferred project folder.

cd C:/Projects/Python git clone
Code language: plaintext (plaintext)
  • In VS Code, open the folder and create workspace file.
    • File > Open Folder > C:/Projects/Python/python-in-vscode/
    • File > Save Workspace As > workspace.code-workspace
  • Commit and push using the Source Control tab.

If you’re used to other IDEs, like Visual Studio 2019, workspaces are like project or solution files. They simply group together source files.

3 – Add Python code

Add a new file called

I suggest you type the following code, so you can see how helpful the Intellisense feature can be.

def isAnagram(word1, word2): print(f"Checking {word1} vs {word2} ") if len(word1) != len(word2): print ("Nope - diff lengths") return False; for char in word1: if char not in word2: print (f"Nope - word1 has {char} which is not in {word2} ") return False; print(f"Yup - {word1} and {word2} are anagrams") return True; assert isAnagram("note", "tone") assert not isAnagram("note", "dummmmmmm") assert not isAnagram("noter", "tonem")
Code language: Python (python)

Save the changes (Ctrl-S)! This is a confusing part about VS Code. It doesn’t auto-save changes when you try to run / build code. You have to remember to save your changes.

4 – Run it

Because you installed the Python extension, you can run Python scripts by clicking this green play button in the upper-right corner. Note: If you don’t see it, restart VS Code.

VS Code - Run Python script button

This is the same as Terminal > New Terminal > py

Running the anagram Python script outputs the following:

Checking note vs tone Yup - note and tone are anagrams Checking note vs dummmmmmm Nope - diff lengths Checking noter vs tonem Nope - word1 has r which is not in tonem
Code language: plaintext (plaintext)

5 – Break the code so you can debug it

It’s a good idea to figure out how to run the debugger before you really need it. So first, break the code, so you have a reason to use the debugger.

To break the anagram function, I changed “if char not in word2″ to “if word1 not in word2.”

for char in word1: if word1 not in word2: print (f"Nope - word1 has {char} which is not in {word2} ") return False;
Code language: Python (python)

When I run this, one of the asserts fail:

Checking note vs tone Nope - word1 has n which is not in tone Traceback (most recent call last): File "", line 15, in <module> assert isAnagram("note", "tone") AssertionError
Code language: plaintext (plaintext)

6 – Add Debug config

You can click Run > Start Debugging, but this prompts you for your preferred debugging configuration every time. This is a bit annoying, so it’s simpler to add a Debug configuration so it stops prompting you.

  • Run > Open Configurations
  • Select your workspace from the dropdown. Mine is python-in-vscode.
  • Select Python File from the dropdown.

This creates a file called launch.json in your workspace. Mine is here: C:/Projects/Python/python-in-vscode/.vscode/launch.json.

7 – Debug it

  • Put a breakpoint on line 9. Note: To add a breakpoint, hover your cursor to the left of the line numbers and click it. You’ll see a little red dot appear.
for char in word1: if word1 not in word2: print (f"Nope - word1 has {char} which is not in {word2} ") return False;
Code language: Python (python)
  • Press F5 to start debugging.

This will stop the code execution on line 9. You can examine the local variable values by hovering your cursor over them. A more powerful way to debug is to use the Debug Console.

In the Debug Console, you can execute any code in the context of the current executing code. So let’s say you want to examine why the condition is true and why it’s entering this if block when you don’t expect it to.

In Debug Console execute the condition as a string:

f"{word1} not in {word2}"
Code language: Python (python)

This outputs the following:

'note not in tone'
Code language: plaintext (plaintext)

Oh, now you see the problem. You’re accidently checking if word1 is in word2 instead of checking if char is in word2. Fix the condition by changing “if word1 not in word2″ to “if char not in word2.”

Then restart the debugger (Run > Restart Debugger or Ctrl-Shift-F5).

Leave a Comment