Nbdev_export raises JSONDecodeError

Hi, I have an issue with nbdev version 2.3.9

When running nbdev.nbdev_export() in a notebook cell this is the output:

---------------------------------------------------------------------------
JSONDecodeError                           Traceback (most recent call last)
Cell In[8], line 2
      1 #| hide
----> 2 import nbdev; nbdev.nbdev_export()

File ~/mambaforge/lib/python3.10/site-packages/fastcore/script.py:110, in call_parse.<locals>._f(*args, **kwargs)
    107 @wraps(func)
    108 def _f(*args, **kwargs):
    109     mod = inspect.getmodule(inspect.currentframe().f_back)
--> 110     if not mod: return func(*args, **kwargs)
    111     if not SCRIPT_INFO.func and mod.__name__=="__main__": SCRIPT_INFO.func = func.__name__
    112     if len(sys.argv)>1 and sys.argv[1]=='': sys.argv.pop(1)

File ~/mambaforge/lib/python3.10/site-packages/nbdev/doclinks.py:137, in nbdev_export(path, **kwargs)
    135 if os.environ.get('IN_TEST',0): return
    136 files = nbglob(path=path, **kwargs)
--> 137 for f in files: nb_export(f)
    138 add_init(get_config().lib_path)
    139 _build_modidx()

File ~/mambaforge/lib/python3.10/site-packages/nbdev/export.py:48, in nb_export(nbname, lib_path, procs, debug, mod_maker, name)
     46 if lib_path is None: lib_path = get_config().lib_path
     47 exp = ExportModuleProc()
---> 48 nb = NBProcessor(nbname, [exp]+L(procs), debug=debug)
...
    354 except StopIteration as err:
--> 355     raise JSONDecodeError("Expecting value", s, err.value) from None
    356 return obj, end

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

When running nbdev_export in the terminal this is the output:

Traceback (most recent call last):
  File "/home/arthur/mambaforge/bin/nbdev_export", line 10, in <module>
    sys.exit(nbdev_export())
  File "/home/arthur/mambaforge/lib/python3.10/site-packages/fastcore/script.py", line 119, in _f
    return tfunc(**merge(args, args_from_prog(func, xtra)))
  File "/home/arthur/mambaforge/lib/python3.10/site-packages/nbdev/doclinks.py", line 137, in nbdev_export
    for f in files: nb_export(f)
  File "/home/arthur/mambaforge/lib/python3.10/site-packages/nbdev/export.py", line 48, in nb_export
    nb = NBProcessor(nbname, [exp]+L(procs), debug=debug)
  File "/home/arthur/mambaforge/lib/python3.10/site-packages/nbdev/process.py", line 92, in __init__
    self.nb = read_nb(path) if nb is None else nb
  File "/home/arthur/mambaforge/lib/python3.10/site-packages/execnb/nbio.py", line 57, in read_nb
    res = dict2nb(_read_json(path, encoding='utf-8'))
  File "/home/arthur/mambaforge/lib/python3.10/site-packages/execnb/nbio.py", line 16, in _read_json
    return loads(Path(self).read_text(encoding=encoding, errors=errors))
  File "/home/arthur/mambaforge/lib/python3.10/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "/home/arthur/mambaforge/lib/python3.10/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/home/arthur/mambaforge/lib/python3.10/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

When running nbdev_clean in the terminal this is the output:

home/arthur/mambaforge/lib/python3.10/site-packages/nbdev/clean.py:115: UserWarning: Failed to clean notebook
  warn(f'{warn_msg}')
/home/arthur/mambaforge/lib/python3.10/site-packages/nbdev/clean.py:116: UserWarning: 'cells'
  warn(e)

Any help would be greatly appreciated as I’ve seen similar errors in the forums but not with these commands :pray:

2 Likes

This happened to me too today. There doesn’t seem to be any information on what causes the error.

2 Likes

I was seeing a similar error and was able to trace it to the conversion of some double quotes in a notebook markdown cell to a unicode version (by Quarto?). I changed the double quotes to single quotes and the error went away.

You found the needle in the trace stack. Thank you sir.

1 Like

For me, the error appears related to Windows. According to https://dev.to/methane/python-use-utf-8-mode-on-windows-212i (h/t pass encoding=utf8 when opening files by xl0 · Pull Request #1288 · fastai/nbdev · GitHub), setting an environment variable should solve the problem. And I just confirmed that that works, for my case anyway. (I can now successfully run the thing that was triggering the error with the Unicode double quotes back in place.) Specifically, the steps I followed:

  1. Open Powershell
  2. Run [System.Environment]::SetEnvironmentVariable('PYTHONUTF8','1','tjhuegerich') from the prompt
  3. After closing and reopening Powershell, run dir env: to confirm that the PYTHONUTF8 environment variable has been persistently set.

Interesting, I didn’t mention I’m using Ubuntu 20.04.5 LTS so there is a Linux issue somewhere also.

Were the notebooks causing those errors ever touched by someone using Windows? I’m not sure I understand correctly how this stuff works, but I’m thinking that if the notebooks were created/saved on Windows, they could be encoded in a different way that then causes an error when opened on Linux. (I think preventing that kind of scenario is the purpose of this pull request.)

The notebooks were created using nbdev_new however, I created an empty GitHub repo and cloned that first.

1 Like

Even though this thread is closed, I’m going to add to it for posterity’s sake. I had the same error but a different detail, on Linux.

I was seeing an error:

json.decoder.JSONDecodeError: Extra data: line 3079 column 1 (char 19218471)

This was apparently due to my notebook file not being fully saved. (I have some extra media which makes the save take a while). Waiting for the jupyter “checkpoint” to be fully saved and re-running nbdev_export fixed the problem for me.

1 Like