Installing Python at the University of Innsbruck#

Note

Python works very well on the stations in the computer room. It is a nice way for you to not have to carry your laptop with you for each class, and also you will learn some linux on the go! Furthermore, it is also useful for the climate class since all necessary packages are installed and ready to be used.

However, if you prefer not to, you can skip this lesson.

In this section we use the powerful linux PATH variable you’ve learned about in the linux tutorial to “install” the same Python program in your personal Linux account. This is done in a couple of easy steps.

The default python on linux systems#

Open a terminal and type:

$ python

This should have started the python command line, looking like this:

Python 2.7.5 (default, Apr  2 2020, 13:16:51) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

The >>> is a prompt, waiting for you to type python commands. Type [CTRL+D] to exit.

Python is available per default on virtually any linux system, for the simple reason that many programs actually use python internally. However, as you can see, it runs python version 2 (as of 2020, modern Linux distributions now removed the python command and only offer python3). We can also run python 3 with a simple:

$ python3

Now, shouldn’t this python program be enough? For simple usages like today’s examples, yes it would suffice. But as we are going to see, python alone isn’t really useful for us scientists: we need so-called third party packages, tools developed with and for python, but not available per default. While some of them are so important that they are available as linux packages, it is much easier (and safer) to use our own python installation for installing those. This is the primary purpose of a personalized python installation.

Using a custom (pre-installed) python at the university#

It turns out I have “installed” a more recent python version (and some additional packages) on a shared repository available to everyone with a UIBK account. I wrote “installed” with quotation marks because, in linux, the definition of “installed” is subjective. Let’s say that I’ve put the python executables somewhere where everybody can see them. Let’s try it:

$ /project/c7071047/miniconda3/bin/python
Python 3.7.3 | packaged by conda-forge | (default, Jul  1 2019, 21:52:21) 
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

You should have been able to launch a new python interpreter: this version is more recent and provided by Anaconda, Inc. This is the python I’d like you to use from now on (when at the University - for your personal computer see the next section).

Note

The /project folder is a shared folder. I don’t know where the device is located physically, but it definitely isn’t in the computer room. This is yet another powerful feature of linux systems: shared folders look just like normal folders. Quite practical, huh? Now this may have one simple drawback: launching this python might be a bit slow sometimes (especially if all of you try to open it at the same time). Most of the time this shouldn’t be a big problem, though.

Making this change “permanent”#

Nobody wants to write such a long command to start python. So what we are going to do is to add the /project/c7071047/miniconda3/bin/ folder to our PATH, and in such a way that it is remembered for your later sessions.

We are going to edit a special file in your HOME, ~/.bashrc. This file contains a list of commands which are executed automatically each time you open a terminal. How practical! Let’s open this file and add the following two lines at the end of it:

# added for the python course:
export PATH="/project/c7071047/miniconda3/bin:$PATH"

Warning

Careful! This will add a folder to the PATH variable, which is a fundamental element in linux: make sure that you add these two lines (and exactly these lines: respect the upper- and lower-case as well!) to your .bashrc

What did we just do? We added a folder to the PATH (remember what this is?) and added it at the beginning of it. This is important because linux is going to look for programs in order: if a python executable is found in the first folder, no need to look for another one (i.e. ignore the default linux program).

Note

For this change to take effect, close your terminal and open a new one! ~/.bashrc is executed only once, at the opening of a new terminal.

If everything worked fine, after typing python in the command line you should be given the most recent version I prepared for you.

Learning summary#

  • python is available per default on almost all linux distributions, for the simple reason that many software packages use python internally

  • on linux, a python “installation” is simply a link to a python executable file - on Windows, it’s a bit more complicated