Tutorial: terminal & ipython

Workshop 1

The terminal

This brief command line tutorial follows up on the information provided in the lecture notes 01B on “The terminal, conda, & python”.

The following cheatsheet shows the most important commands in both operating systems, Windows and Mac OS/Linux:

In addition to these commands, it is important to know that the command prompt represents the current directory with a dot ., and the parent directory with two dots .. . It is also useful to remember that you don’t have to manually type long directory names yourself, but that you can start typing the first letters of the name and then hit the TAB key for auto-completion.

The following two sections demonstrate some important tasks on the command line using the exercise from the lecture notes:

Exercise

Goal: Create a directory for our programming course at a location in your file system that suits you, for example:

/home/flo/documents/fhv/LV_scipro/
C:\Users\flo\Documents\fhv\LV_scipro


  1. Open a terminal and perform the following tasks from the terminal
  2. Change directory to the location in your file system where you want to create the new folder
  3. Create the new directory (pick a name that you like! I call it scipro for now)
  4. Create another directory called testdir
  5. Move testdir into scipro
  6. Verify that testdir is listed within scipro
  7. Move into scipro, then testdir, and then back out into scipro
  8. Delete testdir

On Windows

When we open a command prompt, the prompt is situated in the Home directory. On Windows, the Home directory most often looks similar to C:\Users\YourName. Let’s open a command prompt, and verify in which directory the prompt is:

In the screenshot, we see the start-up message of the prompt (this might look different in your case!), and the first command line in the second to last row. It reads C:\Users\knan>. The > sign tells us that the prompt is ready for us to type in a command. In the screenshot, the first command I typed is chdir, which asks the prompt to display the path of the current directory. The output that resulted from the command is printed in the last row, C:\Users\knan. The path displayed in front of the prompt sign > is identical to the output of the command. From that we learn, that the prompt always tells us in which directory it is currently located.

Let’s do tasks 2–4 from the exercise. That is, we first move to a location in the file system where we want to create a directory for our course. In this example, I will create the directory on the Desktop.

The first of the three commands changed the directory to the Desktop.

How do we know that the prompt actually changed to the Desktop?

We know, because the path in front of the prompt sign > of the second line changed to C:\Users\knan\Desktop.

The other two commands created two folders on the Desktop, one called scipro one called testdir.

The very first command I used earlier (chdir) was used stand-alone by itself. This time, the command to change directory to the Desktop involved two words. The first word is the command cd, followed by a space and then the directory where we want to go. Similar with creating a new directory. We tell the prompt that we want to mkdir (“make a directory”) with the name scipro.

Tasks 5 and 6 ask us to move the directory testdir into the directory scipro and verify that the task was successful.

The command to move testdir into scipro consists of three words. We can read it out loud like “move testdir into scipro”. The prompt confirms by tellung us that it moved one directory, and when we ask it to show us the contents of scipro, it displays some information about the storage disk, the file path of the directory, and then it lists three sub-directories. Hold on, why three? It shows the current directory (.), the parent directory (..), and then testdir. So, first of all, moving testdir into scipro did work! The two directories . and .. are always contained in every directory. They allow for easy and consistent navigation through the file system of your computer. To demonstrate this, let’s move into testdir and then back out of it into scipro. Remember that we are currently still on the Desktop. So,

The first command moves into scipro and continues straight into testdir. To achieve this, we separate the two directory names with a \. To move back out of testdir we cd into .., which represents the parent directory. The paths displayed in front of the prompt tell us that the navigation worked as expected and we ended up in scipro.

The last task asks us to delete testdir. The cheatsheet tells us that the correct command is rmdir. To make this exercise a bit more realistic and insightful, I added another file into testdir in the meantime. Let’s see whether rmdir does the job:

Upon rmdir testdir, the prompt tells us that the directory (testdir) is not empty. This is a safety mechanism that prevents you from accidentally deleting a folder that contains files or other folders. To delete a non-empty directory, you have to use del as you see in the screenshot.

