jupyter / nbconvert

Jupyter Notebook Conversion
https://nbconvert.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
1.72k stars 564 forks source link

Getting Error when running nbconvert command : ImportError: cannot import name 'contextfilter' from 'jinja2' #1742

Open amish44 opened 2 years ago

amish44 commented 2 years ago

When running nbconvert command getting below error : Stack Trace: Traceback (most recent call last): File "/usr/local/bin/jupyter-nbconvert", line 5, in from nbconvert.nbconvertapp import main File "/usr/local/lib/python3.7/site-packages/nbconvert/init.py", line 4, in from .exporters import * File "/usr/local/lib/python3.7/site-packages/nbconvert/exporters/init.py", line 3, in from .html import HTMLExporter File "/usr/local/lib/python3.7/site-packages/nbconvert/exporters/html.py", line 14, in from jinja2 import contextfilter ImportError: cannot import name 'contextfilter' from 'jinja2' (/usr/local/lib/python3.7/site-packages/jinja2/init.py)

Command: jupyter nbconvert --execute --TemplateExporter.exclude_input=True --stdout --no-input --inplace --allow-errors --ExecutePreprocessor.timeout=-1 --ExecutePreprocessor.kernel_name=python /test.ipynb

Nbconvert version:

nbconvert 6.1.0

Other dependencies used: Package Version


argon2-cffi 21.3.0 argon2-cffi-bindings 21.2.0 asn1crypto 1.5.1 atomicwrites 1.4.0 attrs 21.4.0 awscli 1.16.263 azure-common 1.1.28 azure-core 1.23.0 azure-storage-blob 12.11.0 backcall 0.2.0 beautifulsoup4 4.10.0 bleach 4.1.0 boto3 1.9.253 botocore 1.12.253 cattrs 1.1.2 certifi 2020.12.5 cffi 1.15.0 chardet 3.0.4 charset-normalizer 2.0.12 click 8.1.0 colorama 0.4.1 configparser 5.0.1 cryptography 3.4.8 csvdiff 0.3.3 debugpy 1.6.0 decorator 5.1.1 defusedxml 0.7.1 docutils 0.15.2 entrypoints 0.4 idna 2.10 importlib-metadata 4.11.3 importlib-resources 5.6.0 ipykernel 6.10.0 ipython 7.32.0 ipython-genutils 0.2.0 isodate 0.6.1 jedi 0.18.1 Jinja2 3.1.1 jmespath 0.10.0 json5 0.9.6 jsonschema 4.4.0 jupyter-client 7.2.0 jupyter-contrib-core 0.3.3 jupyter-contrib-nbextensions 0.5.1 jupyter-core 4.9.2 jupyter-highlight-selected-word 0.2.0 jupyter-latex-envs 1.4.6 jupyter-nbextensions-configurator 0.4.1 jupyterlab 1.2.21 jupyterlab-pygments 0.1.2 jupyterlab-server 1.2.0 looker-sdk 7.20.0 lxml 4.8.0 MarkupSafe 2.1.1 matplotlib-inline 0.1.3 mistune 0.8.4 more-itertools 8.12.0 msrest 0.6.21 nbclient 0.5.13 nbconvert 6.1.0 nbformat 5.2.0 nest-asyncio 1.5.4 notebook 6.4.1 numpy 1.21.5 oauthlib 3.2.0 oscrypto 1.3.0 packaging 21.3 pandas 1.2.1 pandocfilters 1.5.0 parso 0.8.3 pexpect 4.8.0 pickleshare 0.7.5 pip 22.0.4 pluggy 0.13.1 prometheus-client 0.13.1 prompt-toolkit 3.0.28 psutil 5.9.0 ptyprocess 0.7.0 py 1.11.0 py4j 0.10.7 pyarrow 3.0.0 pyasn1 0.4.8 pycparser 2.21 pycryptodomex 3.14.1 Pygments 2.11.2 PyJWT 2.3.0 pyOpenSSL 19.1.0 pyparsing 3.0.7 pyrsistent 0.18.1 pyspark 2.4.0 pytest 4.6.5 python-dateutil 2.8.2 pytz 2020.5 PyYAML 5.1.2 pyzmq 22.3.0 redshift-connector 2.0.903 regex 2022.3.2 requests 2.27.1 requests-oauthlib 1.3.1 rsa 3.4.2 ruamel.yaml 0.17.21 ruamel.yaml.clib 0.2.6 s3transfer 0.2.1 scramp 1.4.1 Send2Trash 1.8.0 setuptools 57.5.0 six 1.16.0 snowflake-connector-python 2.3.10 soupsieve 2.3.1 terminado 0.13.3 testpath 0.6.0 tornado 6.1 traitlets 5.1.1 typing_extensions 4.1.1 urllib3 1.25.11 wcwidth 0.2.5 webencodings 0.5.1 wheel 0.37.1 zipp 3.7.0

Requirements.txt file: pyspark==2.4.0 nbconvert==6.1.0 notebook==6.4.1 pandas==1.2.1 jupyterlab==1.2.21 jupyter_contrib_nbextensions==0.5.1 pytest==4.6.5 configparser==5.0.1 boto3 snowflake-connector-python==2.3.10 awscli==1.16.263 csvdiff looker_sdk==7.20.0 ruamel.yaml pyarrow~=3.0.0 redshift-connector==2.0.903 regex==2022.3.2

Please let me know how can I fix it

astrofrog commented 2 years ago

I'm running into this too

qzhang234 commented 2 years ago

I'm running into this too. Would really appreciate some help

qzhang234 commented 2 years ago

I did conda install jinja2 and now I got a different error with xelatex:

(work) PS E:\work\2022_RheoXPCS> jupyter nbconvert --to pdf .\Read_C2_Write_CCS.ipynb
[NbConvertApp] Converting notebook .\Read_C2_Write_CCS.ipynb to pdf
[NbConvertApp] Support files will be in Read_C2_Write_CCS_files\
[NbConvertApp] Making directory .\Read_C2_Write_CCS_files
[NbConvertApp] Making directory .\Read_C2_Write_CCS_files
[NbConvertApp] Making directory .\Read_C2_Write_CCS_files
[NbConvertApp] Writing 32912 bytes to notebook.tex
[NbConvertApp] Building PDF
Traceback (most recent call last):
  File "C:\Users\qzhang234\Anaconda3\envs\work\Scripts\jupyter-nbconvert-script.py", line 10, in <module>
    sys.exit(main())
  File "C:\Users\qzhang234\Anaconda3\envs\work\lib\site-packages\jupyter_core\application.py", line 264, in launch_instance
    return super(JupyterApp, cls).launch_instance(argv=argv, **kwargs)
  File "C:\Users\qzhang234\Anaconda3\envs\work\lib\site-packages\traitlets\config\application.py", line 846, in launch_instance
    app.start()
  File "C:\Users\qzhang234\Anaconda3\envs\work\lib\site-packages\nbconvert\nbconvertapp.py", line 346, in start
    self.convert_notebooks()
  File "C:\Users\qzhang234\Anaconda3\envs\work\lib\site-packages\nbconvert\nbconvertapp.py", line 518, in convert_notebooks
    self.convert_single_notebook(notebook_filename)
  File "C:\Users\qzhang234\Anaconda3\envs\work\lib\site-packages\nbconvert\nbconvertapp.py", line 483, in convert_single_notebook
    output, resources = self.export_single_notebook(notebook_filename, resources, input_buffer=input_buffer)
  File "C:\Users\qzhang234\Anaconda3\envs\work\lib\site-packages\nbconvert\nbconvertapp.py", line 412, in export_single_notebook
    output, resources = self.exporter.from_filename(notebook_filename, resources=resources)
  File "C:\Users\qzhang234\Anaconda3\envs\work\lib\site-packages\nbconvert\exporters\exporter.py", line 181, in from_filename
    return self.from_file(f, resources=resources, **kw)
  File "C:\Users\qzhang234\Anaconda3\envs\work\lib\site-packages\nbconvert\exporters\exporter.py", line 199, in from_file
    return self.from_notebook_node(nbformat.read(file_stream, as_version=4), resources=resources, **kw)
  File "C:\Users\qzhang234\Anaconda3\envs\work\lib\site-packages\nbconvert\exporters\pdf.py", line 183, in from_notebook_node
    self.run_latex(tex_file)
  File "C:\Users\qzhang234\Anaconda3\envs\work\lib\site-packages\nbconvert\exporters\pdf.py", line 153, in run_latex
    return self.run_command(self.latex_command, filename,
  File "C:\Users\qzhang234\Anaconda3\envs\work\lib\site-packages\nbconvert\exporters\pdf.py", line 110, in run_command
    raise OSError("{formatter} not found on PATH, if you have not installed "
OSError: xelatex not found on PATH, if you have not installed xelatex you may need to do so. Find further instructions at https://nbconvert.readthedocs.io/en/latest/inst
all.html#installing-tex.
(work) PS E:\work\2022_RheoXPCS>
pawlodkowski commented 2 years ago

Also ran into this issue with nbconvert==6.1.0. Strange as I've been using the same script for months now without any problem, with all dependencies fixed to the specific versions. Is it possible that this older version of nbconvert got some kind of bug retroactively?

EDIT: a workaround for me was to upgrade to the latest version (pip install nbconvert==6.4.3). I guess it's not ideal, but hey... works for now.

bollwyvl commented 2 years ago

Jinja2 3.1.1

as the title suggests, jinja 3.1.x, one of the key upstreams of nbconvert, has deprecated a number of APIs. You might be able to pin to jinja<3.1 if you are relying on some bug/feature, but #1737 and subsequent have been released, restoring this compatibility.

we'll be trying to ensure better coverage of this in the future, and stay as compatible as possible, but as a tool and a library relied upon by other packages, it's rather hard for us to just blanket pin dependency versions, see #1740.

nbconvert==6.1.0

As has been suggested in a number of places related to this, if you start hard pinning individual values of dependencies, you likely need to pin, and check in, a complete solution to your environment, including all transient dependencies, and critically evaluate when you are forced to re-solve, e.g. because of security concerns.

A number of tools make this more tractable, such as:

proinsias commented 2 years ago

Still seeing this issue with nbconvert 6.5.0. Had to pin jinja2<3.1.

scchess commented 2 years ago

pip install nbconvert==6.4.3 worked for me.

lneuhaus commented 1 year ago

pip install nbconvert --upgrade solved this for me (installing version 7.2.9). I think the issue should be closed.

nevakrien commented 1 year ago

same issue rn on a new environment with everything baseline other than intel-pytorch-extension. tried playing with the tornado version since it helped me last time this error came up.

in genral this is really anoying because its on jupyter notebook which I use for alot of things and i see this error once a month

MostafaaElhadidy commented 5 months ago

pip install nbconvert==6.4.3 worked for me.

this actually worked! thanks