I am currently writing some Python code in a Jupyter notebook and I wonder if there is any possibility to track the execution of statements directly in Jupyter? I know there is this module called "trace": https://docs.python.org/3/library/trace.html#trace.Trace.results But it seems to me that with "trace" it is only possible to show the flow of execution in the console or to export it to a new file. However, I would like to display it directly in Jupyter Notebooks. Is this possible - with or without the mentioned module - and if yes how?
Thanks for your help.
For something along the lines of debugging code, you may be interested in the cell magic %%debug, discussed here to open a debugger in the notebook and use pdb commands. Or PixieDebugger if you need something fancier.
You may also be interested in the variable inspector extension for JupyterLab. There is (old) an animated example of using it here. You can easily try it out by clicking on launch binder badge there and starting a notebook. Then right-click in the open notebook and select Open Variable Inspector from the list. The animation will show you how to drag the tabs to arrange them side by side on your screen. Now as you run code, you'll see the variables updated.
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 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()
When debugging a .py file within pycharms debug mode i can interact using a python prompt when hitting a breakpoint. Is there any way to use the current state of pycharm within a jupyter notebook istead of the python prompt?
It would make debugging quite a bit easier since you could reuse code snippets for debugging purposes.
I couldn't find anything about it, but for me it feels like it could be a thing.
Thanks for any help!
Based on this post, the functionality you are looking for does not exist. The key piece here is that you're trying to import an existing python interpreter into a new IPython kernel. However, from the post linked, you can attach a notebook to an existing IPython kernel with a little bit of work.
A possible solution would be for you to switch to using an IPython interpreter in PyCharm, then attach to that exising kernel in a notebook when needed (as described in the section above).
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.
I usually have 5-10 different ipython notebooks running with associated consoles/kernels. It is hard for me to keep track of which console/kernel belongs to which notebook. For a while I put a comment with the notebook title at the top of each notebook and executed that comment in the kernel. That way I could type the '#' and then up arrow from console and it would show the comment. I think there is probably an easier way to keep track of my consoles.
The below code snippets show what is going on.
Suppose I am running an ipython notebook server, and I create a new notebook with a title called TestA.
Then suppose I run the following commands in the new notebook
%connect_info #prints connection information
%qtconsole
This opens up an ipython terminal console window (kernel might be the proper name) associated with the same notebook.
If I then type the following command in the ipython terminal console
%connect_info
I can determine which notebook is associated with which ipython consoles/kernels, but this requires a bit of work. Is there a command, magic, or macro that I can get the title (if present) of the currently running notebook?
I saw How do I get the current IPython Notebook name
Though the comments indicate this might not work, this solution appears to work on my setup but I wasn't sure if there is a better way to do this.
I decided to reask this question based on a Q&A on the SO meta site: https://meta.stackexchange.com/questions/147027/purposely-re-asking-duplicating-an-old-question
Answer has not changed, by design the kernel does not know what is speaking to it. Like TV news presenter do not have a camera pointed on every person that listen to TV at night.
And as usual the question is what are you trying to accomplish, you might be trying to do something the hard way.