Important

Files and directories deleted with del are gone forever. They don’t move to the “Trash” but are immediately deleted!

Programmers usually don’t use names for files or directories that contain spaces. However, since most of you likely have some directories that contain spaces, I am showing you how to deal with them:

If you read carefully up until here, I don’t need to explain anymore what the first two commands and their output mean. At this point I want to highlight the useful feature of command auto-completion. At the last prompt, I am typing cd Nam (i.e., the first three letters of the directory "Name with spaces"). Note that I even leave out the tick marks ". Now, when I hit the TAB key, this is what happens automatically:

You can cylce through your previous commands with the arrow buttons. Arrow-up will display the most recent command, and Arrow-down will bring you back to the empty prompt.

OK, so let’s move into this directory and then go back to our Home directory. To do this, let’s count how many directories we have to step out of. There is "Name with spaces" in scipro, which is on the Desktop. So, our Home directory is three levels up. That makes three .. and we can switch to Home like that:

I show you another trick, if you don’t want to count. But first, let’s got back into "Name with spaces" in one go:

You will reach your home directory from anywhere in your file system if you type

That concludes our basic command prompt tutorial. Make sure you understand how to navigate through your file system using the command line!

Main differences to Mac OS / Linux, which are built around a powerful terminal

There are many differences, like different commands and capabilities, but for our course, you only need to know two visual differences:

  • The command line prompt in Windows starts with a >, in Linux it starts with a $.
  • The paths of the file system are written with backslashes \ in Windows, but with forward slashes / in Linux.

Please be aware of those two differences, when you see lecture material from my computer (which uses Linux).

On Mac OS and Linux

When we open a terminal, the prompt is situated in the Home directory. On Unix-like operating systems, the Home directory most often looks similar to /home/YourName. Let’s open a terminal, and verify in which directory the prompt is:

In the screenshot, we see a terminal with a command prompt. It reads flo@steed:~$. The $ sign tells us that the prompt is ready for us to type in a command. In the screenshot, the first command I typed is pwd (print working directory), which asks the prompt to display the path of the current directory. The output that resulted from the command is printed out, /home/flo.

Let’s also understand the term that is displayed before the prompt sign $. flo is the username, and steed is the name of the computer. In between the : and the $, the prompt displays the current path. In the above case ~. The ~ is the Unix symbol for Home directory.

Let’s do tasks 2–4 from the exercise. That is, we first move to a location in the file system where we want to create a directory for our course. In this example, I will create the directory on the Desktop.

The first of the three commands changed the directory to the Desktop.

How do we know that the prompt actually changed to the Desktop?

We know, because the path in front of the prompt sign $ of the second line changed to ~/Desktop.

The other two commands created two folders on the Desktop, one called scipro one called testdir.

The very first command I used earlier (pwd) was used stand-alone by itself. This time, the command to change directory to the Desktop involved two words. The first word is the command cd, followed by a space and then the directory where we want to go. Similar with creating a new directory. We tell the prompt that we want to mkdir (“make a directory”) with the name scipro.

Tasks 5 and 6 ask us to move the directory testdir into the directory scipro and verify that the task was successful.

The command to move testdir into scipro consists of three words. We can read it out loud like “move (mv) testdir into scipro”. When we ask the prompt to show us the contents of scipro with ls, it lists testdir. So, moving testdir into scipro did work! Let’s do something slightly different:

We just used an option to the command ls, -a. This option lists all files and directories within scipro, so also the hidden files and directories. It shows the current directory (.), the parent directory (..), and then testdir. The two directories . and .. are always contained in every directory. They allow for easy and consistent navigation through the file system of your computer. To demonstrate this, let’s move into testdir and then back out of it into scipro. Remember that we are currently still on the Desktop. So,

The first command moves into scipro and continues straight into testdir. To achieve this, we separate the two directory names with a /. To move back out of testdir we cd into .., which represents the parent directory. The paths displayed in front of the prompt tell us that the navigation worked as expected and we ended up in scipro.

