IPython notebook: debugging by stopping the execution at specific point - python

i want to interactively debug code that is deeply hidden in some class.
I know that it is possible open the ipdb using
from IPython.core.debugger import Pdb
Pdb().set_trace()
This work also in ipython (jupyter) notebook.
Another possibility that I find very convenient when I am working in the terminal is to open a shell at a specific point using
from IPython import embed
embed()
and then use ipython directly to interact with variables etc.
However, if I use this in notebook, I get a ipython shell embedded into the notebook, and this shell does not even work well (e.g., if I use print, the kernel blocks).
What I would find most convenient is to interrupt the kernel execution at some predefined point in the code and keep all current variables, so that I can use another cell for debugging and the possibility to, e.g., develop and test new code with the current variables.
Is there some way to achieve this?

Related

how can I configure PyCharm so that running a Python script file is visible for Python Console

I am writing Python scripts in Pycharm with IPython installed. So I can use Python Console in Pycharm to type Python commands and check the immediate output of the codes. However, when I run a script file after pressing 'Run' button (Shift+F10), all the variables and functions are not visible to the Python Console. This is, however, the feature of Spyder, another popular Python IDE. So here is my question: how can I configure Pycharm so that running a Python script file is visible for Python Console? Thanks
You could also run the part of your code you want to test/check in the console by selecting it and then right clicking and clicking on "Execute Selection in Console Alt-Shift-E". That's what I use sometimes when the debugger is not helpful. After running the code (you can also just "run" functions or classes) the console knows the functions and you can use the same features that Spyder has. However, be aware that when you change the code you need to run it in the console once to update the console definitions!
You can not. But you can use pdb (which will break code execution where you need it and you will be able to do the same things, as in the Python Console).
And, which is better and more powerful, you can use PyCharm's debugger. It represents all available variables in tree-like structures and is really handy.

What's the best practice in the workflow of writing and running python files with Vim?

Currently, I write pythons files in Vim, and run it with jupyter qtconsole. The advantage of this way is that I could work with Vim so get all the benefits of Vim.
I could run the python directly in Vim using the pymode plugins, but in this way I cannot see and manipulate the output variables, and the figures are opened in another window which is quite annoying when I have to close them to make Vim responsible again. Compared with this, in jupyter qtconsole I could use %maplotlib inline to display figures elegantly.
However, my current workflow has a big disadvantage that every time I run my python script in qtconsole, and then I edit my python script, it is not so easy to run it again with the modified script. Since the module has been loaded, rerun it will not automatically reload the modified module source. I found no easy way to overcome this drawback. Currently I have to restart the kernel and then reset the path, turn on %matplotlib inline, and %run python-script.py again.
Any one can give me a solution?
I find an answer which solves my problem by using ipython extension autoreload.
%load_ext autoreload
%autoreload 2
Then I do not have to restart kernel any more.

Python code snippet that runs always when a ipython notebook closes

I would like to add a python code to an ipython notebook that will run every time I close the ipython tab. I tried to see if I can set a cell to do it but I had no luck.
Is this possible either using an ipython API or some other hook mechanism?
One option could be using the atexit python module to register an exit handler. This would work if your page in the IPython notebook is actually a python process.

breaking into IPython notebook server at arbitrary line?

I usually do the following trick for debugging, add following snippet to a place where I want to break into IPython shell:
from IPython.terminal import embed
ipshell = embed.InteractiveShellEmbed()
ipshell()
Does anyone know of a way to do something similar, but instead of spawning shell, start an interactive notebook session in browser?
For that to work, you'd have to either
have the thing you're trying to debug already running in your python notebook daemon's control,
or you'd have to have a debugging backend that you can attach to your process started from within your notebook daemon.
Since the second, to my knowledge, doesn't exist (yet), your only option would be to start the program you want to debug from within your notebook.

How can I save / copy classes & functions I've written in the python interpreter?

How can you save functions/ classes you've writing in a python interactive session to a file? Specifically, is there a way in pydev / eclipse's interactive session (on a mac) to do this?
I just started learning python - and am enjoying using the interpreter's interactive session for testing and playing with modules I've written. However, I find myself writing functions in the interpreter, which I think, oh it would be cool to save that to my script files. How do I do this?
I tried:
import pickle
pickle.dump(my_function, open("output.p", "w"))
But it seems to be more of a binary serialization, or at least nothing that I could copy and paste into my code...
Are there ways to see the code behind classes & functions I've defined in the interpreter? And then copy them out of the interpreter?
Update:
Ok, here's what I've learned so far:
I missed the easiest of all - PyDev's interactive session in eclipse allows you to right click and save your session. Still have to remove >>>'s, but gets the job done.
IPython is apparently the way to go to do this.
How to save a Python interactive session? has more details.
The best environment for interactive coding sessions has to be IPython, in my opinion. It's built on and extends the basic Python interpreter with a lot of magic, including history. For example, you can issue the command %logstart to dump all subsequent input to a file, which still needs to be edited afterward before it will be a script, but gives you a lot to work with.
When installing IPython, don't forget pyreadline.
In general, however, it is best to write code in an IDE and then run it. IPython helps here as well. If you write and save the script, then use the IPython "run" command to run it, the entire global namespace of the script will be available for inspection in your IPython session. Additionally, you can use the -d argument to run to trigger the pdb debugger immediately on any unhandled exception.
If you're more of a straightlaced IDE and debugger kind of guy, then the easiest and best lightweight environment has to be PyScripter.
I think the answer is to change your workflow.
What I do is write my functions in an editor (emacs), and then press a key combination (Ctrl-c Ctrl-e) to send the region of text to the (i)python interpreter.
That way I can save the function if I want, and also play with it in an interpreter.
Emacs is central to how I do it, but I'm sure there must be similar approaches with many editors (vim, gedit, etc) and IDEs.
PS. Finding a good editor is crucial when working with Python. The editor must be able to move blocks of code to the left and right easily, or the whitespace issue becomes too onerous.
I dislike typing blocks of code in the python interpreter because it doesn't allow me to shift blocks easily. You'll like Python even more when you find the right editor.
You can setup a python history file which stores everything you type into the interpreter.
Here's how:
http://docs.python.org/tutorial/interactive.html
I think it can't be done.
Python can perform instrospection with the inspect module, but the inspect.getsource function won't work without a source file.

Categories

Resources