Bug similar with #307
nbconvert version: 4.1.0
python version: Python 2.7.11 :: Anaconda 2.5.0 (64-bit)
I have this error using the Python API interface any cell with non ascii character
Cell generate error:
# 帽
Code run:
import nbformat
from nbconvert.preprocessors import ExecutePreprocessor
nb_base = 'test_error.ipynb'
with open(nb_base, 'rt') as f:
nb = nbformat.read(f, as_version=4)
ep = ExecutePreprocessor(timeout=600)
ep.preprocess(nb, {})
with open('out-'+nb_base, 'wt') as f:
nbformat.write(nb, f)
Traceback (most recent call last):
File "error_nb.py", line 12, in
nbformat.write(nb, f)
File "C:\Users\Javier\Anaconda\lib\site-packages\nbformat__init__.py", line 169, in write
fp.write(s)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xf1' in position 469: ordinal not in range(128)
If I run, works fine.
jupyter nbconvert --to notebook --execute test_error.ipynb --output test_error.ipynb
When using nbformat.write on Python 2, make sure to use the unicode-aware io.open (Python 3 open) instead of the old builtin open.
@minrk Shouldn't using from builtins import open (in the vein of the py2to3 guide) also work?
Only if you have python-future installed. io.open is part of the standard library, so you don't need any extra packages to use it.
You'd think that a package labeled builtins wouldn't require an extra package to use it :).
Ok closing this.
You'd think that a package labeled builtins wouldn't require an extra package to use it :).
Compatibility weirdness. The module containing builtin functions is builtins on Python 3, and __builtin__ on Python 2. python-future provides builtins on Python 2.
Personally I don't like python-future because of this kind of thing - six is a more conservative alternative that I tend to use. But a lot of people do like future, because it lets you write code that looks more like regular Python 3 and make it work on Python 2.
Most helpful comment
When using nbformat.write on Python 2, make sure to use the unicode-aware
io.open(Python 3 open) instead of the old builtinopen.