The last task asks us to delete testdir. The cheatsheet tells us that the correct command is rmdir. To make this exercise a bit more realistic and insightful, I added another file into testdir in the meantime. Let’s see whether rmdir does the job:

Upon rmdir testdir, the prompt tells us that the directory (testdir) is not empty. This is a safety mechanism that prevents you from accidentally deleting a folder that contains files or other folders. To recursively delete a non-empty directory with all its contents, you have to use `rm -r`` as you see in the screenshot.

Important

Directories deleted with rm are gone forever. They don’t move to the “Trash” but are immediately deleted!

Programmers usually don’t use names for files or directories that contain spaces. However, since most of you likely have some directories that contain spaces, I am showing you how to deal with them:

If you read carefully up until here, I don’t need to explain anymore what the first two commands and their output mean. At this point I want to highlight the useful feature of command auto-completion. At the last prompt, I am typing cd "Nam (i.e., the first three letters of the directory "Name with spaces"). Now, when I hit the TAB key, this is what happens automatically:

Note the / at the end of the path you want to change directory to. If you carefully read the previous screenshots you saw that I didn’t include the trailing / in my manual commands. In our cases, both approaches are ok.

You can cylce through your previous commands with the arrow buttons. Arrow-up will display the most recent command, and Arrow-down will bring you back to the empty prompt.

OK, so let’s move into this directory and then go back to our Home directory. To do this, let’s count how many directories we have to step out of. There is "Name with spaces" in scipro, which is on the Desktop. So, our Home directory is three levels up. That makes three .. and we can switch to Home like that:

I show you another trick, if you don’t want to count. But first, let’s got back into "Name with spaces" in one go:

You will reach your home directory from anywhere in your file system if you type cd alone or cd ~:

That concludes our basic command prompt tutorial. Make sure you understand how to navigate through your file system using the command line!

The interactive python interpreter

In all operating systems we start the python interpreter from the terminal with the same command: python for the standard interpreter, and ipython for the interactive interpreter. Remember, that you need to have the correct conda environment activated for the command to work.

From these two screenshots you see that the python interpreter uses >>> as prompt, the ipython interpreter uses In [#]:. Make sure you know these different prompt signs, so that you can tell ‘whom you are speaking to’ when using the terminal. The computer command line and the python interpreters speak different languages and commands that work for one, do not work in the other context. When you have the choice, I suggest you always use the interactive python interpreter ipython, which has some convenient features built in that the other interpreter is missing.

Two ways of executing python code

Remember one of our simple programs we wrote in class, for example

# Program for calculating the usage time (in minutes) 
# of an appliance with a given wattage, 
# if 1kWh of electricity is available

# Define variables
wattage = 1000  # ~ of appliance in (W)
cfac1 = 1000    # conversion factor for kilo
cfac2 = 60      # conversion factor for minutes

# Perform calculation
minutes = wattage * cfac2 / cfac1

# Output
print(minutes)

We can save this code in a .py file, for example $SCIPRO/01_workshop/calculate_usage_time.py, and then execute the program from the terminal like this:

This command only works if we are talking to the computer command line! So, we need to see a prompt that looks like > in Windows and like $ in Mac OS. If we see a prompt like >>> or In [#]: we are talking to one of the python interpreters and the above command fails.

When we are talking to the python interpreter, we can run the program interactively. In other words, we can copy and paste the python code into the python promt, like that:

Note that pasting into the terminal works with right-click, or with a keyboard shortcut like Ctrl-Shift-C (this is likely different for your operating system–the Internet will know!).

At this point, it is important for you to be able to distinguish between the different prompts on your terminal, which look different for the computer command line and the python interpreter. I also want you to be able to execute python code both ways, from the command line and from within the interpreter.

Learning checklist

  • I can use a terminal to navigate through my file system.
  • I understand the different appearance of the computer command line and the python interpreter, although “both live in my terminal”.
  • I can execute python code from the command line and from within the python interpreter.