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
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.
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.
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 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.
Become a Member to join the conversation.