jupytext
jupytext copied to clipboard
`jupytext --execute` causes `NameError: name __session__ is not defined`
When I execute Jupytext py:percent formatted notebooks in Jupyter Lab that use the __session__ variable to resolve the path to the notebook, it works.
When I execute such notebooks via jupytext --execute, it fails with NameError: name __session__ is not defined.
repro.py:
# ---
# jupyter:
# jupytext:
# cell_metadata_filter: -all
# formats: py:percent
# text_representation:
# extension: .py
# format_name: percent
# format_version: '1.3'
# jupytext_version: 1.16.7
# kernelspec:
# display_name: base
# language: python
# name: python3
# ---
# %%
print(f"{__session__=}")
Works in Jupyter Lab:
Fails when run via jupytext CLI:
❯ jupytext --execute ~/repro.py
[jupytext] Reading /home/jovyan/repro.py in format py
[jupytext] Executing notebook with kernel python3
Traceback (most recent call last):
File "/opt/conda/bin/jupytext", line 10, in <module>
sys.exit(jupytext())
^^^^^^^^^^
File "/opt/conda/lib/python3.12/site-packages/jupytext/cli.py", line 497, in jupytext
exit_code += jupytext_single_file(nb_file, args, log)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/conda/lib/python3.12/site-packages/jupytext/cli.py", line 734, in jupytext_single_file
exec_proc.preprocess(notebook, resources=resources)
File "/opt/conda/lib/python3.12/site-packages/nbconvert/preprocessors/execute.py", line 103, in preprocess
self.preprocess_cell(cell, resources, index)
File "/opt/conda/lib/python3.12/site-packages/nbconvert/preprocessors/execute.py", line 124, in preprocess_cell
cell = self.execute_cell(cell, index, store_history=True)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/conda/lib/python3.12/site-packages/jupyter_core/utils/__init__.py", line 165, in wrapped
return loop.run_until_complete(inner)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/conda/lib/python3.12/asyncio/base_events.py", line 691, in run_until_complete
return future.result()
^^^^^^^^^^^^^^^
File "/opt/conda/lib/python3.12/site-packages/nbclient/client.py", line 1062, in async_execute_cell
await self._check_raise_for_error(cell, cell_index, exec_reply)
File "/opt/conda/lib/python3.12/site-packages/nbclient/client.py", line 918, in _check_raise_for_error
raise CellExecutionError.from_cell_and_msg(cell, exec_reply_content)
nbclient.exceptions.CellExecutionError: An error occurred while executing the following cell:
------------------
print(f"{__session__=}")
------------------
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[1], line 1
----> 1 print(f"{__session__=}")
NameError: name '__session__' is not defined
In the meantime, is there some other way that a notebook can resolve its own path at runtime? I checked and the __file__ variable is not set either. Thanks in advance for any help!
Hi @ctcjab , I am afraid this has to do with a difference in how notebooks are executed in Jupyter Lab or nbconvert, which Jupytext uses (see the stack trace above).
Can I suggest that you confirm that the issue is there with the command line jupyter nbconvert (with an .ipynb notebook), and if so, raise an issue at https://github.com/jupyter/nbconvert ?
Thanks @mwouts, done in https://github.com/jupyter/nbconvert/issues/2213