Installing a package via pip in code

I came across this snippet at Hello Qubit  |  Cirq  |  Google Quantum AI

try:
    import cirq
except ImportError:
    print("installing cirq...")
    !pip install --quiet cirq
    import cirq

    print("installed cirq.")

Since when has installing a package via pip in code so easy ? !pip install --quiet package-name
The answers here show all sorts of ways to run an exec but no mention of !pip

Or can this be done only via a notebook ?

This is part of ipython (Introducing IPython — IPython 8.4.0 documentation), which is also part of Jupyter notebooks.

1 Like