Libraries

Imports

Libraries are included with the keyword import.

import math

math.cos(2)
import collections as col  # Change the namespace with `as`

counter = col.Counter(["a", "b", "c"])
Common built-in libraries.

Library

Use

math

Mathematical functions

os

Operating system interfaces

re

Regular expressions

sys

System-specific parameters and functions

Installing

PIP installs python libraries.

pip install numpy
# Or
python -m pip install numpy

Python packages are distributed as wheels. PyPI lists almost every public wheel. PIP grabs libraries from here.

Common external.

Library

Use

flask

Web design

matplotlib

Plotting

numpy

Matrices and arrays

pandas

Data tables

scipy

Scientific computing

torch

Neural networks

Environments

For a project, it is best practice to use a virtual environment instead of installing libraries globally. This is done with the python module venv.

python -m venv .venv  # Make the directory .venv to contain the libraries
source .venv/bin/activate  # Activate environment on Linux or MacOS
.venv/Scripts/activate  # Activate environment on Windows
deactivate  # Leaves environment

Furthermore, dependencies should be listed in a file that is named requirements.txt with their version.

Contents of requirements.txt. `
numpy==1.2
pandas>=2.1
pip install -r requirements.txt  # Install requiremements