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).
Related
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.
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 have a pet project which I started as a Jupyter notebook. So far, I put all the Python code in the notebook.
At the start everything was fine. But over time the code I wrote in the notebook became more and more complex. Now it is close to unmanagenable: When I find an error, I need
to navigate to the code part with the error (usually at the beginning of the notebook),
fix the error there,
go to (usually) the bottom of the notebook to trigger the execution of the code I changed.
I want to separate the code in two parts:
One that will be stored as Python files and which I will edit using an editor (and/or an IDE).
The code in the Jupyter notebook that calls code parts 1 and presents its output (i. e. use the Jupyter notebook as a user interface for the Python code from step 1).
Let's assume that the notebook runs on my local machine (Windows 7; Jupyter runs in Anaconda) and the Python files are also stored locally.
What are good ways to use code from IPython files such that I can modify this code frequently and fast?
By "frequently and fast" I mean "with as little steps as possible that are necessary to propagate changes from Python files to the notebook". The ideal solution would be something where I change one of the Python files, run one command, and then the changes are available in the Jupyter notebook. Or, to use an older analogy, I want it to be like PHP -- you change the code often and immediately see the results of your changes.
Update 1: I tried to use the solution with %load TestClass.py in a cell.
The problem is that the cell contents is not updated, if the file changes.
Example:
Let's say I put the text
class TestClass:
def __init__(self):
print("TestClass constructor")
into TestClass.py. Then I create a cell in Jupyter notebook with %load TestClass.py. When I execute that cell, the code from TestClass.py is imported and the line %load TestClass.py gets commented out.
Now I change TestClass.py to
class TestClass:
def __init__(self):
print("TestClass constructor")
print("change")
When I execute the cell, its contents has not changed.
Sounds like the autoload extension of IPython is exactly what you need. Simply plug
%load_ext autoreload
%autoreload 2
in one of the first cells of your Jupyter notebook and imported python modules are automatically reloaded on change. You also can do changes to installed python packages, provided you have installed them editable.
I've been working on a similar sort of problem. If you use Jupyterlab instead of the older Jupyter Notebooks, you can open multiple nb's at the same time, edit the one holding your functions, and then restart the kernel in the other to update when you import. Unfortunately you lose all your variables and need to run the entire notebook again but it allows the changes in the first nb to propogate into the second.
This isn't a perfect answer but has allowed me to keep working on my projects.
You're writing:
and the line %load TestClass.py gets commented out.
I understand, that you comment it out. Why? Re-run the import and the class gets updated.
BTW !run TestClass.py should do the same - at least in Jupyter NB.
I usually unload the modules I want to refresh then reimport. Objects bound to old code don't get refreshed and every now and then I have to restart the kernel but it's a lot faster than restarting the kernel each time. I would only suggest this for someone who already understands or is willing to understand the impact of removing modules from sys.modules. But it saves a lot of time for me.
See UnloadModules here.
My typical usage in Jupyter:
from coppertop import Unload
Unload("fred.joe")
from fred.joe.sally import arthur
arthur.doSomethingInteresting()
I just made the transition from Spyder to VScode for my python endeavours. Is there a way to run individual lines of code? That's how I used to do my on-the-spot debugging, but I can't find an option for it in VScode and really don't want to keep setting and removing breakpoints.
Thanks.
If you highlight some code, you can right-click or run the command, Run Selection/Line in Python Terminal.
We are also planning on implementing Ctrl-Enter to do the same thing and looking at Ctr-Enter executing the current line.
You can:
open a terminal at Terminal>New Terminal
Highlight the code you want to run
Hit Terminal>Run Selected Text
As for R you can hit CTRL Enter to execute the highlighted code. For python there's apparently no default shortcut (see below), but I am quite sure you can add yours.
In my ver of VSCode (1.25), shift+enter will run selection. Note that you will want to have your integrated terminal running python.
One way you can do it is through the Integrated Terminal. Here is the guide to open/use it: https://code.visualstudio.com/docs/editor/integrated-terminal
After that, type python3 or python since it is depending on what version you are using. Then, copy and paste the fraction of code you want to run into the terminal. It now has the same functionality as the console in Spyder. Hope this helps.
I'm still trying to figure out how to make vscode do what I need (interactive python plots), but I can offer a more complete answer to the question at hand than what has been given so far:
1- Evaluate current selection in debug terminal is an option that is not enabled by default, so you may want to bind the 'editor.debug.action.selectionToRepl' action to whatever keyboard shortcut you choose (I'm using F9). As of today, there still appears to be no option to evaluate current line while debugging, only current selection.
2- Evaluate current line or selection in python terminal is enabled by default, but I'm on Windows where this isn't doing what I would expect - it evaluates in a new runtime, which does no good if you're trying to debug an existing runtime. So I can't say much about how useful this option is, or even if it is necessary since anytime you'd want to evaluate line-by-line, you'll be in debug mode anyway and sending to debug console as in 1 above. The Windows issue might have something to do with the settings.json entry
"terminal.integrated.inheritEnv": true,
not having an affect in Windows as of yet, per vscode documentation.
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.