ipynbname icon indicating copy to clipboard operation
ipynbname copied to clipboard

Errors when running notebooks programmatically

Open micahjsmith opened this issue 3 years ago • 2 comments

When executing notebooks programmatically using papermill/nbconvert, ipynbname fails with an IndexError.

I've attached a reproducible example.

Archive.zip

pipenv sync
pipenv run python -m ipykernell install --name=ipynbname
pipenv run papermill ipynbname-demo.ipynb ipynbname-demo-executed.ipynb

The demo notebook just contains one cell,

import ipynbname
nb_fname = ipynbname.name()
nb_fname

You can see that running it with papermill returns an error, but the notebook executes fine from Jupyter Notebook/Lab and produces the correct nb name.

$ pipenv run papermill ipynbname-demo.ipynb ipynbname-demo-executed.ipynb
Input Notebook:  ipynbname-demo.ipynb
Output Notebook: ipynbname-demo-executed.ipynb

Executing:   0%|          | 0/1 [00:00<?, ?cell/s]Executing notebook with kernel: ipynbname

Executing: 100%|██████████| 1/1 [00:01<00:00,  1.26s/cell]
Executing: 100%|██████████| 1/1 [00:01<00:00,  1.48s/cell]
Traceback (most recent call last):
  File "/Users/micahsmith/workspace/sandbox/ipynbname/.venv/bin/papermill", line 8, in <module>
    sys.exit(papermill())
  File "/Users/micahsmith/workspace/sandbox/ipynbname/.venv/lib/python3.8/site-packages/click/core.py", line 829, in __call__
    return self.main(*args, **kwargs)
  File "/Users/micahsmith/workspace/sandbox/ipynbname/.venv/lib/python3.8/site-packages/click/core.py", line 782, in main
    rv = self.invoke(ctx)
  File "/Users/micahsmith/workspace/sandbox/ipynbname/.venv/lib/python3.8/site-packages/click/core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/Users/micahsmith/workspace/sandbox/ipynbname/.venv/lib/python3.8/site-packages/click/core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "/Users/micahsmith/workspace/sandbox/ipynbname/.venv/lib/python3.8/site-packages/click/decorators.py", line 21, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/Users/micahsmith/workspace/sandbox/ipynbname/.venv/lib/python3.8/site-packages/papermill/cli.py", line 250, in papermill
    execute_notebook(
  File "/Users/micahsmith/workspace/sandbox/ipynbname/.venv/lib/python3.8/site-packages/papermill/execute.py", line 122, in execute_notebook
    raise_for_execution_errors(nb, output_path)
  File "/Users/micahsmith/workspace/sandbox/ipynbname/.venv/lib/python3.8/site-packages/papermill/execute.py", line 234, in raise_for_execution_errors
    raise error
papermill.exceptions.PapermillExecutionError: 
---------------------------------------------------------------------------
Exception encountered at "In [1]":
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-1-48b4ae80b430> in <module>
      1 import ipynbname
----> 2 nb_fname = ipynbname.name()
      3 nb_fname

~/workspace/sandbox/ipynbname/.venv/lib/python3.8/site-packages/ipynbname/__init__.py in name()
     77         or raises a FileNotFoundError exception if it cannot be determined.
     78     """
---> 79     _, path = _find_nb_path()
     80     if path:
     81         return path.stem

~/workspace/sandbox/ipynbname/.venv/lib/python3.8/site-packages/ipynbname/__init__.py in _find_nb_path()
     59 def _find_nb_path() -> Union[Tuple[dict, PurePath], Tuple[None, None]]:
     60     try:
---> 61         kernel_id = _get_kernel_id()
     62     except (MultipleInstanceError, RuntimeError):
     63         return None, None  # Could not determine

~/workspace/sandbox/ipynbname/.venv/lib/python3.8/site-packages/ipynbname/__init__.py in _get_kernel_id()
     35     """
     36     connection_file = Path(ipykernel.get_connection_file()).stem
---> 37     kernel_id = connection_file.split('-', 1)[1]
     38     return kernel_id
     39 

IndexError: list index out of range

Thanks for a useful package! Making this work in more usage scenarios will undoubtedly make the package even more versatile and useful.

micahjsmith avatar Mar 20 '21 20:03 micahjsmith

same

SHuang-Broad avatar May 18 '23 16:05 SHuang-Broad

Same. Please help.

LinuxIsCool avatar Mar 07 '24 17:03 LinuxIsCool