Tutorial: python on the terminal

Unit 2 – Online meeting

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 python and ipython are programs that we installed into a specific conda environment. So, for the command to work, you must activate the correct conda environment first.

From these two screenshots you see that the python interpreter uses >>> as prompt, the ipython interpreter uses In [#]:. Remember that the computer command line uses a prompt sign like > on Windows and $ on Mac and Linux. Make sure you recognize these different prompt signs, so that you immediately know ‘whom you are talking 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.

Whenever you do interactive python work on the terminal, I suggest you use the interactive python interpreter ipython, which has some convenient features built in that the other python interpreter is missing, such as interactive help through access to code documentation using ?, auto-completion of commands and file paths when hitting the TAB key, or command history navigation using the Arrow-up and Arrow-down keys.

Two ways of executing python code

In our first workshop unit, we used two websites to execute python code online before we installed conda and python on our own computers. We used https://www.pythonanywhere.com/try-ipython/ as an online ipython instance and https://www.online-python.com/ as online editor to write .py files that we executed with a Run button.

Now, that we have installed python ourselves, we can execute our code on our own machines.

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 with the python command, like this:

Note that if you write the python command in the terminal, it will open the python interpreter. If you provide a filepath, like here, the python interpreter executes the program contained in the python script, much like the Run button on the website we used.

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

When you are talking to the python interpreter, you can run the program interactively. In other words, you 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