Pint: Physical Quantities
Pint is a Python package designed for defining, operating, and manipulating physical quantities. It allows for arithmetic operations between numerical values and units of measurement, as well as conversions between different units. The library includes a comprehensive list of physical units, prefixes, and constants, and its modular design enables users to extend or rewrite unit definitions. It integrates well with numerical libraries like NumPy and Pandas (via `pint-pandas`) and is actively maintained, with the latest stable version 0.25.3 released on March 19, 2026.
Warnings
- breaking Minimum Python version support has increased over time. For Pint v0.25.3, Python 3.11+ is required. Older versions like 0.18 required Python 3.8+ and Numpy 1.19+; version 0.10 required Python 3.6+. Check the changelog for specific version compatibility.
- gotcha Creating multiple instances of `UnitRegistry` across different modules in your project can lead to `ValueError: Cannot operate with Quantity and Quantity of different registries.` when performing operations between them.
- breaking Attempting to cast a `Quantity` with offset units (e.g., temperature in Celsius) to a boolean value will raise a `ValueError` since version 0.10. This prevents ambiguous boolean evaluations.
- gotcha The `auto_reduce_dimensions` and `autoconvert_to_preferred` parameters of `UnitRegistry` are `False` by default for performance reasons. This means units might not automatically simplify or convert to preferred forms unless explicitly enabled.
- gotcha There is a separate Python library for pulsar timing called `PINT` (all uppercase). Be careful not to confuse `pint` (lowercase, for physical quantities) with `PINT` when installing or searching, as they are distinct projects.
Install
-
pip install pint -
conda install -c conda-forge pint
Imports
- UnitRegistry
from pint import UnitRegistry
Quickstart
from pint import UnitRegistry
# Initialize the UnitRegistry, which stores unit definitions and handles conversions
ureg = UnitRegistry()
# Define quantities using numerical values and units from the registry
distance = 24.0 * ureg.meter
time = 8.0 * ureg.second
# Perform calculations; Pint automatically handles unit propagation
speed = distance / time
print(f"Calculated Speed: {speed}")
# Convert quantities to different compatible units
speed_kmh = speed.to(ureg.kilometer / ureg.hour)
print(f"Speed in kilometers per hour: {speed_kmh}")
# Quantities can also be created by parsing strings
force = ureg.Quantity("100 N")
print(f"Force from string: {force}")