I usually use Matlab for processing results. However I'm required now to use Python. I want to set a breakpoint in a Python Jupyter notebook to go through functions. I was able to set up breakpoint but I can't actually go line by line debugging with this breakpoint. In Matlab I used to press F10 to do this.
In JupyterLab with the Xeus Python kernel (often tagged with 'XPython' when you are choosing the kernel) you can use the visual debugger. The run-next-line feature is the 'Next' button in the 'CALLSTACK' pane of the debugger panel. See the animation here for the 'Next' tooltip when hovering. And, although not featured in that animation, the tool tip actually shows 'Next (F10)' presently in my browser.
You can try it by clicking here. The session that spins up starts with a notebook open that has a nice introduction and guide to the features.
More can be found about the visual debugger here.
Current 'default' launches via MyBinder, have the Xeus Python kernel and the debugger available already. You can run the following code in a cell in any of those sessions to pull in the debugger demonstration notebook.
!curl -o debugger.ipynb https://raw.githubusercontent.com/jupyterlab/debugger/dfd6bf9d51a7a0cd78ca54f4173fcf527bd4d7fd/examples/index.ipynb
You'll find when you open it, that notebook also is set to trigger opening with the correct kernel necessary for debugging selected. Note that won't always be the first choice for when you start a new notebook.
Related
I wanted to give VSC a try for developing some Python programs, where I only used PyCharm before. One of the most helpful features for me in Pycharm was the PyDev Console, where I can quickly try small snippets of code (think 3-10 lines), and adjust it to work the way I want it to.
I see VSC has a console, but it's much more like the regular IDLE console, where it's kind of hard to write these snippets of code (fixing something 2 lines prior for example is pretty much impossible).
I've been searching for an extension that'll give me a PyCharm-like console experience in VSC, but have been unable to find one. Is is out there? Or is there another way to get to the same result (like setting up a custom console based on the same PyDev console)?
Have you tried Jupyter Notebook and Interactive? There are provided by the Jupyter Extension which is bound with Python Extension.
Open the Command Palette (Ctrl+Shift+P), with the command of:
Jupyter: Create New Blank Notebook
Jupyter: Create Interactive Window
You can refer to the official docs for more details.
I am still new to vscode, but I am having trouble getting some of the tools for python Jupyter notebooks to work in VSCode Version: 1.56.2 on ubuntu linux 20.04 LTS.
So according to the documentation, there are supposed to be buttons for debugging, including a button to "run code by line". This makes it easier to debug any code issues in a notebook cell. The documentation suggests the notebook interface should look like this.
The buttons in the upper left are the ones that I am interested in.
Now, when I look at my own interface, it looks like this.
So the two interfaces look very different. I am not sure if I need to change any settings in VSCode to enable these buttons. The documentation did not mention changes to any settings.
Any suggestions would be appreciated.
"Run code by line" has not yet been implemented for the new notebooks interface that you are seeing. In the meantime, you can opt back into the old interface with "Run code by line" support by doing the following:
Open your user settings.json by typing Ctrl+Shift+P > "Preferences: Open Settings (JSON)"
Add the following line to your user settings.json file:
"jupyter.experiments.optOutFrom": ["NativeNotebookEditor"]
If the workbench.editorAssociations setting is present in your settings.json file, delete it.
Reload VS Code for the new settings to take effect
In vscode 1.59 (see https://github.com/microsoft/vscode-docs/blob/vnext/release-notes/v1_59.md#jupyter-run-by-line)
Jupyter "Run By Line"
We've been working on supporting the "Run By Line" feature in Jupyter
notebooks. This feature is essentially a simplified debug mode that
lets you step through your cell's code line by line without any
complex debug UI. This is still experimental, but you can try it out
by setting "jupyter.experimental.debugging": true, installing
version 6 of ipykernel in your selected kernel, then clicking the "Run
By Line" button in the cell toolbar.
"jupyter.experimental.debugging": true
Of interest
We have been working on supporting debugging in Jupyter notebooks, so
that you can set breakpoints in notebook cells, execute cells
step-by-step, and use all other VS Code debugger features. This is
experimental, but you can try it out by setting
"jupyter.experimental.debugging": true, installing version 6 of
ipykernel in your selected kernel, then clicking the "Debug" button in
the notebook toolbar.
in vscode v.158, https://github.com/microsoft/vscode-docs/blob/vnext/release-notes/v1_58.md#jupyter-notebook-debugging
Make a debug point and just press F10 ; Debug will started automatically
I have my '.py' file, and I'm running its functions on the Python Interactive Window (the notebook-like feature of VSCODE).
All the variables and functions are stored only in this Jupyter environment. They're not even accessible on the terminal.
Img that shows the variables on my Jupyter env
When I click "Debug Cell" (as you see on the img), it starts debugging using the Jupyter Environment, with all variables, but it doesn't stop on the breakpoints I've put. Like it doesn't get linked with VSCODE.
If I type F5 or click on "Run and Debug", it starts the Debug Session running the entire .py file, which is not what I want.
In this case, it can access the breakpoints, but not my variables.
I just want to debug a specific part of my code using the variables that I have already created. I must have been missing something really simple.
The only answer I can find on the internet is "you should create a launch.json file", but it doesn't fix my situation.
Does anyone know what I'm missing?
When I click "Debug Cell" in the python file, it executes the result in the "Interactive" window and can stay at the breakpoint I set, and I can also see the value of the variable executed in the code in the "Interactive" window :
Since the debugging function of python code and "Interactive" function are provided by the python extension, and the kernel function of Jupyter also depends on the Jupyter extension, please try to reinstall these two extensions and reload VS Code.
My environment:
VS Code: 1.53.1 (user setup)
python: 3.9.1
VS Code extensions: Python(2021.1.502429796); Jupyter(2020.12.414227052); Pylance(2021.2.2).
OS: Windows_NT x64 10.0.19041
Is there a way to customize (e.g., JSON config) the 'Python Interactive' Window to iPython/Jupyter console that comes with the MS Python extension?
I would like to be able to adjust the size of the variable explorer pane within the window (it can't currently be resized and takes up a bit of screen space.
I would also like the iPython console to look/operate like the native iPython console. That is, have an open console and be able to submit commands just by hitting enter (vs. the default shift+enter). Ideally, I'd like to be able to have it look/operate like Spyder's interactive console. Any thoughts or ideas would be appreciated.
As a workaround I can launch an iPython session from the integrated terminal then select all code from the editor and send it to the terminal (which then gets sent into iPython), but this is not ideal and loses the advantage of the interactive window and the built-in variable explorer which is very nice! Thanks
I'm a developer on this extension. We don't currently have these customizations that you are looking for. But if you would like to report these as enhancements then you could report them on our github here:
https://github.com/microsoft/vscode-python/issues
That's the best way to get attention for adding a new feature.
I've recently switched to VSCode, and am wondering if there's a way to make the Python Interactive Window from the Jupyter support in VSCode work like the console in Spyder where I just have to select code and press ctrl+enter to send it, without having to create cells everytime.
For now I'm resigned to work with the Terminal until my code is clean and then create a cell when I have reusable code, and would like to just work directly with the PIW.
You can always change the default console setting by:
Opening the Command Palette (⇧⌘P)
Typing "Preferences:Open Settings (JSON)
Edit this line:
"python.dataScience.sendSelectionToInteractiveWindow": false
You should be able to do this with the latest python extension. The select the code you want to execute and press shift-enter. Is that not working?
For me (now) interactive mode runs after setting "jupyter.sendSelectionToInteractiveWindow": true
#FranciscoRZ. You should have seen a popup option for this, but if it didn't come up for you it can just be manually set in VSCode options. Just change this guy here:
Python->Data Science: Send Selection To Interactive Window
That should get you what you are looking for.
OP's Note: Accepting this answer because it will be the right anwser starting with the February release of VS Code Python
If you have the notebook saved as a python percentage script (which is more git friendly)
each "cell" will be delimited by # %% and the default run command is "Shift+Ctrl".
Later once you are working in the interactive window, If you want a particular cell you wrote on the fly to be in you script, there is one button which says "Paste code into file" right next to the recently executed cell in the interactive window.
And in case you are using the notebook for the sake of being able to later export it to html or pdf, once executed in the interactive window, there is an export button as well.