This is not a how-to-use it document. Nor is it a reference document. It will point you at quite a bit of reference material for IPython, and some for Pdb, also. It's mostly hints, tips, and suggestions.

1   IPython

Some documentation:

-- The home page for IPython is here --
https://ipython.org/
-- You can find documentation on IPython here --
https://ipython.readthedocs.io/en/stable/
-- Here is a description at Wikipedia --
https://en.wikipedia.org/wiki/IPython

Help:

  • For help on running IPython from the command line, type:

    $ ipython --help
    
  • For help with using IPython interactively, at the IPython interactive prompt, type: quickref.

  • The question mark operator -- For information about and help with any object, type some_object? (help) or some_object?? (more help).

1.1   Working with and using IPython profiles

For help with profiles, do this:

$ ipython help profile
$ ipython profile list -h
$ ipython profile create -h
$ ipython profile locate -h

To create a default profile, do:

$ ipython profile create

To create a profile with a specific name, do:

$ ipython profile create mynewprofile

Then customize your profile by doing the following:

  • Edit the configuration file in the profile control directory under ~/.ipython/. The default profile, for example, is in ~/.ipython/profile_default/. There are lots of comments in that file to help you.

  • Create one or more startup files in the startup subdirectory under the directory that controls your profile, for example:

    • ~/.ipython/profile_default/startup/
    • ~/.ipython/profile_mynewprofile/startup/

    See the README file in that startup subdirectory.

For more information about configuring IPython, see -- https://ipython.readthedocs.io/en/stable/config/index.html

1.2   Hints and tips for IPython

You can store and reload the values of variables across runs of IPython with %store var_name and %store -r var_name. Type %store? for help and more options with this. In order to automatically reload these variables/values each time you start IPython, turn on autorestore in your ipython_config.py in your IPython profile directory:

## Lightweight persistence for python variables.
#
#  Provides the %store magic.

## If True, any %store-d variables will be automatically restored when IPython
#  starts.
#c.StoreMagics.autorestore = False

Use:

  • Use %store? for help on the %store magic command.
  • %store variable1 variable2 to store the current value of variables.
  • Use %store -r variables1 variables2 to (re-)load the stored values of variables.
  • Use %store -r to (re-)load all stored variables.

You can define macros with the %macro magic command.

  • For help with macros, type %macro?.
  • Use %macro n1-n2 n3 to create a macro containing specified lines from history.
  • Use, for example, print(mymacro) to display the contents of a macro.
  • Use %edit mymacro to edit and modify the contents or commands in a macro.
  • Use %store mymacro to store/save your macro for use in a subsequent IPython session.

You can use your favorite editor to edit files and text data from within IPython. For help on this, type %edit? at the IPython interactive prompt. Also see https://ipython.readthedocs.io/en/stable/interactive/tips.html#lightweight-version-control.

You can edit lines of history. This is very useful, for example, for capturing lines of Python code that you have tested at the IPython interactive prompt and pasting them into a Python (.py) file. To do so, first use %history -n or, e.g., %history -n -l 40. The second form gets 40 lines of history from previous IPython sessions, too. Then do, for example: %edit 20/4-20/8 14-22.

1.3   Debugging

There are several ways to do interactive debugging in Python code. Here are several options and alternatives:

  • Run your code inside IPython with the %run -d .... For example, the following starts Pdb sets a breakpoint on line 11 and breaks at the first executable line:

    [ins] In [4]: %run -d -b11 test01.py arg1 arg2 arg3
    
  • Run your code as you normally would, but first put the following in your code where you'd like to break into Pdb:

    import pdb; pdb.set_trace()
    
  • You can drop into IPython itself by adding the following in your code where you want to do so:

    from IPython import embed ; embed()
    

    Then you can inspect variables and use other features of IPython. And, to continue execution, type exit at the IPython prompt.

Here is a helpful link on Python debugging: https://switowski.com/blog/ipython-debugging

And, that leads us to a discussion of Pdb.

2   Pdb

The reference doc for Pdb is in the Python standard library documentation -- https://docs.python.org/3/library/pdb.html

For help while Pdb is running, type the command help (or short form h). You can then ask for help on any of Pdb's commands, for example help break.

2.1   A few hints and suggestions for Pdb

You can type multiple commands on a single line by separating those commands with " ;; " (a double semicolon). For example:

ipdb> c ;; print('item:', item)

You can create an alias and an easy way to execute comples commands with the Pdb alias command. For example:

ipdb> alias show print(item)
ipdb> alias go_and_show c ;; print(item)

You can automatically display a variable or expression if its value has changed when execution stops with the Pdb display command. For example:

ipdb> display item
ipdb> display item.upper()

You can specify a list of commands to be executed when a specific breakpoint is reached with the commands command. For example, the following sets variable count to zero, and then each time breakpoint 1 is reached, it increments count and prints its valure:

ipdb> count = 0
ipdb> commands 1
(com) count += 1
(com) print(f"count: {count}")
(com) end

Type help pdb at the Pdb prompt for more extensive help for Pdb.

If you want to automatically set breakpoints when you enter Pdb, you can put them in a file .pdbrc in your current directory or in your home directory. For example:

# file: .pdbrc
b 26
b 30

Actually, you can put other Pdb commands and almost any Python code in your .pdbrc file.

But, if you run the debugger inside IPython with %run -d ..., you may have to exit and re-start IPython between debugging runs in order to reload .pdbrc. Or, maybe something is mis-configured on my system. I'm don't know.


Published

Category

python

Tags

Contact