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"])
Library |
Use |
|---|---|
|
Mathematical functions |
|
Operating system interfaces |
|
Regular expressions |
|
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.
Library |
Use |
|---|---|
|
Web design |
|
Plotting |
|
Matrices and arrays |
|
Data tables |
|
Scientific computing |
|
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