The Terminal is an interface that allows the user to write instructions directly to the OS. You can use the Terminal to run the Python program that you just created. When running programs in the Terminal, the Terminal will display status and debugging messages produced by the program, which can be very useful.
Each operating system has its own version of the Terminal. To get started, open up the Terminal program for your operating system. In most distributions of Linux it is calledTerminal, in Windows it is called Command Prompt, and in macOS it is called shell. Many of the commands also differ between operating systems; however, the commands that we will use in this book will be mostly the same irrespective of operating system. You should see a prompt in the upper left-hand corner that looks something like this:
The first thing that you need to do to run your program is to navigate in the Terminal to the directory containing your program. Terminal commands take place within a specific folder in the computer's filesystem. Windows, Mac, and Linux all use thecdcommand, which stands forchange directoryand can be used to change the working directory. To change to the directory containing yourhello_world.pyfile, you can use the following command:
$ cd <relative/path/to/directory>
A couple of things to note here:
When you see angle brackets in a Terminal command or a line of code, this often indicates a placeholder for something that is specific to your system or configuration. In the place of<relative/path/to/directory>, you will need to type the relative path to the folder containing your program (thehello_world.pyfile that you just created).
The$symbol indicates that the command should be entered into the Terminal and should not be copied with the rest of the command. Not all Terminal programs use a dollar symbol to indicate a prompt, so don't worry if yours doesn't lookexactly like this.
The relative path to the directory refers to the exact sequence of folders leading from the current directory to the destination directory. The current directory is often listed at the beginning of each prompt. I the current directory is not listed at the beginning of the prompt, you can find it by using thepwdcommand in Linux and Mac.
The following command is used in Linux and Mac in order to show the current working directory:
$ pwd
The following is the command used in Windows to show the current working directory:
$ cd
In the relative path, each subsequent folder is separated by a forward slash (/) in Linux and Mac, and a backward slash (\) in Windows. The following is what it looks like on my computer to find the current directory, change to mychapter2folder, and then verify that I am in the correct location:
If it is easier, you can also do this incrementally by changing folders one directory at a time. If you accidentally move to the wrong directory, you can use..to move up one directory in the filesystem:
$ cd ..
This command will change the working directory to the parent folder of the previous working directory. Another useful command for navigatingfiles is thelscommand in Mac and Linux, and thedircommand in Windows. This command will list all of the files and folders in the current directory.
InMac and Linux, the following command is used to list the contents of the current directory:
$ ls
In Windows the following command is used to list the contents of the current directory: