Cannot import name 'contextfilter' from 'jinja2' error when importing fastbook

I get an error when trying to run import fastbook. It is blocking me from following the course. Any ideas how to resolve it?

When I run this:

!pip install -Uqq fastbook
import fastbook
fastbook.setup_book()

I get this error output:

ImportError                               Traceback (most recent call last)
/tmp/ipykernel_28/2566296125.py in <module>
      1 #hide
      2 get_ipython().system('pip install -Uqq fastbook')
----> 3 import fastbook
      4 fastbook.setup_book()

/opt/conda/lib/python3.7/site-packages/fastbook/__init__.py in <module>
     12 try: import sentencepiece
     13 except ModuleNotFoundError: warn("Missing `sentencepiece` - please run `pip install 'sentencepiece<0.1.90'`")
---> 14 try: from nbdev.showdoc import *
     15 except ModuleNotFoundError: warn("Missing `nbdev` - please install it")
     16 try:

/opt/conda/lib/python3.7/site-packages/nbdev/__init__.py in <module>
      5 
      6 if IN_IPYTHON:
----> 7     from .showdoc import show_doc

/opt/conda/lib/python3.7/site-packages/nbdev/showdoc.py in <module>
      9 from .export import *
     10 from .sync import *
---> 11 from nbconvert import HTMLExporter
     12 from fastcore.utils import IN_NOTEBOOK
     13 

/opt/conda/lib/python3.7/site-packages/nbconvert/__init__.py in <module>
      2 
      3 from ._version import version_info, __version__
----> 4 from .exporters import *
      5 from . import filters
      6 from . import preprocessors

/opt/conda/lib/python3.7/site-packages/nbconvert/exporters/__init__.py in <module>
      1 from .base import (export, get_exporter, 
      2                    ExporterNameError, get_export_names)
----> 3 from .html import HTMLExporter
      4 from .slides import SlidesExporter
      5 from .templateexporter import TemplateExporter

/opt/conda/lib/python3.7/site-packages/nbconvert/exporters/html.py in <module>
     10 from traitlets.config import Config
     11 from jupyter_core.paths import jupyter_path
---> 12 from jinja2 import contextfilter
     13 
     14 from nbconvert.filters.highlight import Highlight2HTML

ImportError: cannot import name 'contextfilter' from 'jinja2' (/opt/conda/lib/python3.7/site-packages/jinja2/__init__.py)

I’ve tried restarting the kernel but it doesn’t change anything.

where do you run this, colab, etc?

Hey, thanks for your reply. Sorry for being unclear in my original post.

I am using Gradient. I am currently on 02_production and have been able to follow the course so far. Yesterday I could run the following (which is in the start of 02_production) without any errors:

!pip install -Uqq fastbook
import fastbook
fastbook.setup_book()

When I ran it this morning I got the error in my post. I tried running it again now and now it works without an error! So my problem is resolved. Thank you again for replying.

1 Like