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 | |
Use the Python interpreter (see below) | Interactive, terminal-based, some help | |
Use IPython | Interactive, terminal-based, more help and features | |
Use Jupyter | Interactive, web-based | |
Use Visual Studio Code | Interactive, install on local computer, use locally installed Python and Python packages |
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:
Within nano, write:
- 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:
or, if you want to be explicitly use Python 3:
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:
or for python3
:
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:
or (for explicit Python 3):
The Python prompt looks like this:
Type, for example:
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 ...
... and press enter. Now a variable called s
will hold some text.
Now type ...
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:
(or as a one-liner: import matplotlib.pyplot as plt; plt.plot([1, 4, 9, 16]); plt.show()
)
You will see a window appear:
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.