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.
Running Python Code From the Command-Line
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.
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
Change into that directory with
cd, and then make another directory called
running_scripts. Again, change into that directory.
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
NUL—which is null, an empty file—and enter the name of the file in question.
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,
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.
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.
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.
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
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
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.
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.
And you’ll see the commands are the same as when using Ubuntu. Opening directories, listing with
touch command works in the same way as Ubuntu, creating an empty file.
And you can use
nano in the same way to edit the script.
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.
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 - 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
Become a Member to join the conversation.
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?