The terminal, conda, & python

Workshop 1

Learning outcomes

At the end of this session you will have a basic understanding of

  • which programming languange we will use and why
  • how to install and manage this “programming language” on your computer using a geeky command line interface to communicate with your computer

Programming

Why program?

  • Part of Science
  • Complex computations, computer models
  • Tangible visualizations
  • NES course modules
    • Grundlagen der Optimierung
    • Data Science
    • Flexibilitätsmanagement
    • Most Master Theses
    • Work after you earned your degree

How to program?

  • Coding1 environment2
  • Programming language
  • Computational thinking (analytical, logical, pragmatic, creative)
    • Creating algorithms
    • Divide and conquer
    • Pattern recognition
    • Solution generalization

The compromise of programming languages

python

a modern, versatile, high-level programming language

  • easy to read & learn
  • interpreted (i.e., not compiled)
  • large collection of open-source packages (‘python ecosystem’)
  • cross-platform compatibility

installation and management is non-trivial

python management

Different projects require different python versions and packages

python
a programming language
the python interpreter
a program that reads and executes python code,
available in different versions
a package
a ready-made bundle of code (from the python open-source ecosystem)
includes several ‘modules’, and can be part of a ‘library’
conda
a package and environment manager for python
a conda environment
an isolated and self-contained workspace where a specific version of python and required packages can be installed

Learning a new language

Important

Programming can only be learned by regular exercise!

Tip

Ask questions!

  • in a search machine (one of the most important skills in programming!)
  • to your friends and colleagues
  • in class and tutorium
  • Using AI: be critical and cautious. (optional:) read this excellent opinion piece

The terminal

Synonyms: Command prompt, command line, console

Why use a terminal?
  • Some tasks can not be performed in graphical user interfaces (GUIs)
  • Virtually all super computers and most web servers use Linux and need to be controlled from remote locations using terminals
Exercise

Open a terminal on your computer!

Demo

What can you do in a terminal?

  • Use [TAB] for auto-completion of commands and directory names
  • Hit [TAB] twice to get a list of options
  • current directory .
  • parent directory ..
Important

Directories and files deleted with rm or del are lost forever. They don’t go to the trash, they are directly deleted.

Exercise

Goal: Create a directory for our programming course at a location in your file system that suits you, for example:

/home/flo/documents/fhv/LV_scipro/
C:\Users\flo\Documents\fhv\LV_scipro


  1. Open a terminal and perform the following tasks from the terminal
  2. Change directory to the location in your file system where you want to create the new folder
  3. Create the new directory (pick a name that you like! I call it scipro for now)
  4. Create another directory called testdir
  5. Move testdir into scipro
  6. Verify that testdir is listed within scipro
  7. Move into scipro, then testdir, and then back out into scipro
  8. Delete testdir
Important

In the following semester, I will refer to the directory we created above as $SCIPRO. This is were all our exercises will live.

Note

Learning more about the terminal and its underlying language3 could fill entire workshop days. The primer you learned here and in the brief tutorial that I prepared for you will get you through this course. If you want to dive deeper, there are many starting points and good tutorials out there, for example Ryans linux command line turorials.

Installing python on your computer

conda


Important
Conda
a package and environment manager for python
A conda environment
an isolated and self-contained workspace where a specific version of python and required packages can be installed
Anaconda
an open-source distribution of conda that ships with a lot of additional software packages
Miniconda
a minimal installer for conda that ships with only few additonal, necessary software packages
Channels (in the context of conda)
are an essential part of the conda ecosystem by distributing packages and software that can be used with conda.

Installing Miniconda

System requirements
  • 32- or 64-bit computer
  • minimum of 400 MB disk space
  • Windows, mac, or Linux operating systems
  • For Windows: Windows 8.1 or newer
  • Admin rights (usually not possible on work laptops)
Approach 1: Standard GUI installation

Please follow the instructions here (i.e., download and run the executable installation file).

Approach 2: terminal

If you want to be adventurous, execute the following commands one-by-one in your terminal

