hepunits is a Python library that provides a comprehensive set of units and physical constants specifically tailored for the High Energy Physics (HEP) system of units. It simplifies working with quantities in HEP research by offering well-defined units like MeV and constants such as the speed of light. The library is actively maintained, with regular updates for Python version compatibility and minor feature enhancements, currently at version 2.4.4.
pip install hepunitsVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to import and use units and physical constants. It also shows the optional integration with the Pint library for more advanced quantity handling, which requires installing `hepunits[pint]`.
Ensure your Python environment meets the minimum `requires_python` specified for the `hepunits` version you are using. Upgrade Python if necessary.
Install `hepunits` with the `pint` extra: `pip install hepunits[pint]`.
Always be mindful of the specific unit system (HEP) that hepunits employs. Convert to or from other unit systems explicitly when interacting with external libraries or data not using HEP units.
If you require unit-aware arithmetic and type safety, use the `hepunits.pint` integration by importing the `hepunits.pint` module and obtaining its unit registry.
pip install hepunits
Ensure both operands have compatible units, for example: `energy = 10 * hepunits.GeV; total_energy = energy + 5 * hepunits.GeV`
Import constants from `hepunits.constants`, or access them directly from the top-level `hepunits` module: `from hepunits.constants import c` or `import hepunits; speed_of_light = hepunits.c`