I have noticed that “pip install” produces an “invalid syntax” error when opening and re-running a cell in a .ipynb notebook that has been re-opened

pip install works in new cell but not in previously created

Note – the top cell is the newly created cell while the bottom is from the creation of the notebook.

I have noticed a strange quirk in jupyter lab/collab. When I am re-opening a previously created notebook, the pip install command produces an error. I have been getting around this error by just copying and pasting the command into a new cell and running that cell, which works. Can anybody explain in detail why this would be happening?

After a few searches, I have not found a solution. I strive to understand how python works under the engine and want to understand any possible mechanisms that would be causing this error.

Answer

!pip install in Jupyter is a shell command.

pip itself is not a Python statement, therefore not valid Syntax