def require_packages_not_changed():
    """Verify notebook test does not change packages in the Python test environment.

    Raise AssertionError if the pre-existing set of Python packages changes in any way.
    """
    cirq_packages = set(m.name for m in list_modules()).union(["cirq"])
    packages_before = set(
        (d.name, d.version)
        for d in importlib.metadata.distributions()
        if d.name not in cirq_packages
    )
    yield
    packages_after = set(
        (d.name, d.version)
        for d in importlib.metadata.distributions()
        if d.name not in cirq_packages
    )
    assert packages_after == packages_before
Verify notebook test does not change packages in the Python test environment.

    Raise AssertionError if the pre-existing set of Python packages changes in any way.
    
dev_tools/notebooks/notebook_test.py