Setup environment

Python

Python is an easy to write general purpose programming language, meaning it can be used for a variety of different applications.

Scripting language

Python is a scripting language. Code written in Python is interpreted on the fly, instead of compiled into a piece of software beforehand. Python can be used to write stand-alone software, but (as other scripting languages) it can also be used to extend the functionality of existing software.

For example Python scripts can extend

  • Blender

  • Grasshopper

  • GIMP

  • FontForge

  • Glyphs

Terminal

As a scripting language Python can be used inside the console (Command Line Interface) of your OS in an interactive mode: type a line of code, press enter and it’s interpreted.

terminal

~$ python3
Python 3.10.6 (main, Mar 10 2023, 10:55:28) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> data = np.random.randint(low=0, high=2, size=(7,7), dtype=int)
>>> plt.imshow(data)
<matplotlib.image.AxesImage object at 0x7f051108bd90>
>>> plt.show()

(The $ is an indicator for code that’s written in the CLI. If you see >>> before Python code, this means that it’s written in an interactive session.)

Code is nothing else then plain text. The code above (without >>>)

import numpy as np
import matplotlib.pyplot as plt
data = np.random.randint(low=0, high=2, size=(7,7), dtype=int)
plt.imshow(data)
plt.show()

written to a file like random7x7.py can be executed through the CLI:

$ python random7x7.py

Virtual environment

It is recommended to create a virtual environment for your project/ setup. All the libraries and dependencies installed for this project are isolated and won’t conflict other libraries and their dependencies. The easiest way to create a virtual environment may be with conda, but other options like pipenv are also possible. We will work with Anaconda or Miniconda.

Anaconda or Miniconda?

The main difference is that Anaconda has a GUI to manage/ start environments. In addition more libraries are installed than with miniconda. Miniconda is controlled through the CLI, which may be faster and is definitely lighter.

Anaconda

Download and install Anaconda. Next to Python, a GUI (Anaconda Navigator) to manage packages and environments is included.

Open Anaconda Navigator

Click on Environemnts in the left pane, then click create

Type a name, like syntaxerror and the environment will be created

Click on the play button right to the environment, open a terminal and type

pip install flat

Then click on Home, then you see a grid of Applications: Install Jupyter Lab.

Then start Jupyter Lab, a browser window should open. On the left is a file explorer, where you can search for the project folder. Try to run the notebook.

Miniconda

Download Miniconda3 for your OS and execute the installer. Miniconda includes Python.

Open the command line of your operating system (see help for Linux, Mac, (Windows) (for Windows users: open “Anaconda Prompt”) and create a new environment. Type the following line by line and press Enter after each line.

# check if conda is installed
conda --version

Create a new environment

Change the name my_environment in the code below to a name of your choice. (It’s common that variable values inside code snippets are enclosed by < and >, like below. If so, replace the name inclusive < > with a name of your choice.) This is the name of your environment and you have to type it when you start it later on, so make sure it’s not too long and easy to remember.

# create a new environment
conda create -n <my_environment> -y
# for exampel: conda create -n syntaxerror -y

You can see all environments created by you with the following command:

# list available environments
conda env list

Next we can activate our newly created environment. (Of course you have to insert your environments name in the place of <my_environment>.)

# activate your environment
conda activate <my_environment>

Install external packages with conda

External packages (libraries) are installed in an activated environment with the command conda install packagename. Install the packages jupyter and jupyterlab:

conda install jupyter jupyterlab pip -y

Install external packages with pip

Some libraries (external packages) are not available through conda’s package manager. We can use pythons package installer pip, which we have installed with the code above.

# important: activate your environment first
conda activate <my_environment>

# install the library flat with pip
pip install flat

Jupyter Notebooks & Jupyter Lab

Jupyter Notebooks are a great way to combine code and its output with documentary text and images all in one document. It’s also great for developing code, as you can execute blocks of code and see/ visualize the result inside the notebook.

(The material for this seminar has the form of a JupyterBook. It consists of Jupyter Notebooks and Markdown-Files, rendered as HTML.)

Run Jupyter Lab

After we have installed Jupyter Lab, we can run it from within our activated environment with the command:

# run jupyter lab
# (activate the environment first)
jupyter lab

Depending on your needs there are some additional arguments like:

jupyter lab --browser = False

This will not open Jupyter Lab automatically in a browser. Instead it will print a URL to the terminal, which we can paste into a browser of our choice.

jupyter lab --notebook-dir = D:/

Define the directory in which you want to launch Jupyter Lab. For e.g. if you’re working on Windows and have your installation on C and your files on D.

Deactivate conda

When we’ve finished our work, we can deactivate the environment with:

# deactivate environment
conda deactivate

Remove an environment

conda remove -n <my environment> --all

Appendix

Terminal emulators

Here are some terminal emulators (beyond the default one of your OS):

Cross Platform

Alacritty

Kitty

Tabby

Linux

Terminator

Konsole

Mac

iTerm2