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
    • Data Science
    • Grundlagen der Optimierung
    • Effiziente Netzwerke
    • Entscheidungen unter Unsicherheit

How to program?

  • Coding1 environment2
  • Programming language
  • Computational thinking
    • 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 brief primer you learned here 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
Approach 1: Standard GUI installation

Please download and run the executable installation file that you find here.

Approach 2: terminal

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

curl -O "https://repo.anaconda.com/miniconda/Miniconda3-latest-Windows-x86_64.exe"
start /wait "" Miniconda3-latest-Windows-x86_64.exe /S
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-one

mkdir -p ~/miniconda3
curl -O "https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-arm64.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"

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

bash Miniconda3-latest-MacOSX-arm64.sh -b -u -p ~/miniconda3
rm -rf Miniconda3-latest-MacOSX-arm64.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 file for your computer architecture that ends with .sh.

After the file has downloaded, run the following commands to install it:

bash Downloads/Miniconda3-latest-MacOSX-arm64.sh -b -u -p ~/miniconda3
rm -rf Downloads/Miniconda3-latest-MacOSX-arm64.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.
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 -rf ~/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 scipro2023, which contains python version 3.11 and a number of additional packages we will need throuought the course. To do this, use the following command:

conda create -n scipro2023 python=3.11 ipython jupyterlab numpy pandas scipy matplotlib pyodbc pandasql
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 scipro2023
  1. Open the python interpreter
python
  1. Try typing something!

Recap & learning checklist

  • I am aware that learning programming is like learning a new language
  • I am aware that every programmer spends a lot of time searching for solutions to coding challenges online
  • I understand the difference between python and the python interpreter
  • I know that the python ecosystem consists of many (open-source) packages that are built for a wide range of applications
  • I know that different versions of python can be installed on a computer along with different packages. I know that conda is a package and environment manager that allows me to manage these installations and workspaces.
  • I installed conda and created my first conda environment that is specific for this course
  • I know what a terminal is (and I will use the Miniconda prompt instead of the Command prompt for any terminal activities in this course if I am using Windows)
  • I can open a terminal and use it to navigate through directories, activate my conda environment, and start the python interpreter

Footnotes

  1. Synonym: Programming↩︎

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

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