Skip main navigation

New offer! Get 30% off your first 2 months of Unlimited Monthly. Start your subscription for just £35.99 £24.99. New subscribers only T&Cs apply

Find out more

Common problem with VS Code

Learn about a very common annoyance when running Python code from VS Code.
Hi, everyone. I’m making this video to illustrate a frustration within VS Code. And that is when you try to run lines of Python in the Python interpreter, and then run a Python file. It’s just an annoyance of VS Code that I haven’t yet found a workaround for. So let me show you what it is, and show you how you can work with it, but it’s just annoying. So the first thing to note is I have VS Code open, and I have this Python file with some very simple code, and then down here, in the lower pane, I have the terminal. And so on a Mac, this will be the terminal. On a Windows machine, this will be the PowerShell.
Even though it still says terminal, it’s like you’re using the Windows PowerShell, which is basically the Windows Terminal. And now, I’m going to go up here, and select some lines of code, and run them. Run the lines of code in the Python terminal. And so something really important down here happens. It actually opens a new terminal window, so we were in this terminal window, and now, we are in the second one. And VS Code pasted these commands into the terminal down here. And this starts up the Python interpreter.
So these commands here are what started the Python interpreter, yours will not look the same, because you probably don’t have a separate Python environment, and you have different paths in your computer, anyways. But that’s OK, they don’t look the same. That’s not a problem. Those are the commands that are valid on your computer to start the Python interpreter. So that’s what it does, and then here’s some header information about the Python interpreter. It prints the version, and the location, and some things, and that’s fine. And then, it actually prints the code in here and runs it. So it runs this nice for loop where we print one, two, three.
You can also click down here and run other Python code, like so.
There we go. OK, so this is all good we have run lines of code down here, nothing wrong with that at all. But now, let’s try to run the file. So I’m going to click here, and, now, run Python file in terminal. And look at what happens.
This command, right here, VS Code has pasted this command into the Python interpreter, and tried to run it, but there’s a really big problem with that. This command is for your terminal or your PowerShell. It’s not for the Python interpreter. This is invalid Python code, so you get a syntax error.
So what we need to do is exit the Python interpreter, so we can be back in the terminal, and run the file. So to do that, just type “quit,” and now you can go up here, and you can run Python file in terminal. And then, it runs the file. See how it issues the exact, here we go, the exact same command that it did up here. This is the exact same. But now, it’s actually issuing it in the valid location in the terminal. This runs your file, and see what prints out? It’s very nice. Now, there’s a problem. If you try to run lines of code again, so run select lines of Python terminal.
It will now not restart the Python interpreter, so see it just pasted this code directly into the terminal without starting the Python interpreter. And that, of course, is not good. So you need to start it again yourself, and you can do that by clicking in here. So clicking - see where my cursor is, I’m clicking here. And then, I’m just going to use the up arrow to go to the previous command that started the Python interpreter. This is probably going to be the most confusing portion if you are not used to this, but it will be a command that ends in bin forward slash Python. All this is, is the path to your Python interpreter.
So it should be a one-part command, so there shouldn’t be multiple segments. So this has - notice this command has two segments, the first one and the second one. Shouldn’t have that, it should just be one. And now, it’s sort of back up. And, in fact, while we’re here, I’ll show you something. So this is the command that ran the file, right? This is the command that VS Code pasted into the terminal to run the file, and all it is, is it’s the path to our Python interpreter, and then the path to the file we want to run. And that’s the command that I’ve just gone up to here.
So I’m just using the up and down arrow keys to cycle through previous commands, so here we are. So one thing you can always do is just delete the part that is the path to the file, and then hit enter. This will start the Python interpreter. Now, you can go back up here, and you can run these lines. Now it’ll paste them down here, and it’s successful because it’s in the Python interpreter. So I know that’s probably confusing. The takeaway is VS Code is going to either paste the lines of code down in this pane, or the commander on the file down in this pane. You need to make sure that this pane is on the right thing.
Is it on your terminal or is the Python interpreter open? I have yet to find a way to make this work smoothly without having to make sure the Python interpreter is closed or open. You can open other terminals here, so you can add other terminals, and I thought there is a way to have one of them be where all the Python where, sorry. One of them be the location where when you try to run lines of code, it would go to the Python interpreter, and another one of these be just a terminal that, when you run a Python file, it would go here. But, honestly, I’ve yet to get that to work, and I’ve looked online.
It looks like I’m not the only one with this frustration, but this will be frustrating to you. So what do you do what should you do? Honestly, what I’ve gotten a habit of is actually just running full Python files. I know that’s not as helpful because when you’re learning, you want to build a run a few lines at a time, sometimes. But as you write longer files anyway, you’re going to want to run the whole file, because it won’t make sense to run a few lines in the middle of the file.
There’ll be too many other lines of code they depend on above in the file. So as you write more code, it’s actually not going to be as useful just to select a few lines and run it below. So what I end up usually doing, is just running files of Python, and that way, I never really run into this issue, because I’m never switching back and forth between running lines of Python and files. So that’s really it. If you have any questions, just ask in the discussion form. And great job, I’ll see you in the rest of the course.

In this video, you will learn about a very common annoyance when running Python code from VS Code. Were you able to guess what this problem could be in the previous step?

Watch this video to find out more.
The issue with running code in VS Code occurs when we switch from running lines of Python code to running an entire Python file or vice versa.
A suggested solution to this problem would be sticking to running only Python files, however, if you are new to Python, running lines of code to test them would be useful. For this purpose, the video explains how you could go about switching between the two.
As you get better, you will no longer need to run lines of code, and running entire Python files will be the optimal approach, especially when the code you are using becomes more complex.

Join the discussion

Take some time to explore this problem. If you are able to come up with a more efficient method of dealing with it, share the method with your peers.
Use the Comments section below and let us know your thoughts. Try to respond to at least one other post and once you are happy with your contribution, click the Mark as complete button to check the step off, then you can move to the next step.

This marks the end of the activity. In the next activity, we are going to start exploring different ways of running Python scripts.

This article is from the free online

Intermediate Python

Created by
FutureLearn - Learning For Life

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now