Skip to content

Python programming

This page describes how to program in Python on the UPPMAX clusters.

There are multiple ways to program in Python:

Description Features Screenshot
Use a text editor (see below) Non-interactive, no help Using GNU nano for Python
Use the Python interpreter (see below) Interactive, terminal-based, some help Using the Python interpreter
Use IPython Interactive, terminal-based, more help and features Using IPython
Use Jupyter Interactive, web-based Using Jupyter
Use Visual Studio Code Interactive, install on local computer, use locally installed Python and Python packages Using VSCode

Use a text editor

Using a text editor to program in Python is a simple way to write code: it is the same as writing any text file.

Here we use the text editor GNU nano to write a Python script:

nano example_script.py

Within nano, write:

print('Hello, world!')
  • To save, press CTRL + O (i.e. the letter), then enter to keep the same filename
  • To quite, press CTRL + Q

You can run this Python script in the shell by:

python example_script.py

or, if you want to be explicitly use Python 3:

python3 example_script.py

Some features of this approach are:

  • this is a simple way to write code: it is the same as writing any text file.
  • you get no help while writing code
  • you can only run the script from start to finish, i.e. you cannot partially run the script
How to run a Python script line-by-line?

You can run a Python script line-by-line using a Python debugger, such as pdb.

On the terminal, for python, do:

pdb example_script.py

or for python3:

pdb3 example_script.py

See the official Python documentation of pdb here.

Use the Python interpreter

After loading a Python module, you have the Python interpreter available.

Forgot how to load a Python module?

See the UPPMAX page about Python here.

What is a Python interpreter?

In computing, an interpreter is a program that reads text and runs it directly, without any additional steps.

The Python interpreter runs the Python commands you type directly, without any additional steps.

Start the Python interpreter by typing:

python

or (for explicit Python 3):

python3

The Python prompt looks like this:

>>>

Type, for example:

print('Hello, world!')

and the interpreter will run the statement.

Exit the Python interpreter with CTRL + D, quit() or exit().

The Python interpreter gives limited auto-complete while writing code

How do I get auto-complete?

As an example, writing this line of code in the Python interpreter ...

s = 'Hello, world!'

... and press enter. Now a variable called s will hold some text.

Now type ...

s.

and press Tab twice. You will see a list of things you can do with that string.

The Python interpreter can show graphics.

How do I get the Python interpreter to show graphics?

In the Python interpreter, run this code line-by-line:

import matplotlib.pyplot as plt
plt.plot([1, 4, 9, 16])
plt.show()

(or as a one-liner: import matplotlib.pyplot as plt; plt.plot([1, 4, 9, 16]); plt.show())

You will see a window appear:

A window with the plot

You will only see a window appear, if you've logged in to Rackham with SSH with X forwarding enabled.

Spoiler: ssh -X sven@rackham.uppmax.uu.se.

The Python interpreter cannot directly run scripts.