Skip main navigation

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

Find out more

Running Python scripts: VS Code vs. Terminal

Learn how to run a Python script from within VS Code as well as a terminal outside of VS Code.
12.1
In this lecture, we are going to look at how we can run a Python script from within VS code and from the terminal outside of VS code. So the script I’m looking at is this python_argv_ex1. And all of these files will be available to you on the course website. So you can download and file along, which I highly recommend you do. If you want to take a step beyond that, you would actually type out this file yourself from the video. So you’d stop the video here. And you type it out. That’ll help you learn more. But you can also download it to file to follow along.
50.1
So let’s first look at the code that we have in front of us. So the first thing you see is this string right here. And this string is defined between three quotation marks. See the three here. And notice how when I highlight them up here, VS code automatically highlights the closing quotation marks. That’s nice. It makes the code a little easier to read. But what is the string? This is called a docstring. So when you define a string between three quotation marks at the top of your script, the Python interpreter recognizes this as a docstring. And it will assign this string to the variable, underscore underscore doc underscore underscore. So if you print that, this is a variable.
96.2
It’s automatically created. And if you print that, it will print this string. So why would you have this string? Well, it’s always a good idea to document your code, whether it’s for a colleague or yourself in six months when you’re looking at your code and trying to figure out what you did. I always recommend writing doc strings, or your scripts, or your functions. And we’ll see that in future lectures, how you would write a docstring for a function. So here, this is docstring for a script. This is just a Python script. So let’s go ahead and run this. I know I’m skipping over the import sys. I’ll come back to that.
131.8
But now, let’s just run this and see that print underscore underscore doc underscore underscore print this. So in a previous lecture, we mapped the action - the print. In fact, let me just bring it up here. So I did, again, Command-Shift-P on a Mac or Control-Shift-P on a Windows to bring up the command palette. And we’ll go to the keyboard shortcuts. And I just searched for run. And in a previous lecture, we mapped this run Python File in Terminal to Command-R or Control-R, if you are on a Windows machine. And so we can just press Command-R or Control-R. And it will automatically run the file in the terminal. And that’s what happens right here.
183.2
But this is the actual command in the terminal that runs the file. And then this is the output. All of this is the output. Let’s break this down. First of all, we see the output of the docstring. Here is the docstring. So this print is what allows this to happen. This is printing that docstring. Now we also see this printed out. What does this come from? This comes from the sys.argv. So let’s break that down now. We’ve imported the cis Python package. And now we are printing sys.argv. argv is a list of the command line arguments that we passed to Python.
235
So when you run a script from the terminal - I’m highlighting the command that actually runs the script - first, what you do is you would write the path to your Python interpreter. And a lot of times, in fact for you, and we’ll see this very soon, you only need to write Python. Because your computer knows that when you type Python, it’s actually pointing to this path. I’ll make that more clear in just a minute here. So the first thing you want to know is that this is just the path to your Python interpreter. On my computer, if we started, in my root directory, you would then go to the users directory. You would then open the William Henry directory.
276.3
Then, anaconda, envs, which is short for environments. Then, the UCI underscore Python 3. Then, bin and then, that’s where the Python interpreter says it’s the base path or the root of your computer to the Python interpreter. Then, we’re passing the path to the file we want to [AUDIO OUT] So again, this is the base path - users/williamhen ry/documents/vscode_ - so that’s my workspace directory - then the, week 2 directory, which I put inside my workspace directory just to organise the code. And the Python argv ex one dot pi. So we’re calling the Python interpreter. And we’re passing it a path to the script we’d like to run. This path counts as a argument.
321.7
And that’s why when we print sys dot argv it prints that out. We’re going to have a few lectures here on argv. So if that’s still confusing, go ahead and watch the next lectures. And it’ll start to make more sense. But now, I want to run this same file, this exact same script, from the terminal but outside of VS code. So this is a terminal that VS Code has opened and displaying within VS code. You can also open a terminal outside. So here, I’ve just opened Terminal on my Mac. And if you’re on a Windows machine, you can open Windows PowerShell or the Anaconda prompt. It might be easier to open the Anaconda prompt you’ve used in previous lectures.
365
So you can open the Anaconda prompt and then you do have to navigate to your workspace directory.
374.9
And this is where it could be confusing. This is actually fairly simple. But if it’s new to you, then it can seem confusing at first. So I will talk through how it is on my machine or in my Terminal. It’s going to be very similar whether you’re on a terminal on a Mac or in the Anaconda prompt on a Windows machine. So when I open the Terminal, it opens me too my user directory. So what I mean is that right now if I type ls, it’s going to list the contents of my user directory. If you’re in a Windows, you can type dir and that will list the contents. It won’t on a Mac.
412.6
That’s why it says, “dir command not found.” But on Windows you could type that. And so I’m in my user directory. And you can see all the contents of my user directory. In fact, let me actually go to my user directory right there. So now I am in the File Explorer or in the Finder. So Finder on a Mac, File Explorer in Windows. I’m actually in my user directory. But this is the prettier representation. So you can see a list of everything in my user directory. But this is the same list here. It’s just a text representation. I’m looking at the exact same thing. But here I have the folder icons, et cetera. This is just listing by name.
459.3
So now I want to go to the Documents folder because I want to go to my VS Code workspace. So I’m going to type CD, which is short for Change Directory. And then I’m going to type, documents. And if I type, ls - again, ls on a Mac, dir on a Windows machine. Type ls so we can see the contents of my documents directory. So I can show you that in the finder as well if I open documents. Now we see the contents. And you see the contents are the same because I’m looking at the same thing. This is just the text representation. And this is more graphical. So here’s my VS Code workspace.
494.5
So now, I’m going to do CD VS Code. And you can always press Tab to autocomplete. Now, I’m in my workspace. I want to go into the week 2 directory. And now, I see all the files in my week two directory. In fact, [INAUDIBLE] to all these files here.
518.3
I’m seeing the same files. I’m looking at the same directory. So now that we’re here, let’s run that script just like we did from within VS Code.
537
So now, I’m going to run the script. And there’s a big difference here. What’s the big difference? Well, I’m only typing Python. Why is that? Because Python will, on my machine - actually, I need to activate my environment. You don’t need to do this. So I’m going to activate my environment that I’m using for this course. You don’t need to worry about this.
560.2
OK. So now, when I type Python, it is going to point to the exact same Python. So this path that VS Code - VS Code prints the entire path. So that’s what you see right here. When I just type Python in the terminal, it will point to that. And that will be the same on your machine. So if you have multiple environments, you’ll have to activate one. But most of you - you’d know if you have multiple environments because you would have set it up. So I’m guessing that probably all of you, in this course, don’t have to worry about that. So if you just type Python, it will point to the right Python interpreter.
594
And then I’m going to type the name of the script.
605.9
There we go. So I’ve typed the name of the script. And I’m just going to run it by pressing Enter. So notice what happens here when I press Enter. It runs the script. And so it prints that docstring. And it prints sys.argv which is the argument in the command line. It’s the first argument, which is just the file we want to run. And you may also notice, why did I only have to type the file name and not the entire path here. Why didn’t I type the entire path? Well, let me copy that path if it’ll let me - maybe not. The reason I did - just type the name is because I’m already in that directory.
652.6
I am already here. So I can just type the name. And that’s called a relative path. So it will look for the file relative to where I am. I’m already in the directory. So just do this. So if I just type the name, it will look for this name in the directory I’m in. And of course, it finds it. And there you go. Could I still use the absolute path? So I just copied and pasted the path from VS Code. This is the absolute path, meaning it’s not relative it starts in the root directory. That’s what the first forward slash means. That first forward slash means it’s starting in the root directory. I don’t have to have the quotes.
692
But the quotes help, especially because there’s a space in here. But if you’re wondering, do you have to have quotes when you use an absolute path - the answer is no, you don’t have to. But now, if I hit Enter, it still runs the same script because this is still pointing to the same script. And then we get the output we see here. And when you’re using the absolute path, you don’t need to be in the same directory as the code. I just navigated here to help learn how to navigate. And also, I think it makes things a little more clear.
723.4
But if I go back to my user directory - so now I’m in my user directory - I can still run that piece of code. I just have to use the absolute path. I still run the same script. But I need to use the absolute path.
740.6
All right, that was a lot. So high level, what are we trying to take away from this? First, we covered docstrings. We looked at how we can print that. We reviewed how we can run a script from VS Code. And we observed the output, both of printing the docstring and of printing sys.argv.
764
We also had a quick crash course on how to run something from the Terminal or the Anaconda prompt, if you’re on a Windows machine. And this is going to be important. So this in some ways might have been the hardest part of these few lectures, if this is new to you. But if you get this - excellent job. If you have any questions, just ask in the discussion forum. And we’ll continue to learn more about sys.argv in the next lecture. All right, I’ll see you in the next lecture.

