I am working with visual studio code (python) using ssh to access a remote server (where the code is located). I am able to write code, run and debug without any problem. However I am not able to generate matplotlib figures during debug in the same way as i can do it without the ssh connection.
I've tried to follow several suggestions from internet and other post here but none of them is fully working.
I've tried the option of " Jupiter> Debug current file in interactive window " but it does not work. I am not sure why, but the debug in the interactive window is not responding and i can not work during debug.
I've tried python debug and the only matplotlib backend that works is (matplotlib.use("WebAgg"). However for this backend it only works with one figure and the debug is block after the plot.
I have also tried to use X.Org (in my case xquartz over mac) but the figures are really slow and it is not useful.
Any idea on how to plot figures during debug in the same way as in local development?
Thanks!
I asked this question in GitHub and got the following answers:
One way to do this is to do the 'Debug your current file in the interactive window'.
There's a bunch of caveats though.
You need to have ipykernel installed into the python environment
you're using. If you run that command it should have asked you to do
so.
It's likely easier to use if you put cell markers around pieces of
code # %%. This would also allow you to prerun bits of your script
before you need to debug the part that's causing problems. This is
what it looks like when you do that:
When debugging in the interactive window, your code in your script
is running as an IPython cell. If you split it up, it might be
multiple cells or it could be one large cell (for the whole file).
As you step, the cell execution is moved forward one line at a time.
However the execution is paused in between steps. This means
anything you type in the interactive window won't be executed.
Instead you have to run extra code in the 'debugger console'. Shown
below:
This debugger console is where you can run normal matplotlib
commands.
Related
I'm trying to activate the debugging functions that are now available on VSCode for Jupyter Notebooks but I'm having some problems with that.
In my interface I can see the button to 'Run by Line' next to every cell but when I click it nothing happens except the normal execution of the entire cell, but that mode is supposed to execute your cell line by line... I've tried putting some breakpoints even I know it is not necessary in that mode but still nothing.
As you can see here the buttons for debugging are shown a few seconds after hitting the 'Run by Line' button but they do nothing.
As a workaround to not being able to use that useful mode, I tried the standard debug mode of VSCode for Jupyter Notebooks which let you go breakpoint to breakpoint on your cell, the problem (I think it is) is that when I use that what VSCode does is open another tab with just the code of the cell I'm debugging in a temporary .py file, which is not the behaviour I would like to have, because I think it should do that on the Notebook tab itself without any new file...
Here I show you a screenshot of the temporary .py that it's created:
That's just the code of my notebook first cell!
Any help with this? I would like to solve both problems or at least one of them.
I've checked the requisites for the debug modes and make sure I fullfill everyone of them but I'm not so used to use the debug functionalities of VSCode so maybe I'm skipping something.
This is a current bug. It was notified to the dev team here:
https://github.com/microsoft/vscode-jupyter/issues/8258
The bug is caused by vscode using a shortened username. In my case GUILLE~1 instead of Guillermo.
The current solution available is to create a new user in your computer with a maximum length of 8 characters.
I normally use RStudio to do data science work. I load the datasets, and then write and try new code line by line, changing parameters, exploring the data, and executing small chunks at a time.
I'm currently using PyCharm for a project in Python, and it seems to be geared to software development, running and debugging entire scripts from start to finish, which is incredibly slow and frustrating when used to the RStudio mode.
Is it possible to have a similar data science environment to RStudio is it better to change tool to something else?
If you want to run code line-by-line you could try this plugin: https://plugins.jetbrains.com/plugin/11945-python-smart-execute.
If you want to change the default hotkey from "Ctrl+Shift+A" to default RStudio "Ctrl + Enter" you could do that here.
Also, here's a ticket about merging the plugin into PyCharm: https://youtrack.jetbrains.com/issue/PY-38919.
You can enable "Scientific Mode" in PyCharm. In this mode all code is executed inside the same interactive Python Console, all plots and tabular data are shown in a separate panel, there is a separate panel with Documentation. Also it supports code cells execution (code blocks inside *.py file separated by #%%), current line/file execution in Python Console (available in the context menu in the editor).
The full description is available here: https://www.jetbrains.com/help/pycharm/matplotlib-support.html
I am running quite a time consuming python code in PyCharm, where data is initially loaded, then a model is created etc.
I would like to know if it is possible to set a breakpoint at a particular point in the code where the state of the environment will be saved. From there on I want to make edits to the rest of the code, however ideally re-run from the breakpoint I set.
By doing this I would avoid the loading of data, and model training which would be a repeated process.
Is this possible in Python or is there such a functionality in PyCharm?
Thanks
AFAIK and according to this question form 2014, PyCharm does not support hot-swapping code during debug mode. When you set a breakpoint and do some changes in code they will not be applied.
You can however setup a Python propmt in debug console, which will have access to all your variables at the time of current code execution. You can modify their values and check why your code is failing. To do so just click on this icon
yes, you can do check the python code at breakpoints using pycharm debugger
Refer this for how to use it .how to use a debugger in pycharm
You cannot use code modified code in between and then start from that breakpoints using debugger.
little hack to do this, one need to run the python code in debugger mode ie python -i main.py and also need to make sure that there in no interdependence of previous code to the new modified one.
One option I have found is to convert your code to ipython notebook. Put all the heavy-weight/one-time run code in the first few cells and unstable/to be debugged code in the later cells.
Now, you run the first few cells one time, so the output from these cells is stored and readily available always for later cells. Whenever you change code in later cells, you only have to run those cells (unless you modified something in the earlier cells in which case you need to rerun those cells as well).
Pycharm/most python IDEs support running and debugging ipython notebook cells (individually or in groups).
End goal: After running a script regardless of ending perfectly or erroring out, I would like to have my ipython environment revert back to the directory it was in before running the script.
I have successfully used the advice given in the SO post,
how-do-i-change-directory-back-to-my-original-working-directory-with-python.
This works great if the script runs without failing.
Ideas:
1) I have considered wrapping my code in one large try: and except: utilizing the advice in the post mentioned above. As this would surely handle the issue. However, it seems tedious and unnecessary to have to write this into all my scripts.
2) I was thinking a good a solution would be to have ipython automatically run a "revert directory" script after every user ran script, that would change the directory back to the original one. I have looked around a bit to see how one would configure ipython to run a script automatically after every run. From my search so far I have not found this to be an option. I was expecting to find a method to accomplish this through the ipython config file.
3) Another solution that would be easy would be to somehow use the information found by using the line magic command %dhist. As the first result given by this command is the original directory that ipython was opened in. However, other than having the results print from executing the command %dhist... I am not sure how to work with the printed information.
4) I must be not searching the right terms, as I feel like this is probably something that is possible or has been asked. If indeed someone has asked this, please point me in the right direction and I will remove this question promptly.
Relevant information:
Working on os x
Using Jupyter QtConsole 4.3.1
IPython 6.2.1
I just started using jupyter for a python project. I constantly find myself adding an extra cell just to perform some basic try&error debugging. This way I omit the whole code of the cell is being executed but it still doesn't feel like the right way to do it.
Does Jupyter provide something like a static kernel terminal, for example always visible at the bottom of the screen, where I can simply paste code and execute runtime variables?
By the way: I did search but didn't find anything looking for static console, and terminal. Maybe I'm just looking in the wrong direction.
Thanks!
The jupyter console command will provide you with an interpreter environment that you can experiment with code running within the Jupyter environment outside of a notebook.
It's not exactly what you're looking for but may provide a better environment for testing and developing code that you can then paste into the appropriate notebook.