Closed bshoshany closed 1 month ago
I can confirm this at my end as well. The # %%
comments are also kept if you write a .py
script with cell comments (# %%
) from scratch and export that to an .ipynb
file using the "Jupyter: Export Current File as Jupyter Notebook" command. You don't need to have converted from an .ipynb
notebook file to a .py
script first to observe this behavior.
This behavior is new in 2024.7.0. If I install 2024.2.0, I get the old behavior which just converts the blocks starting with # %%
to code cells and leaves out the # %%
comments themselves, as expected. In versions after 2024.2.0 and before 2024.7.0, the export fails with the error message Extension 'vscode.ipynb' is not known or not activated. View Jupyter [log](command:jupyter.viewOutput) for further details
,
I am using VS Code 1.92.2 running on Windows with a remote in WSL 2 (Ubuntu 22.04.4 LTS), in a Python 3.11.9 environment.
closing as dupe of https://github.com/microsoft/vscode-jupyter/issues/15778
Environment data
Expected behaviour
When VS Code exports a notebook into a Python script, it indicates where each cell starts by adding a comment of the form
# %%
. When exporting a Python script in this format back into a notebook, these comments, which only indicate where each cell starts and are not actual code comments, should not be included in the actual cell.Actual behaviour
The
# %%
comments are included in the cells, creating messy and redundant code.Steps to reproduce:
test.ipynb
print("This is a code cell")
to the code cell.test.py
The contents of the script will be:
Note: Steps 1-7 aren't actually necessary, they are only meant to demonstrate that this file is what VS Code considers to be the equivalent of the notebook we created.
test2.ipynb
Note how the comment
# %%
, which is used to indicate the beginning of a cell and is not part of the actual code in the cell, is erroneously included in the cell. In larger notebooks, this happens in every code cell (but not Markdown cells):Note that jupytext, which can also export and import notebook from the "percent" format, doesn't have this issue:
will correctly generate the notebook without the
# %%
comments.