diff --git a/.gitignore b/.gitignore
index d3e751f..d2fddea 100644
--- a/.gitignore
+++ b/.gitignore
@@ -11,6 +11,7 @@ dist/
 MANIFEST
 *pytest_cache*
 .eggs
+.mypy_cache
 
 # WebDAV file system cache files
 .DAV/
diff --git a/CHANGES b/CHANGES
index 39f8248..b21be59 100644
--- a/CHANGES
+++ b/CHANGES
@@ -20,6 +20,8 @@ Pint Changelog
   disequality (Issue #1051, Thanks Guido Imperiale)
 - Fixed issue where quantities with a very large magnitude would throw an IndexError 
   when using to_compact()
+- Fixed crash when a Unit with prefix is declared for the first time while a Context
+  containing unit redefinitions is active (Issue #1062, Thanks Guido Imperiale)
 
 
 0.11 (2020-02-19)
diff --git a/pint/quantity.py b/pint/quantity.py
index 3c5bb86..454c462 100644
--- a/pint/quantity.py
+++ b/pint/quantity.py
@@ -17,6 +17,7 @@ import numbers
 import operator
 import re
 import warnings
+from typing import List
 
 from pkg_resources.extern.packaging import version
 
@@ -1795,38 +1796,46 @@ class Quantity(PrettyIPython, SharedRegistryObject):
 
         return self._REGISTRY.Measurement(copy.copy(self.magnitude), error, self._units)
 
+    def _get_unit_definition(self, unit: str) -> UnitDefinition:
+        try:
+            return self._REGISTRY._units[unit]
+        except KeyError:
+            # pint#1062: The __init__ method of this object added the unit to
+            # UnitRegistry._units (e.g. units with prefix are added on the fly the
+            # first time they're used) but the key was later removed, e.g. because
+            # a Context with unit redefinitions was deactivated.
+            self._REGISTRY.parse_units(unit)
+            return self._REGISTRY._units[unit]
+
     # methods/properties that help for math operations with offset units
     @property
-    def _is_multiplicative(self):
+    def _is_multiplicative(self) -> bool:
         """Check if the Quantity object has only multiplicative units."""
         return not self._get_non_multiplicative_units()
 
-    def _get_non_multiplicative_units(self):
+    def _get_non_multiplicative_units(self) -> List[str]:
         """Return a list of the of non-multiplicative units of the Quantity object."""
-        offset_units = [
+        return [
             unit
-            for unit in self._units.keys()
-            if not self._REGISTRY._units[unit].is_multiplicative
+            for unit in self._units
+            if not self._get_unit_definition(unit).is_multiplicative
         ]
-        return offset_units
 
-    def _get_delta_units(self):
+    def _get_delta_units(self) -> List[str]:
         """Return list of delta units ot the Quantity object."""
-        delta_units = [u for u in self._units.keys() if u.startswith("delta_")]
-        return delta_units
+        return [u for u in self._units if u.startswith("delta_")]
 
-    def _has_compatible_delta(self, unit):
+    def _has_compatible_delta(self, unit: str) -> bool:
         """"Check if Quantity object has a delta_unit that is compatible with unit
         """
         deltas = self._get_delta_units()
         if "delta_" + unit in deltas:
             return True
-        else:  # Look for delta units with same dimension as the offset unit
-            offset_unit_dim = self._REGISTRY._units[unit].reference
-            for d in deltas:
-                if self._REGISTRY._units[d].reference == offset_unit_dim:
-                    return True
-        return False
+        # Look for delta units with same dimension as the offset unit
+        offset_unit_dim = self._get_unit_definition(unit).reference
+        return any(
+            self._get_unit_definition(d).reference == offset_unit_dim for d in deltas
+        )
 
     def _ok_for_muldiv(self, no_offset_units=None):
         """Checks if Quantity object can be multiplied or divided

unit redefinition in a context breaks transform in another context
```python
ureg = pint.UnitRegistry()
for i in range(2):
    print(f"run {i}")
    ctx = pint.Context.from_lines(["@context _", "cal = 4 J"])
    with ureg.context("sp", ctx):
        u = ureg.Unit("nm")
        q = ureg.Quantity(1, u)
        q.to('J')
```
Output:
```
run 0
run 1
KeyError: 'nanometer'
```
The first run never fails.
The failure on the second run only happens if
1. ctx is recreated from lines between runs, and
2. the Unit instance is created while the contexts are enabled, and
3. the target unit is one used as a source in a redefinition in ctx (``q.to('ft')`` has no problem)
diff --git a/pint/testsuite/test_issues.py b/pint/testsuite/test_issues.py
index 230b797..a3be41f 100644
--- a/pint/testsuite/test_issues.py
+++ b/pint/testsuite/test_issues.py
@@ -459,6 +459,19 @@ class TestIssues(QuantityTestCase):
         p = (q ** q).m
         np.testing.assert_array_equal(p, a ** a)
 
+    def test_issue507(self):
+        # leading underscore in unit works with numbers
+        ureg.define("_100km = 100 * kilometer")
+        battery_ec = 16 * ureg.kWh / ureg._100km  # noqa: F841
+        # ... but not with text
+        ureg.define("_home = 4700 * kWh / year")
+        with self.assertRaises(AttributeError):
+            home_elec_power = 1 * ureg._home  # noqa: F841
+        # ... or with *only* underscores
+        ureg.define("_ = 45 * km")
+        with self.assertRaises(AttributeError):
+            one_blank = 1 * ureg._  # noqa: F841
+
     def test_issue523(self):
         src, dst = UnitsContainer({"meter": 1}), UnitsContainer({"degF": 1})
         value = 10.0
@@ -653,28 +666,32 @@ class TestIssues(QuantityTestCase):
         with self.assertRaises(DimensionalityError):
             q.to("joule")
 
-    def test_issue507(self):
-        # leading underscore in unit works with numbers
-        ureg.define("_100km = 100 * kilometer")
-        battery_ec = 16 * ureg.kWh / ureg._100km  # noqa: F841
-        # ... but not with text
-        ureg.define("_home = 4700 * kWh / year")
-        with self.assertRaises(AttributeError):
-            home_elec_power = 1 * ureg._home  # noqa: F841
-        # ... or with *only* underscores
-        ureg.define("_ = 45 * km")
-        with self.assertRaises(AttributeError):
-            one_blank = 1 * ureg._  # noqa: F841
-
     def test_issue960(self):
         q = (1 * ureg.nanometer).to_compact("micrometer")
         assert q.units == ureg.nanometer
         assert q.magnitude == 1
 
+    @helpers.requires_numpy()
+    def test_issue973(self):
+        """Verify that an empty array Quantity can be created through multiplication."""
+        q0 = np.array([]) * ureg.m  # by Unit
+        q1 = np.array([]) * ureg("m")  # by Quantity
+        assert isinstance(q0, ureg.Quantity)
+        assert isinstance(q1, ureg.Quantity)
+        assert len(q0) == len(q1) == 0
+
+    def test_issue1062(self):
+        # Must not be used by any other tests
+        assert "nanometer" not in ureg._units
+        for i in range(2):
+            ctx = Context.from_lines(["@context _", "cal = 4 J"])
+            with ureg.context("sp", ctx):
+                q = ureg.Quantity(1, "nm")
+                q.to("J")
+
 
-try:
+if np is not None:
 
-    @pytest.mark.skipif(np is None, reason="NumPy is not available")
     @pytest.mark.parametrize(
         "callable",
         [
@@ -706,17 +723,3 @@ try:
         type_before = type(q._magnitude)
         callable(q)
         assert isinstance(q._magnitude, type_before)
-
-    @pytest.mark.skipif(np is None, reason="NumPy is not available")
-    def test_issue973():
-        """Verify that an empty array Quantity can be created through multiplication."""
-        q0 = np.array([]) * ureg.m  # by Unit
-        q1 = np.array([]) * ureg("m")  # by Quantity
-        assert isinstance(q0, ureg.Quantity)
-        assert isinstance(q1, ureg.Quantity)
-        assert len(q0) == len(q1) == 0
-
-
-except AttributeError:
-    # Calling attributes on np will fail if NumPy is not available
-    pass

pip install -e .[dev]
parse_log_pytest
requirements.txt
pytest pytest-cov
apt-get update apt-get install -y gcc
3.7
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
certifi @ file:///croot/certifi_1671487769961/work/certifi
coverage==7.2.7
exceptiongroup==1.2.2
importlib-metadata==6.7.0
iniconfig==2.0.0
packaging==24.0
-e git+https://github.com/hgrecco/pint.git@54c53e3635d272340b555ac0157dd640ead537e0#egg=Pint
pluggy==1.2.0
pytest==7.4.4
pytest-cov==4.1.0
tomli==2.0.1
typing_extensions==4.7.1
zipp==3.15.0

name: pint
channels:
  - defaults
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=main
  - _openmp_mutex=5.1=1_gnu
  - ca-certificates=2025.2.25=h06a4308_0
  - certifi=2022.12.7=py37h06a4308_0
  - ld_impl_linux-64=2.40=h12ee557_0
  - libffi=3.4.4=h6a678d5_1
  - libgcc-ng=11.2.0=h1234567_1
  - libgomp=11.2.0=h1234567_1
  - libstdcxx-ng=11.2.0=h1234567_1
  - ncurses=6.4=h6a678d5_0
  - openssl=1.1.1w=h7f8727e_0
  - pip=22.3.1=py37h06a4308_0
  - python=3.7.16=h7a1cb2a_0
  - readline=8.2=h5eee18b_0
  - setuptools=65.6.3=py37h06a4308_0
  - sqlite=3.45.3=h5eee18b_0
  - tk=8.6.14=h39e8969_0
  - wheel=0.38.4=py37h06a4308_0
  - xz=5.6.4=h5eee18b_1
  - zlib=1.2.13=h5eee18b_1
  - pip:
      - coverage==7.2.7
      - exceptiongroup==1.2.2
      - importlib-metadata==6.7.0
      - iniconfig==2.0.0
      - packaging==24.0
      - pint==0.12.dev29+g54c53e3
      - pluggy==1.2.0
      - pytest==7.4.4
      - pytest-cov==4.1.0
      - tomli==2.0.1
      - typing-extensions==4.7.1
      - zipp==3.15.0
prefix: /opt/conda/envs/pint

pint/testsuite/test_issues.py::TestIssues::test_issue1062
pint/testsuite/test_issues.py::TestIssues::test_alternative_angstrom_definition pint/testsuite/test_issues.py::TestIssues::test_angstrom_creation pint/testsuite/test_issues.py::TestIssues::test_issue104 pint/testsuite/test_issues.py::TestIssues::test_issue105 pint/testsuite/test_issues.py::TestIssues::test_issue170 pint/testsuite/test_issues.py::TestIssues::test_issue252 pint/testsuite/test_issues.py::TestIssues::test_issue29 pint/testsuite/test_issues.py::TestIssues::test_issue323 pint/testsuite/test_issues.py::TestIssues::test_issue339 pint/testsuite/test_issues.py::TestIssues::test_issue354_356_370 pint/testsuite/test_issues.py::TestIssues::test_issue45 pint/testsuite/test_issues.py::TestIssues::test_issue468 pint/testsuite/test_issues.py::TestIssues::test_issue50 pint/testsuite/test_issues.py::TestIssues::test_issue507 pint/testsuite/test_issues.py::TestIssues::test_issue52 pint/testsuite/test_issues.py::TestIssues::test_issue523 pint/testsuite/test_issues.py::TestIssues::test_issue532 pint/testsuite/test_issues.py::TestIssues::test_issue54 pint/testsuite/test_issues.py::TestIssues::test_issue54_related pint/testsuite/test_issues.py::TestIssues::test_issue61 pint/testsuite/test_issues.py::TestIssues::test_issue61_notNP pint/testsuite/test_issues.py::TestIssues::test_issue62 pint/testsuite/test_issues.py::TestIssues::test_issue625a pint/testsuite/test_issues.py::TestIssues::test_issue625b pint/testsuite/test_issues.py::TestIssues::test_issue625c pint/testsuite/test_issues.py::TestIssues::test_issue655a pint/testsuite/test_issues.py::TestIssues::test_issue655b pint/testsuite/test_issues.py::TestIssues::test_issue66 pint/testsuite/test_issues.py::TestIssues::test_issue66b pint/testsuite/test_issues.py::TestIssues::test_issue69 pint/testsuite/test_issues.py::TestIssues::test_issue783 pint/testsuite/test_issues.py::TestIssues::test_issue85 pint/testsuite/test_issues.py::TestIssues::test_issue856 pint/testsuite/test_issues.py::TestIssues::test_issue856b pint/testsuite/test_issues.py::TestIssues::test_issue86 pint/testsuite/test_issues.py::TestIssues::test_issue876 pint/testsuite/test_issues.py::TestIssues::test_issue902 pint/testsuite/test_issues.py::TestIssues::test_issue912 pint/testsuite/test_issues.py::TestIssues::test_issue93 pint/testsuite/test_issues.py::TestIssues::test_issue932 pint/testsuite/test_issues.py::TestIssues::test_issue960 pint/testsuite/test_issues.py::TestIssues::test_issues86b pint/testsuite/test_issues.py::TestIssues::test_micro_creation
.gitignore CHANGES pint/quantity.py
.gitignore CHANGES pint/quantity.py