In this video, you will learn how to run a Python script from within VS Code as well as a terminal outside of VS Code.

As we saw in the video, when you define a string between three quotation marks at the top of your script, the Python interpreter identifies this as a ‘doc’ string. This string will automatically be stored with the name ___doc___.

Docstrings are useful when used to describe what the script is doing. By having one in your script, when a peer is looking at your code, or when you are looking at it 6 months after creating it, it is clear what the script is aiming to achieve.

Running Python script within the VS Code terminal

Follow along

The file used in this exercise is python_argv_ex1.py and is available for download from the Downloads section below. Once you have downloaded the file, open the file in VS Code, and follow along with the video.

Mapping command shortcuts

The presenter made use of a command shortcut to run a line of code. To create your own shortcuts open the command palette.

  • Mac: cmd+shift+p
  • Windows: ctrl+shift+p

Select ‘keyboard shortcuts’, and press the pencil button on the right of the command to edit to shortcut preference.

The presenter used the shortcut cmd R to run his script within the terminal in VS Code.

Running Python script from a terminal outside VS Code.

When running your Python script from a terminal outside of VS Code, here are the steps summarized from the video:

  • Move from the User directory to the directory where the scripts are stored.
  • Use the command “python” followed by the name of the script, then run the script.

sys.argv

To use the sys.argv command, it is necessary for you to import the sys package in Python. This can be done easily by using the code line: import sys as seen in the video. argv is simply a list of the command line arguments that we passed to the Python interpreter. print(sys.argv) will print the commands that produced the output in the terminal once your script has been run.

Let’s take a closer look at sys.argv in the the next step.

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