pip install in cell

Python
packaging
Published

April 30, 2024

Sometimes, I want to install pip packages in a Jupyter notebook without specifying the packages and pinning them in a requirements.txt.

This is mainly for short notebooks that I want to share and that should run without any modifications.

I have now found a solution with two IPython magic commands.

%%capture suppresses cell output so that no information about the pip install or the already installed packages is added to the notebook.

%pip install installs the specified packages.

%%capture

%pip install goose3
from goose3 import Goose

url = "https://www.bbc.com/news/world-europe-68929873"

g = Goose()
article = g.extract(url=url)

article.meta_description
'Nicaragua accused Germany of breaching the UN genocide convention by sending military hardware to Israel.'

Jupyter