Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Running Python Scripts in Terminal

00:00 In this lesson, I want to show you how to run Python scripts in the terminal. And since we want to run a Python file in the terminal, let’s start in the terminal.

00:08 First, you need to make sure that you’re navigating to the location on the system where you want to create your Python file. I’m in my Documents directory, which works for me.

00:18 The location is not that important, but you need to remember that you will create your Python file in the directory that you’re currently in. If you’re not sure how to navigate in the terminal, then you can check out the first lesson of this video course where I linked a tutorial, which contains some helpful commands to get started with the terminal.

00:39 For now, we’ll just use one terminal command, the echo command. So you can think of echo similarly to the print() function in Python.

00:48 It outputs what you’re given as an argument. So if I output the string ‘Hi!’, then you see that Hi! is outputted to the terminal. That’s pretty nice because you can also redirect this output to a file.

01:04 We’ll do this in a moment, but first let’s go a bit deeper with our echo command. And this time we don’t want just to give out string ‘Hi!’, but we want to give out a string that is actually Python code.

01:18 Let me write it and then we can talk about it in a moment. echo and then space, and then a single quote that’s important, let’s talk about this in a moment as well.

01:28 Then print("Hello, World!") to start your Hello, World! string just like before, don’t forget to close the string with the double quotes, then the closing parentheses to close the print() function call.

01:43 And in the end, you add a single quote again to close the string that you’re passing in to the echo command.

01:51 First, let me hit Enter and then let’s have a look. Just like before, the echo command outputs what you put in as an argument to the terminal.

02:01 So here it’s the print("Hello, World!") Python code. Since your Python code contains the string with the double quotes, you need to use the single quotes in front of the print() and in the end of the command.

02:14 Otherwise, you would run into a problem there. So you can basically think about this print("Hello, World!") like a string package that you pass into the echo command.

02:25 Now, I was mentioning that you can redirect this output to a file instead of outputting this code into the terminal. And you can do that by using the greater sign.

02:37 And after the greater sign, you can put a file name, in this case, I will use hello.py. And what’s happening now when I press Enter is that this text that I’m having here is not outputted to the terminal, but redirected in the hello.py file.

02:53 And if the hello.py file doesn’t exist yet, then the echo command will also create this file, which is pretty cool, and that’s how I’m using it here right now.

03:03 One detail to note here is that not the echo command itself creates the file, but by using the redirection operator, the terminal creates the file.

03:13 So echo sends the output and then the terminal recognizes like, Hey, the echo command is followed by a redirection operator. And since after that, there is a file, the terminal creates the file. Note that there is no output at all right now because we redirected it into the hello.py file.

03:33 Now to check that the hello.py file is present, let’s run the file.

03:40 Running Python scripts in a terminal follows the same pattern on all operating systems. You use the Python command just like you do when you start a REPL session, but instead of hitting Enter right away, you pass in the Python file path as an argument.

03:55 So on Windows, that’s py or python, and then the file path. And on Linux and macOS, it’s python3 or python, depending on your configuration, followed by the file path.

04:09 When you’re in the same folder as your Python file, you don’t have to add in the full file path, but you can just refer to it by the file name, just like I did when I redirected the output to the hello.py file.

04:23 So here on macOS, I can type python space not hitting Enter because I don’t want to go into the REPL. Instead, I add hello.py as the argument to the Python command, and now when I hit Enter, you can see that the Hello, World! message is outputted to the terminal.

04:42 So that shows you how to run Python files in the terminal. And I want to point out another little detail here. Compared to the echo command from before, now the output doesn’t show the print() and the parentheses and the double quotes, but it shows what the print() function call does outputting “Hello, World!”.

05:03 So that verifies that we’re actually running the Python code inside of hello.py.

05:10 And although the output is similar to our REPL session from before, running actual Python files has a few advantages. One of the advantages of saving your Python code in a file is that you can run the script many times without needing to retype the code over again.

05:27 That also means you can make edits to the file and run it again then. Also, you can back up and share your code better when you save it into a file. In other words, you can do all the things that you can do with files because it’s a file.

05:42 And last but not least, if you want to develop larger functionality in your Python code, using a script is the way to go. But you definitely don’t want to use the echo command for that.

05:55 So it’s time to enter the world of text editors.

Become a Member to join the conversation.