Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable Python Interactive for use as a debugging console #1278

Open
geophpherie opened this issue Aug 14, 2019 · 64 comments
Open

Enable Python Interactive for use as a debugging console #1278

geophpherie opened this issue Aug 14, 2019 · 64 comments
Assignees
Labels
feature-request Request for new features or functionality interactive-window Impacts interactive window interactive-window-debugging

Comments

@geophpherie
Copy link

I think this is related to / mentioned deep in the comments here, but I'm not convinced they're quite duplicates.

I think it'd be tremendously useful to be able to also select Python Interactive as your debug terminal, essentially launching a Python Interactive window at a breakpoint with the state of your program loaded into it, giving you access to your variables. That way plotting functions and visualizations could be made use of pretty easily as a way of debugging. This varies slightly from the current "Debug Current File in Python Interactive window", which would run your entire script in Python Interactive, in that you are still able to configure the debugger launch.json. For my specific use case it would allow passing in command line args to the script, while still being able to debug in Python Interactive.

At a high level, I can see two ways of it happening:

  1. Use only Python Interactive and allow it to continue execution of the code as the full debug tool.
  2. Use the main debug terminal for the debugging flow, but at a breakpoint launch the Python Interactive window that opens preloaded with the current program state. At that point, the Python Interactive window is essentially detached from debugger, but at least has the data from the program at that time, allowing for more native exploring of the variables. Once the user continues debugging, the current Python Interactive window is either updated or a new one is launched at the next breakpoint.

My current work around is just to write a data frame to a .csv in the current debugger, then load it into Python Interactive - which works well enough, but I think it'd be really helpful if they were somehow integrated!

Thank you for making this product so awesome to use!

@IanMatthewHuff
Copy link
Member

@jbeyer16. I like this suggestion, right now there is a bit of an odd bifurcation between the debugging and interactive debugging workflows. We'll consider it in the next triage meeting.

@geophpherie
Copy link
Author

awesome! looking forward to seeing how it develops. thanks!

@alexhamiltonRN
Copy link

Great suggestion @jbeyer16. Thanks for considering @IanMatthewHuff - the described functionality would be a huge improvement for my workflow.

@adamryczkowski
Copy link

