The VS Code Python Extension
Learn how to extend VS Code functionality through the use of extensions. You’ll see how to search for extensions using keywords, sort the results and how to customize the extensions once installed. This lesson will also show you how to install and configure the Python extension which supports:
- Linting
- Debugging support
- Code completion with IntelliSense
- Code snippets
- Unit testing support and more
00:00 I’ve just opened Visual Studio Code and the first thing you’ll notice is that it’s got a very minimalistic user interface. We’ve got our standard welcome page front and center, and on the left here, we have the action bar with six icons: Explorer, Search, Source Control, Debug, Extensions, and at the bottom here, Manage. First, let’s take a look at Extensions. Visual Studio Code is extensible, meaning that third-party developers can write extensions that users can download from the Marketplace.
00:34 You’ll notice here that I have one extension enabled: Code Runner. VS Code also recommends me extensions based on other programs I’ve got on my computer, like Atom Keymap and Kubernetes.
00:46 If we click the More Options button here and select Show Built-in Extensions, we see that VS Code contains some extensions that are built into the program.
00:56 We’ve got extensions for PROGRAMMING LANGUAGES, which allows for things like syntax highlighting and bracket matching; FEATURES that VS Code comes bundled with; and finally, THEMES so that you can customize the editor to look as you want.
01:10 Let’s delete the sort filter here and run a search for Python. This is how we search the Marketplace for new extensions. If we click on Python here, you’ll see that we get lots of different information about the Python extension.
01:25 I’ll click on Contributions, and now you can see all of the settings and other features that this extension adds to the editor. We can change all of these. At the very top here, I’ll click on Install, and now we should have a good experience working with Python in Visual Studio Code. There are tons of extensions that you can install, and I’d encourage you to look around and see what looks interesting.
01:49 One of my favorites is called Code Runner, and it allows me to simplify the process of running code. This is the extension that lets me right-click in the editor and select Run Code in all of my other videos.
02:02 It also lets me hide a bunch of other distracting stuff so my video tutorials focus on the content that’s important. Let’s go back to the Explorer and I’ll close out of the Extensions screen here.
02:14
And now we’ll select Open Folder. I’ll create a new folder here on my desktop and I will call it Demo
, and now I’ll click Open.
02:24
Let’s pretend we’re starting to build a new Python package here. I’ll select File > New File, and now I have to save this, so I’ll click on File > Save. I’ll call this file program.py
.
02:39
The .py
tells VS Code that this is a Python file, and so when we create it, you’ll see that it tells us our Python version in the lower-left corner of the screen. If we click on that, we can select here which Python installation on our system that we’d like to use.
02:57 Also, if I click out of this, we can press Command + Shift + P, or Control + Shift + P on Windows and Linux, and that will open the Command Palette. This is a quick way to perform common tasks like creating a new file or refactoring code, and it only requires using the keyboard.
03:16
We don’t have to take our hands off the mouse. Now, let’s actually write a small Python program. I want to start by making a list of a few numbers. I’ll call this list numbers
.
03:27 And you’ll notice that once I start typing, VS Code automatically starts showing me suggestions for functions, classes, and other various members that I might be trying to use in my program.
03:40 This is called IntelliSense, and it’s a fan favorite feature of any version of Visual Studio. I will ignore it for now and finish creating my list. Now, say I want to iterate through the list and print the numbers to the console.
03:55
One way I could do this is with a for
loop. I’ll type for
and now we see IntelliSense trying to help us out. If you noticed, the icon for the second item in the dropdown is a little box. This is called a code snippet, and it lets us quickly insert and edit chunks of code into our program.
04:16
So I’ll move down with the arrows and I’ll hit Tab. And you see here that we have a template for a for
loop right here in our code. I can fill in these highlighted values, pressing Tab to move to the next field.
04:30
Notice that when I open the parentheses, we also get information about the arguments required for the print()
function, along with what the function does. I’ll print the number
.
04:42 You can also hover over a variable or a function to see more information about it. And this feature right here is one of my favorites. Right-click and choose Peek Definition.
04:54 Now you can change the definition of this member without having to leave the current file you’re editing. This isn’t too helpful right now because we’re only working with one file, but pretend this was a function declaration in a file somewhere else. Rather than having to go and search for that, or even have VS Code take us to that file, we can edit the implementation or the definition right here within this file.
05:19 This is one of those features that I really miss when I use other editors. To run this program, we just have to right-click. You’ll notice that I have a Run Code option at the very top, which is what I normally click on in my videos.
05:34 That’s from the Code Runner extension that we saw earlier. If you don’t have that installed, you can select Run Python File in Terminal, and you’ll see the integrated terminal appears at the bottom, and our program is executed. When you create the new Python file, you might’ve seen a pop up in the corner prompting you to install a Python linter. A linter is a program that checks your code for errors, like how you see a squiggly line if you try to use a variable that hasn’t been defined yet.
06:03 You can quickly install linting support from that pop up, which will install PyLint by default. If you want to use a different linter, you can find it in the Extensions Marketplace. All right.
06:14 I think that’s enough for one video. In the next video, we’ll see how we can configure VS Code to our liking.
Austin Cepalia RP Team on April 5, 2019
Thanks, I’m glad you liked it! :)
Peter T on April 8, 2019
At the 3:10 mark, what did you mean when you said: “it only requires the keyboard, you don’t have to take your hands off the mouse”.
Austin Cepalia RP Team on April 27, 2019
The command palette can be summoned with just a keyboard shortcut. From there you can type commands that would normally require you to point and click specific menu items with your mouse, which takes longer than quickly summoning the command palette and typing the command. This saves you a lot of time in the long run when you consider how frequently you perform specific actions.
rehdwolfe on July 12, 2019
Awesome course so far. I’d had long wanted to try VS Code.
I think at 3:10 you meant to say, ““it only requires the keyboard, you don’t have to put your hands ON the mouse”.
Austin Cepalia RP Team on July 13, 2019
Oh yes, you’re right.
chuahengwee on Jan. 6, 2020
just a note, pylint requires AutoSave to be turned on (which is not a defauit settings). Else u will need to manually save the file to see pylint taking effect. also, need to take note of the pylint path especially if working in venv. took me a while to figure this out. would be good if u can include in future updates to the video
jobrywebmail on May 7, 2020
“Run code” produces this output but prints no numbers. If I use even a simple print(‘abcd’) line of code this still is not printed. Any ideas ??
[Running] python -u “c:\Users\Admin\Desktop\DEMO\Program.py”
[Done] exited with code=0 in 0.291 seconds
jobrywebmail on May 7, 2020
re: above problem. Very strange - I repeatedly got this (non-print) output when I selected ‘Run Code’ When I changed to “Run Python file in Terminal” it printed as expected. When I changed back to ‘Run code” it is now printing as expected too. Some funny first-time phenomenon or ?? Anyway, now no problem.
Youngblood on Oct. 2, 2020
I observed the same behavior. Your solution appears to have fixed the ‘Run Code’ lack of output for me too. Thanks!
Peter Chen on May 16, 2021
Great tutorial fit my need but several things stop the progress.
- When I type “for” in the editor, the code snippet sign with a little box didn’t pop out.
- After I hit “run code”, the output window below showed “exited with code=9009”
- I tried “run python file in terminal”, and it returned me “conda:無法辨識”(things like conda: couldn’t be identified etc.)
Could anyone know what the problems are? Great thanks!
Martin Breuss RP Team on May 17, 2021
Hi @jobrywebmail, @Tim Youngblood, and @Peter Chen. The issue you’re encountering with the Code Runner extension is interesting 🤔
I tried to reproduce it with a fresh installation of Code Runner, since I wasn’t using it myself. I was able to run it right after installing it using the context menu that Austin mentioned:
# test.py
print("abc")
Running this snippet through the Run Code context menu gave me the following output:
[Running] python -u "/Users/martin/Desktop/test.py"
abc
[Done] exited with code=0 in 0.061 seconds
So it seems that it was working as expected for me. Was this the first time that you executed any Python code through VSCode?
Troubleshooting Steps
For future reference, if anyone runs into this issue try to do what solved it for them:
- Run the file through Run Python file in Terminal instead
- Then try again using Run Code
It might also help to restart VSCode after installing the extension.
Aleks Lykhin on Sept. 24, 2022
The code snippets are now removed from the python extension in vscode. According to this thread github.com/microsoft/vscode-python/issues/15220, it was done deliberately in Jan 2021. Currently, one has to install an additional code snippet extension to enable them, for example, marketplace.visualstudio.com/items?itemName=frhtylcn.pythonsnippets
aes1189 on Feb. 9, 2023
This is a very well put together video course. I am curious, though, which version of Python it’s based on. I have version 3.11 installed, but in the video at about 2:50 the selection pulldown has version 3.7.1.
R S on July 29, 2023
On my mac - I always need to launch Vs Studio.app. It doesn’t show up within applications. Anyone else facing the same?
Beth DeGrave on March 21, 2024
At 4:27 it shows the for with the lines and says to select one down the for with the box. I am not seeing that available for selection.
Become a Member to join the conversation.
arnautovdmitry2015 on April 4, 2019
Wow! I am waiting for this course! And it is here. You are awesome! VS Code is my favourite code editor.