From e8b715807b59d057f0616ab0392dd3cac4c49de0 Mon Sep 17 00:00:00 2001 From: Adriane Boyd Date: Sun, 5 Nov 2023 15:40:40 +0100 Subject: [PATCH] Improve checks for colab (also helps displacy) --- spacy/util.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/spacy/util.py b/spacy/util.py index f02b998ca..63d7ea466 100644 --- a/spacy/util.py +++ b/spacy/util.py @@ -1097,17 +1097,26 @@ def is_cwd(path: Union[Path, str]) -> bool: def is_in_jupyter() -> bool: - """Check if user is running spaCy from a Jupyter notebook by detecting the - IPython kernel. Mainly used for the displaCy visualizer. - RETURNS (bool): True if in Jupyter, False if not. + """Check if user is running spaCy from a Jupyter or Colab notebook by + detecting the IPython kernel. Mainly used for the displaCy visualizer. + RETURNS (bool): True if in Jupyter/Colab, False if not. """ # https://stackoverflow.com/a/39662359/6400719 + # https://stackoverflow.com/questions/15411967 try: - shell = get_ipython().__class__.__name__ # type: ignore[name-defined] - if shell == "ZMQInteractiveShell": + if get_ipython().__class__.__name__ == "ZMQInteractiveShell": # type: ignore[name-defined] return True # Jupyter notebook or qtconsole + if get_ipython().__class__.__module__ == "google.colab._shell": # type: ignore[name-defined] + return True # Colab notebook except NameError: return False # Probably standard Python interpreter + # additional check for Colab + try: + import google.colab + + return True # Colab notebook + except ImportError: + return False return False