@gramster I understand debugging Python inside Jupyter is not easy. Did you check out how the problem was solved in 2018 by PixieDebugger? (https://medium.com/codait/the-visual-python-debugger-for-jupyter-notebooks-youve-always-wanted-761713babc62)

@gramster
Copy link
Member

gramster commented Nov 3, 2019 via email

@mwindowshz
Copy link

Yes this would be a great feature, just like in Spyder
the debug console is limited and not easy to use.
Interactive python is much better

@syagev
Copy link

syagev commented Nov 13, 2019

This is actually extremely useful for data-science oriented work. PyCharm somehow does this seamlessly, when debugging a regular Python project, the "Debug console" is actually an IPython one. That means, for instance, that it can be used for interactive matplotlib plotting even when the project is not executed in jupyter.

I'm interested to understand how something like this would be implemented to see if I can find some "ugly" temporary workaround. So maybe I can start some sort of listener server in my own code , then when stopped on a breakpoint I coudl launch IPython from the terminal and somehow "attach" it to my current session

@netanelrabinowitz
Copy link

@syagev don't know if this is what you want but it's kind of workaround and it's definitely ugly :)
you can launch an embedded IPython REPL right from the DEBUG CONSOLE tab

    import IPython
    IPython.embed()

and interact with it at the TERMINAL tab.

@derekeden
Copy link

Has there been any update on this ? I love VScode but this is the sole reason I don't use it over my current IDE

@sushant-tj
Copy link

I need this feature man.

@derekeden
Copy link

I need this feature man.

same - honestly would love to use VSCode as my main IDE but there's no way I can effectively debug without an interactive debugger

@robfromdublin
Copy link

I didn't know this wasn't possible and just lost my morning trying different configuration options to make it so. This feature is a must have for me. I find the inconsistency between debug and interactive consoles annoying

@fengyanghe
Copy link

Awesome! I am looking forward to a new interactive debug like WingIDE's. Thanks

@bashtage
Copy link

@netanelrabinowitz This is such a useful hint. It would be great if this could be made into an automatic process somehow so that everything a particular debug config was run the terminal would have an interactive IPython session.

@rchiodo
Copy link
Contributor

rchiodo commented Apr 29, 2020

I think the original problem could possibly have been solved by allowing the user to specify the interactive debug settings in the launch.json.

@sedghi
Copy link

sedghi commented Apr 29, 2020

This is absolutely an necessary feature for data science and machine learning projects. can we have it?

@rchiodo
Copy link
Contributor

rchiodo commented Apr 29, 2020

Which 'this' are you referring to? Launch with interactive as an option in the launch.json is likely possible, but connect to a python process with the interactive console is not.

@sedghi
Copy link

sedghi commented Apr 29, 2020

@rchiodo
Sorry if i was not clear, I was referring to a PyCharm-like debug console which is IPython.
I tried all the above answers to have a simple matplotlib plot to show up while debugging, and it didn't work.

@bashtage
Copy link

The ability to drop into an IPython debug console anywhere is a very powerful tool, and is a real USP of PyCharm.

@hoechenberger
Copy link

I would also like to chime in here and say that this is really a big blocker for my department's adoption of VS Code.

@greazer greazer changed the title Enable Python Interactive as a "console" choice in launch.json, or launch Python Interactive with program state on a debug breakpoint DS: Enable Python Interactive for use as a debugging console May 1, 2020
@whong-art
Copy link

Looking for this feature as well

@m-guggenmos
Copy link

m-guggenmos commented Jun 11, 2020

@syagev don't know if this is what you want but it's kind of workaround and it's definitely ugly :)
you can launch an embedded IPython REPL right from the DEBUG CONSOLE tab

    import IPython
    IPython.embed()

and interact with it at the TERMINAL tab.

Meanwhile - is there any way to automate this nice workaround?

Edit: I missed @bashtage's similar comment.

@sedghi
Copy link

sedghi commented Jul 21, 2020

is there any update here?

@rchiodo
Copy link
Contributor

rchiodo commented Jul 21, 2020

This item is kinda ambiguous.

The original request was to support launch.json settings for the interactive window. That might happen. We don't have a definite plan for it though.

The other request was to allow the user to switch to an IPython terminal in the middle of debugging. That will likely never happen. Transferring kernel state (especially from a non kernel runtime) is non trivial and not something we're likely to pursue.

We might allow the user to start the debugger with IPython as a terminal though. Basically automate the workaround as described by @netanelrabinowitz up above.

The status of this issue is waiting for @jmew or @claudiaregio to split it into those 3 parts.

@sedghi
Copy link

sedghi commented Jul 23, 2020

@rchiodo
As I described in the previous comments, a very handy tool that PyCharm has is that it allows to plot in the middle of debugging which is super useful for different areas of image analysis especially in medical imaging.
The IPython as a terminal with a workaround above does not allow for plotting of any kind.

@rchiodo
Copy link
Contributor

rchiodo commented Jul 23, 2020

@sedghi you should be able to use the first option:

  • support launch.json settings for the interactive window.

You'd start the debugger using the interactive window. This should be possible now minus the settings (arguments to pass to the script etc).

@bashtage
Copy link

We might allow the user to start the debugger with IPython as a terminal though. Basically automate the workaround as described by @netanelrabinowitz up above.

I think this is the closest to what PyCharm does. If you have IPython installed, when you drop into the console, you get an IPython session. There is no available to open as a standard Python session and then transform this to an IPython session.

@aksg87
Copy link

aksg87 commented Aug 6, 2021

@JanHomann @jbeyer16

When following the steps of launching: from IPython import embed; embed()

I am not getting a second external window pop up after trying to display my plot. Anything I am missing here?

My steps:

  1. from IPython import embed; embed()
  2. import matplotlib.pyplot as plt
  3. plt.imshow(...) + plt.show()

image

@aksg87
Copy link

aksg87 commented Aug 6, 2021

@Steph71 not sure what you're missing. The IPython console is available when using the interactive window for debugging. Click the 'Debug Cell' code lens over top of your code. All output will be sent to the interactive window.

image

Perhaps you're talking about being able to eval in the ipython console. That's not currently supported (as the kernel is busy with the cell that's running). You can eval in the debug console at the same time though. Output is then duplicated in the interactive window and the debug console:

image

When trying to debug an individual cell, I still can't interrogate data and plot new aspects while debugging the cell. The debug cell does allow me to step through the existing code and see variables states, but I still don't see how I can plot new things dynamically?

@geophpherie
Copy link
Author

@aksg87 I think you might have to create the figure explicitly before plotting.

from IPython import embed; embed()
import matplotlib.pyplot as plt
plt.ion()
plt.figure()
plt.plot([1,2], [3,3])

seems to work for me.

@vintprox
Copy link

In my use case, it's one keybinding away on Jupyter: Debug Current File in Interactive Window command, but I wouldn't like to waste students' time to set these up. Alas, no default is provided.
launch.json only allows old-school terminal with big payload (that one is distracting, for when you press F5).

@greazer greazer added feature-request Request for new features or functionality and removed enhancement labels May 4, 2022
@TheSamFed
Copy link

Just created a bounty on Bountysource for this issue, would really love to see this as a feature:
https://app.bountysource.com/issues/98730134-enable-python-interactive-for-use-as-a-debugging-console

@landisrm
Copy link

It seems like there are a lot of different possibilities discussed here, but one of them seems to be: use IPython in the debug console. I would sure love to see this feature as well. I don't want to debug in a Jupyter type interface. I just want to set a breakpoint and when I hit it, I can use IPython to inspect things. Maybe this is already possible but if so the setup is not well documented.

As others have said, this is a major limitation of using VS Code for Python, esp. data science type tasks

@ddfabbro
Copy link

ddfabbro commented Jun 27, 2022

In my use case, it's one keybinding away on Jupyter: Debug Current File in Interactive Window command, but I wouldn't like to waste students' time to set these up. Alas, no default is provided. launch.json only allows old-school terminal with big payload (that one is distracting, for when you press F5).

How can I keep using the interactive terminal while debugging the current file in the Interactive Window?

image

I have a situation where during a debugging session I would like to interact with the images using matplotlib.

Problems:

  • There is still a long process after that, so I can not wait for the current cell to finish.
  • Also my code base is fairly large, so I can not start splitting it into Jupyter cells.
  • Also my debugging session is running remotely via Remote - SSH plugin for VS Code, so that is my only UI (can not open matplotlib window)

The way I am dealing with this is logging the images to file (.jpg, .png, etc) in an oldschool OpenCV C++ style. That is not interactive at all, and I always need to convert the images to unsigned chars before saving it to .jpg. Matploblib lets me visualize arrays with any datatype as image using plt.imshow

@rchiodo
Copy link
Contributor

rchiodo commented Jun 27, 2022

You simply type your expressions into the debug console instead of the edit box for the interactive window.

Internally the interactive window is listening to the output of a cell running. So we do something like this:

    const request = kernel.requestExecute(`Your code that submitted for the entire cell');
    request.onOutput((o) => this.sendOutputToView(o))

So it won't accept another request (which is what the input box sends).

However the debug console window does something different. There's a separate thread in the kernel that the debugger inserts and it evaluates whatever you type on that thread. So it's not blocked.

However the output from that eval ends up getting sent back to our code as an output.

Here's an example of me printing something from the debug console as I'm debugging a cell:

debuggerconsole

@kleffy
Copy link

kleffy commented Aug 16, 2022

Sorry! is the feature suggested by @jbeyer16 available now?

@kleffy
Copy link

kleffy commented Aug 16, 2022

Actually, I just found out that the feature exist in vs code. see solution from this stackoverflow thread.

@hoechenberger
Copy link

Actually, I just found out that the feature exist in vs code. see solution from this stackoverflow thread.

This is not what this question / feature request is about. @jbeyer16 was not asking for any interactive debugger, but for the debugger to run in what the Python extension calls the Python Interactive console.

@amunger
Copy link
Contributor

amunger commented Dec 7, 2022

After reading through this thread, it sounds like the most feasible and popular solution to this would be

  1. Add the "Debug file in Interactive Window" as a launch configuration to make it easier to access
  2. Allow code from the input box to run while the kernel is paused at a breakpoint

I attempted to run a cell from a separate IW on the same live kernel and that didn't work, so we would likely need to use the separate thread as suggested here

However the debug console window does something different. There's a separate thread in the kernel that the debugger inserts and it evaluates whatever you type on that thread. So it's not blocked.

If that wouldn't provide what you're after, please open a separate issue that can be discussed and voted on.

@roblourens
Copy link
Member

I'm wondering, would that still work for outputs? Yesterday Don said that running code via the debugger does not run in "inside the kernel", and I don't know exactly what he means by that, but maybe outputs are what would be missing.

@rchiodo
Copy link
Contributor

rchiodo commented Dec 8, 2022

As far as I remember, running evals in the debugger thread still causes the output to go to the currently running cell.

You can test this by just evaling something in the debug console while debugging the interactive window.

@TBlackmore
Copy link

Another vote for this feature!

@raphaelchinchilla
Copy link

It has been more than 4 years since this post appeared. Any update on when it is going to be implemented?

One thing that (hopefully) is easy to implement is to have a better support for the %pdb magic command of IPython

@v4lue4dded
Copy link

I just published package that is my attempt at solving this problem.

The idea is to make it easy to save all local and global variables at a specific point during the debug process to disc and then load them again into an interactive window (and even close the interactive window and load them again and again if needed).

https://github.com/v4lue4dded/debugsnap/
https://pypi.org/project/debugsnap/

first install the package into the env you want to debug:

pip install debugsnap

then start a debugging session using the interactive window environment where you want to debug:
image

(this doesn't have a keyboard shortcut by default, Ctrl +Shift + Enter is my recommendation.

Go a breakpoint in the code where you want to save the snapshot and there open the debug consol:

image

  1. Saving the Debugging Snapshot:
    In your debug session, save the current snapshot.
     from debugsnap import save_snapshot
     save_snapshot(local_vars=locals(), global_vars=globals())

** Warning:** By default a new folder named tmp_save_snapshot will be created in your ~ directory and the shapshot files (containing the global variables, local variables and information about any variable that could not be saved for any reason) will be saved there. To change the storage location you have to provide a storage_path.

  1. Loading the Debugging Snapshot:
    In a new Python session or an interactive environment, load the saved snapshot:
     from debugsnap import load_snapshot
     global_vars, local_vars = load_snapshot()
     globals().update(global_vars)
     locals().update(local_vars)

Now you have all the variables that where present at the breakpoint in an interactive window.

PS: follow the links for info on how to make the saving and loading code into VSCode snippets

@b-a0
Copy link

b-a0 commented Feb 22, 2024

We'll consider it in the next triage meeting.

@IanMatthewHuff I understand it's a while ago, but do you remember what the outcome was in the triage meeting?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request Request for new features or functionality interactive-window Impacts interactive window interactive-window-debugging
Projects
None yet
Development

No branches or pull requests