Repository: pybamm-team/PyBaMM

Issue:
Google Colab fails to install PyBaMM
### Discussed in https://github.com/pybamm-team/PyBaMM/discussions/2521
Google Colab has updated to Python 3.8 but now fails to install PyBaMM due to pybtex.

<div type='discussions-op-text'>

<sup>Originally posted by **reenubosepybamm** December  5, 2022</sup>
%pip install pybamm -q    # install PyBaMM if it is not installed
import pybamm
import numpy as np
import matplotlib.pyplot as plt
GIVING AN ERROR
PluginNotFound                            Traceback (most recent call last)
[<ipython-input-5-103384696aab>](https://localhost:8080/#) in <module>
      2 get_ipython().run_line_magic('pip', 'install pybamm --no-cache -q')
      3 get_ipython().run_line_magic('pip', 'install pybamm -q    # install PyBaMM if it is not installed')
----> 4 import pybamm
      5 import numpy as np
      6 import matplotlib.pyplot as plt

6 frames
[/usr/local/lib/python3.8/dist-packages/pybtex/plugin/__init__.py](https://localhost:8080/#) in _load_entry_point(group, name, use_aliases)
     79         for entry_point in pkg_resources.iter_entry_points(search_group, name):
     80             return entry_point.load()
---> 81     raise PluginNotFound(group, name)
     82 
     83 

PluginNotFound: plugin pybtex.database.input.bibtex not found</div>

Discussion:

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index b8d695b6a..b1f757d68 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,9 @@
 # [Unreleased](https://github.com/pybamm-team/PyBaMM/)
 
+## Bug fixes
+
+- Fix installation on `Google Colab` (`pybtex` and `Colab` issue) ([#2526](https://github.com/pybamm-team/PyBaMM/pull/2526))
+
 # [v22.11](https://github.com/pybamm-team/PyBaMM/tree/v22.11) - 2022-11-30
 
 ## Features
diff --git a/pybamm/citations.py b/pybamm/citations.py
index 756de395f..fdf282d40 100644
--- a/pybamm/citations.py
+++ b/pybamm/citations.py
@@ -5,6 +5,7 @@
 #
 import pybamm
 import os
+import sys
 import warnings
 import pybtex
 from pybtex.database import parse_file, parse_string, Entry
@@ -35,8 +36,9 @@ class Citations:
         # Dict mapping citations keys to BibTex entries
         self._all_citations: dict[str, str] = dict()
 
-        self.read_citations()
-        self._reset()
+        if "google.colab" not in sys.modules:
+            self.read_citations()
+            self._reset()
 
     def _reset(self):
         """Reset citations to default only (only for testing purposes)"""
@@ -143,7 +145,14 @@ class Citations:
 
 def print_citations(filename=None, output_format="text"):
     """See :meth:`Citations.print`"""
-    pybamm.citations.print(filename, output_format)
+    if "google.colab" in sys.modules:
+        raise ImportWarning(
+            "pybtex does not work with Google Colab due to a known bug -"
+            "https://bitbucket.org/pybtex-devs/pybtex/issues/148/."
+            "Please manually cite all the references."
+        )
+    else:
+        pybamm.citations.print(filename, output_format)
 
 
 citations = Citations()


Test patch:
diff --git a/tests/unit/test_citations.py b/tests/unit/test_citations.py
index 015a20bfd..c055a7bf5 100644
--- a/tests/unit/test_citations.py
+++ b/tests/unit/test_citations.py
@@ -39,6 +39,8 @@ class TestCitations(unittest.TestCase):
             citations.register("not a citation")
 
     def test_print_citations(self):
+        import sys
+
         pybamm.citations._reset()
 
         # Text Style
@@ -64,6 +66,12 @@ class TestCitations(unittest.TestCase):
         with self.assertRaisesRegex(pybamm.OptionError, "'text' or 'bibtex'"):
             pybamm.print_citations("test_citations.txt", "bad format")
 
+        # google colab issue - https://github.com/pybamm-team/PyBaMM/issues/2524
+        sys.modules["google.colab"] = "mock"
+        with self.assertRaisesRegex(ImportWarning, "pybtex does not work"):
+            pybamm.print_citations()
+        sys.modules.pop("google.colab")
+
     def test_overwrite_citation(self):
         # Unknown citation
         fake_citation = r"@article{NotACitation, title = {This Doesn't Exist}}"