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

Unlock This Lesson

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

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Running Python Code From the Command-Line

Give Feedback

Learn how to write and execute code from the command-line. You’ll see how to create, edit, save, and execute a script on Windows Ubuntu and Mac OS.

00:00 Next, you’re going to see how to run code from the command line, first in Windows. So, holding down Windows, tapping R, and then typing cmd will get you to the command prompt.

00:16 Now you can change into the Documents/ folder with cd Documents, and you can make a new directory with mkdir and then the name of the directory you want. Here, it’s realpython.

00:27 Change into that directory with cd, and then make another directory called running_scripts. Again, change into that directory.

00:42 Now using dir, you can see that’s an empty directory. And to create a new empty file, we need to use a little trick. We’re going to use copy and then NULwhich is null, an empty file—and enter the name of the file in question.

00:57 So copy NUL hello.py creates a zero-byte file called hello.py. Now you can edit it using the command idle and then the name of the file that you’ve created—here, hello.py.

01:20 In a few seconds, you’ll see IDLE open up, and now you can type the commands you want. Here, again, the classic 'Hello World!' Save the file with Control + S, and then you can close the window either with the X or by doing Alt + F4. Now, you can run the file by typing python, a space, and then the name of the script.

01:43 There you can see, Hello World! has run, and now you can exit by typing exit and hitting Enter. Now, you’ll see how to run scripts from the command line using Ubuntu.

01:56 Right-click on the desktop and pick Open Terminal. And now change into a directory. So here, I’m going to change into the realpython/ directory and the running_scripts/ directory. And you can see with ls that it’s an empty directory—there are no files in there.

02:15 And you can use the touch command to create a new file, so touch, space, then the name of the script. And if you list again, you can see hello.py exists, and listing with the -al switch allows you to see that the file has a size of 0.

02:28 And now you can use nano to edit the file by typing nano, space, and then the name of the file you want to edit. So here we’ll type again the classic print('Hello World').

02:40 And you see you get some color-coding of syntax in this editor, so you can write it out using Control + O and then hitting Enter to save it. And then Control + X will exit nano. Back in Terminal, you can run it with the command python3 and then the name of your script.

03:03 As ever, type exit to quit the terminal. Now, running code from the command line on macOS. Command + Space searches for an app, and you can type “term” to open up a terminal.

03:20 And you’ll see the commands are the same as when using Ubuntu. Opening directories, listing with ls.

03:34 The touch command works in the same way as Ubuntu, creating an empty file.

03:52 And you can use nano in the same way to edit the script.

04:03 You’ll notice there is no color-coding by default in the Mac version of nano. But the commands work the same way—Control + O and then Enter to write the file, and then Control + X to exit.

04:20 python3 and then the name of the script will get it to run, and then you can exit the terminal with exit, using Command + W to close the window after it’s finished.

haydnvose on Oct. 10, 2020

When I go to edit the file I get an error that reads: ‘idle’ is not recognized as an internal or external command, operable program or batch file. What could I have done wrong?

Darren Jones RP Team on Oct. 13, 2020

@haydnvose - can you give me some more info - what operating system you’re on, and which version of python you have installed (you can do that by typing python3 at the command line, and you should be in the REPL which will give you a python version). You can get out of the REPL by typing exit()

Become a Member to join the conversation.