Skip to content

Working with Python scripts

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.

Exercises

Enjoy a video?

You can find a video with solutions to these exercises:

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

Exercise 1: create a Python script

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
Alvis None
Bianca Not needed: no registrations
COSMOS None
Dardel None
Kebnekaise Documentation
LUMI Not needed: no registrations
Pelle Documentation
Tetralith None
  • 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
Alvis nano create_plot.py
Bianca nano create_plot.py
COSMOS nano create_plot.py
Dardel module load nano ; nano create_plot.py
Kebnekaise nano create_plot.py
LUMI nano create_plot.py
Pelle nano create_plot.py
Tetralith nano create_plot.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

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
Alvis Short documentation or long documentation .
Bianca Documentation python hello_world.py
COSMOS Documentation .
Dardel ⚠ Documentation and more documentation .
Kebnekaise Documentation .
LUMI Documentation .
Pelle Documentation python hello_world.py
Tetralith ⚠ 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

You can run this Python script in the shell by:

python hello_world.py