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?
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
Programming can only be learned by regular exercise!
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
- 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
Open a terminal on your computer!
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
..
Directories and files deleted with rm
or del
are lost forever. They don’t go to the trash, they are directly deleted.
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
- Open a terminal and perform the following tasks from the terminal
- Change directory to the location in your file system where you want to create the new folder
- Create the new directory (pick a name that you like! I call it
scipro
for now) - Create another directory called
testdir
- Move
testdir
intoscipro
- Verify that
testdir
is listed withinscipro
- Move into
scipro
, thentestdir
, and then back out intoscipro
- Delete
testdir
In the following semester, I will refer to the directory we created above as $SCIPRO
. This is were all our exercises will live.
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
- 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
- 32- or 64-bit computer
- minimum of 400 MB disk space
- Windows, mac, or Linux operating systems
- For Windows: Windows 8.1 or newer
Please download and run the executable installation file that you find here.
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
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
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
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
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
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
You find the comprehensive conda documentation that includes more detailed installation instructions here.
Installing Miniconda: troubleshooting
Configuring conda to use specific channels
- On Windows, open the Miniconda prompt, on Linux/Mac OS open a terminal.
- Then add the conda-forge channels with the command:
conda config --add channels conda-forge
This only needs to be one time after installation.
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
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
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.
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
- Activate your new environment by typing
conda activate scipro2023
- Open the python interpreter
python
- 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