Skip to content

Working with Python scripts

Working with Python scripts on Arrhenius

Learning outcomes

  • Practice using the documentation of your HPC cluster
  • To be able to create a Python script
  • To be able to run a Python script
For teachers

Teaching goals are:

  • Learners have practiced using the documentation of their HPC cluster
  • Learners have created a Python script
  • Learners have run a Python script

Other goals are:

  • Verify that learners indeed have learned how to create a file
  • Verify that learners indeed have learned how to use a text editor

Lesson plan:

  • 5 mins: prior knowledge
  • 5 mins: presentation
  • 15 mins: challenge
  • 5 mins: feedback

Prior questions:

  • Why would you want to use a script?
  • In which languages can one write a script?
  • How does one run a script?

Why use scripts?

You have multiple lines of code you want a computer to run. Instead of copy-pasting these lines individually to the interpreter, one can use a script.

To run code, we use text files that are run as if each line was typed in into the Python interpreter. Such text files that run (interpreted) code are called 'scripts'.

In this session, we practice to create and run some simple Python scripts, without new theory.

Demonstration

Before doing the exercises, a teacher will speedrun through the exercises.

Exercises

Enjoy a video?

You can find a video with solutions to these exercises:

HPC Cluster YouTube video
Arrhenius HPC YouTube video
Bianca Not needed: no registrations
COSMOS YouTube video
Dardel YouTube video
Kebnekaise YouTube video
LUMI Not needed: no registrations
Pelle YouTube video

Exercise 1: create a Python script

Go to the user documentation for your favorite HPC cluster:

HPC cluster User documentation
Arrhenius HPC User documentation
Bianca User documentation
COSMOS User documentation
Dardel User documentation
Kebnekaise User documentation
LUMI User documentation
Pelle User documentation

For your favorite HPC center, find the documentation on how to create a file. Search for a maximum of 5 minutes! If you cannot find it, use the UPPMAX documentation about nano or ask the AI at the UPPMAX documentation

Answer

From the documentation of your center, searching for, for example, 'create file' or 'text editor', you will find information on how to create a file.

HPC Cluster Documentation
Arrhenius HPC None
Bianca Not needed: no registrations
COSMOS None
Dardel None
Kebnekaise Documentation
LUMI Not needed: no registrations
Pelle Documentation

On your HPC cluster, create a file called hello_world.py, using a text editor or Linux commands

Answer

Creating a Python script hello_world.py using nano:

HPC Cluster How to run nano
Arrhenius HPC nano hello_world.py
Bianca nano hello_world.py
COSMOS nano hello_world.py
Dardel module load nano ; nano hello_world.py
Kebnekaise nano hello_world.py
LUMI nano hello_world.py
Pelle nano hello_world.py

Copy-paste the following content into the script:

print("Hello, world!")
Answer

To paste into a terminal, use CTRL + SHIFT + V (i.e. add SHIFT).

Save the script

Answer

In nano, you can do so with CTRL + O

Close the text editor

Answer

In nano, you can do so with CTRL + X

Verify that the script has its contents saved

Answer

One way to do so, is to use nano to open the file again:

nano hello_world.py

Exercise 2: run a Python script

For your favorite HPC center, find the documentation on how to run a Python script. Search for a maximum of 5 minutes! If you cannot find it, use the UPPMAX documentation about how to run a Python script or ask the AI at the UPPMAX documentation

Answer

From the documentation of your center, searching for, for example, 'Python script', you will find information on how to start a the Python script.

HPC Cluster Documentation Solution
Arrhenius HPC Documentation python hello_world.py
Bianca Documentation python hello_world.py
COSMOS Documentation .
Dardel ⚠ Documentation and more documentation .
Kebnekaise Documentation .
LUMI Documentation .
Pelle Documentation python hello_world.py
  • ⚠ means that the documentation does not clearly answer this question. You may find that you can piece it together easily enough or you may find that you cannot. You are encouraged to contact your HPC center to help them help you better

Run the Python script you just created

Answer
HPC Cluster How to run create_plot.py
Arrhenius HPC python create_plot.py
Bianca python create_plot.py
COSMOS python create_plot.py
Dardel python create_plot.py
Kebnekaise python create_plot.py
LUMI python create_plot.py
Pelle python create_plot.py

Done?

Go the the main room and start working on, for example, the next session. When around two thirds are in the main room, we move on to the next session.