curl https://repo.anaconda.com/miniconda/Miniconda3-latest-Windows-x86_64.exe -o miniconda.exe
Start-Process -FilePath ".\miniconda.exe" -ArgumentList "/S" -Wait
del miniconda.exe

If you are unsure about any setting during the installation process, accept the defaults, except for if you get asked whether you wish to run conda init: here, answer “yes”!

Approach 1: terminal only

First, try to download Miniconda from the terminal. To do so, try the following commands one-by-one4

mkdir -p ~/miniconda3
curl https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-arm64.sh -o ~/miniconda3/miniconda.sh

These two commands first created a directory miniconda3 in your home directory and then tried to download the installation file. If the curl command failed, try

wget https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-arm64.sh -O ~/miniconda3/miniconda.sh

If either the curl command or the wget command successfully downloaded the installation file you can run the following commands one-by-one5. If both commands failed, please move on to ‘Approach 2’.

bash ~/miniconda3/miniconda.sh -b -u -p ~/miniconda3
rm ~/miniconda3/miniconda.sh
Approach 2: Manual download & installation via the terminal

In case both curl and wget commands failed to download the file, please download the installation file manually here. Make sure you choose the right bash file for your computer architecture and place it in the Downloads folder.

After the file has downloaded, run the following commands to install it (you may have to adjust the filename!):

bash Downloads/Miniconda3-latest-MacOSX-arm64.sh -b -u -p ~/miniconda3
rm -rf Downloads/Miniconda3-latest-MacOSX-arm64.sh

If that approach didn’t work either, resort to the graphical installer here.

Important

After installing, you have to initialize your Miniconda. Try the first command, if that doesn’t work, try the second:

~/miniconda3/bin/conda init bash
~/miniconda3/bin/conda init zsh
  • If you are unsure about any setting during the installation process, accept the defaults.
In the terminal, execute the following commands one-by-one
mkdir -p ~/miniconda3
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ~/miniconda3/miniconda.sh
bash ~/miniconda3/miniconda.sh -b -u -p ~/miniconda3
rm ~/miniconda3/miniconda.sh
Important

After installing, you have to initialize your Miniconda. Try the first command, if that doesn’t work, try the second:

~/miniconda3/bin/conda init bash
~/miniconda3/bin/conda init zsh
  • If you are unsure about any setting during the installation process, accept the defaults.
Tip

You find the comprehensive conda documentation that includes more detailed installation instructions here.

Installing Miniconda: troubleshooting

In case the Miniconda installation does not work and we can not figure out why, we can try to install a conda/python distribution very similar to Miniconda, called Miniforge. Follow the instructions listed under Install.

Configuring conda to use specific channels


Task
  1. On Windows, open the Miniconda prompt, on Linux/Mac OS open a terminal.
  2. Then add the conda-forge channels with the command:
conda config --add channels conda-forge

This only needs to be one time after installation.

Important

From now on, I will only refer to the terminal. In the context of our course, this will be the Miniconda prompt on Windows or the terminal on Linux/Mac OS.

Creating our course environment

Task

Using your terminal, create a new conda environment named scipro2024, which contains python version 3.11 and a number of additional packages we will need throughout the course. To do this, use the following command:

conda create -n scipro2024 python=3.11 ipython jupyterlab=4.0.6 numpy pandas scipy matplotlib
Explanation

While it is totally fine to use other versions of python, I want us all to explicitly use the same version throughout this course to avoid any conflicts.

Tip

Install all the programs that you want in an environment at the same time. Installing 1 program at a time can lead to dependency conflicts.

Using your new environment

Exercise
  1. Activate your new environment by typing
conda activate scipro2024
  1. Open the python interpreter
python
  1. Try typing something!

Recap & learning checklist

Footnotes

  1. Synonym: Programming↩︎

  2. Synonym: Integrated development environment (IDE)↩︎

  3. For example, bash (Linux), PowerShell (Windows), or other ones..↩︎

  4. Taken from https://docs.anaconda.com/miniconda/#quick-command-line-install↩︎

  5. This will install Miniconda into a directory called miniconda3 within your home directory. Talk to me if you want to change the location of the installation↩︎