microsoft / vscode-jupyter

VS Code Jupyter extension
https://marketplace.visualstudio.com/items?itemName=ms-toolsai.jupyter
MIT License
1.28k stars 290 forks source link

Interactive Python line numbers don't match those of the code #14661

Open alvaropp opened 11 months ago

alvaropp commented 11 months ago

Applies To

What happened?

I always find that the line numbers listed in errors in an interactive Python session don't match those of the code.

For example, see the following code:

# %%
"""This is a test script."""

# %%
import math

# %% Helper functions
def take_sqrt(x):
    return math.sqrt(x)

# %%
take_sqrt(20 / 0)

# %%

which provides the following error with wrong line numbers:

Screenshot 2023-11-03 at 09 36 28

There has been talk about this issue in the past couple of years and I've seen a few issues closed, but this happens to me every time. It's related to the fact that I have a couple of new lines after the cell delimiter # %%, but I find this very convenient to provide visual separation!

Many thanks 😄

VS Code Version

Version: 1.83.1 (Universal) Commit: f1b07bd25dfad64b0167beb15359ae573aecd2cc Date: 2023-10-10T23:46:55.789Z Electron: 25.8.4 ElectronBuildId: 24154031 Chromium: 114.0.5735.289 Node.js: 18.15.0 V8: 11.4.183.29-electron.0 OS: Darwin arm64 22.6.0

Jupyter Extension Version

v2023.10.1003070148

Jupyter logs

Visual Studio Code (1.83.1, undefined, desktop)
Jupyter Extension Version: 2023.10.1003070148.
Python Extension Version: 2023.20.0.
Platform: darwin (arm64).
Workspace folder ~/Desktop/test_folder, Home = /Users/username
09:39:25.849 [info] Start refreshing Kernel Picker (1699000765849)
09:39:25.854 [info] Using Pylance
09:39:26.464 [info] Process Execution: ~/Desktop/test_folder/.venv/bin/python -m pip list
09:39:26.969 [info] End refreshing Kernel Picker (1699000765849)
09:39:31.121 [info] Starting interactive window for resource '~/Desktop/test_folder/test.py' with controller '.jvsc74a57bd030bc42fd7df1b8c6109f665d8ab7f8f06b7d5768f500ac97364082a3098dffee.~/Desktop/test_folder/.venv/python.~/Desktop/test_folder/.venv/python.-m#ipykernel_launcher (Interactive)'
09:39:31.183 [info] Start refreshing Interpreter Kernel Picker
09:39:31.183 [info] Start refreshing Kernel Picker (1699000771183)
09:39:31.275 [info] Process Execution: ~/Desktop/test_folder/.venv/bin/python ~/.vscode/extensions/ms-toolsai.jupyter-2023.10.1003070148-darwin-arm64/pythonFiles/normalizeSelection.py
09:39:31.278 [info] Starting Kernel startUsingPythonInterpreter, .jvsc74a57bd030bc42fd7df1b8c6109f665d8ab7f8f06b7d5768f500ac97364082a3098dffee.~/Desktop/test_folder/.venv/python.~/Desktop/test_folder/.venv/python.-m#ipykernel_launcher  (Python Path: ~/Desktop/test_folder/.venv/bin/python, Venv, .venv, 3.11.6) for '/Interactive-1.interactive' (disableUI=false)
09:39:31.283 [info] Process Execution: ~/Desktop/test_folder/.venv/bin/python -c "import ipykernel; print(ipykernel.__version__); print("5dc3a68c-e34e-4080-9c3e-2a532b2ccb4d"); print(ipykernel.__file__)"
09:39:31.299 [info] Process Execution: ~/Desktop/test_folder/.venv/bin/python -m ipykernel_launcher --ip=127.0.0.1 --stdin=9004 --control=9002 --hb=9001 --Session.signature_scheme="hmac-sha256" --Session.key=b"ed50f31c-65b9-4bb9-bb94-a0d90f87c3bb" --shell=9003 --transport="tcp" --iopub=9005 --f=~/Library/Jupyter/runtime/kernel-v2-7898LoX7YTSYLhsD.json
    > cwd: ~/Desktop/test_folder/analysis
09:39:31.483 [info] ipykernel version & path 6.26.0, ~/Desktop/test_folder/.venv/lib/python3.11/site-packages/ipykernel/__init__.py for ~/Desktop/test_folder/.venv/bin/python
09:39:31.682 [warn] StdErr from Kernel Process 0.00s - Debugger warning: It seems that frozen modules are being used, which may
0.00s - make the debugger miss breakpoints. Please pass -Xfrozen_modules=off
0.00s - to python to disable frozen modules.
0.00s - Note: Debugging will proceed. Set PYDEVD_DISABLE_FILE_VALIDATION=1 to disable this validation.
09:39:31.973 [info] Process Execution: ~/Desktop/test_folder/.venv/bin/python ~/.vscode/extensions/ms-toolsai.jupyter-2023.10.1003070148-darwin-arm64/pythonFiles/printJupyterDataDir.py
09:39:31.974 [info] Generated code for 1 = <ipython-input-1-6f4bf97a452c> with 13 lines
09:39:31.984 [info] Kernel acknowledged execution of cell 1 @ 1699000771984
09:39:32.097 [info] End cell 1 execution @ 1699000772097, started @ 1699000771984, elapsed time = 0.113s
09:39:32.098 [warn] Cancel all remaining cells due to cancellation or failure in execution
09:39:32.653 [info] End refreshing Kernel Picker (1699000771183)

Coding Language and Runtime Version

Python 3.11.6

Language Extension Version (if applicable)

Python v2023.20.0

Anaconda Version (if applicable)

No response

Running Jupyter locally or remotely?

Local

amunger commented 11 months ago

It looks like you are using run selection in interactive window? Try turning off the special normalization that occurs when that command is run: "jupyter.interactiveWindow.textEditor.normalizeSelection": false

alvaropp commented 11 months ago

Thanks, I'll try this.

There seems to be something else going on, as I'm also getting mismatched line numbers when using the embedded terminal to execute Python scripts.

alvaropp commented 11 months ago

Your suggestion fixes the above toy example, but I'm still seeing problems in longer scripts!

amunger commented 11 months ago

looks like this happens when a selection starts with one or more blank lines. Are you seeing any other cases of this? image

alvaropp commented 11 months ago

Not that I can think of. As I noted above, I always try to have a couple of blank lines before a new section (# %%). The example I posted at the beginning of the issue works fine with your suggested change ("jupyter.interactiveWindow.textEditor.normalizeSelection": false). However, I'm finding mismatched lines with larger scripts (cannot share).