Reading units from a pickle file with Pint

Posted on Tue 30 May 2023 in Pylance, Python, Pint • Tagged with Pylance, Python, Pint

I have a Python package called cloudmodel that uses Pint to define units and adds some to the default registry. When I read a pickle file that use these units, I get an error:

int.errors.UndefinedUnitError: 'usd' is not defined in the unit registry

The way to fix it is to set the application registry to the one used by cloudmodel:

from cloudmodel.unified.units import ureg
from pint import set_application_registry

set_application_registry(ureg)