Libraries


A library is a collection of modules. It is a way to organize and distribute code. Python has a large standard library, and there are many other libraries available for specific tasks.


Library (A collection of modules)
│
├── Module_A.py (A single file with Python definitions and statements)
│   ├── function1()
│   ├── function2()
│   ├── class1()
│   └── class2()
│
├── Module_B.py
│   ├── function1()
│   ├── function2()
│   ├── class1()
│   └── class2()
│
└── Module_C.py
    ├── function1()
    ├── function2()
    ├── class1()
    └── class2()


Standard libraries


Standard libraries are those that are part of the Python installation. They are already available and can be used in your scripts without any additional installation.

Let’s practice with the most common library in Python: math, random, os, System.

import math

# use the math library to calculate the square root of 4
result = math.sqrt(4)
print(result)


import random

# use the random library to generate a random number between 0 and 1
result = random.random()
print(result)


import os

# use the os library to get the current working directory
result = os.getcwd()
print(result)


import System

# use the System library to get the current date and time
result = System.DateTime.Now
print(result)


External libraries


External libraries are those that are not part of the Python installation. They need to be installed before they can be used in your scripts.

These libraries can be installed using the package manager pip. But in Rhino you can also add a first line in your script to install them as such:

# r: pytoml==0.10.2

import pytoml  # it's now installed


🛠 Exercise


01: 🐍⬇️⬇️⬇️ Download the script here ⬇️⬇️⬇️🐍 02: 🐍⬇️⬇️⬇️ Download the script here ⬇️⬇️⬇️🐍 03: 🐍⬇️⬇️⬇️ Download the script here ⬇️⬇️⬇️🐍 04: 🐍⬇️⬇️⬇️ Download the script here ⬇️⬇️⬇️🐍