<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">Is it possible to run a program to a breakpoint in IPython and then change functions within that program? This would be for interactively experimenting with program logic without having to restart the program from scratch. I’m running IPython3 from Emacs using Elpy for interacting with the IPython3 process.<div class=""><br class=""></div><div class="">Consider foo.py as follows:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">#!/usr/bin/env ipython3</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">import sys</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">from IPython.core.debugger import Pdb</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">def print_name():</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">    print ("Alice")</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">def name():</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">    print_name()</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">def main(argv):</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">    print ("In main.")</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">    Pdb().set_trace()</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">    name()</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">if __name__ == "__main__":</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">    main(sys.argv[1:])</span></div></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class="">Running it gives me:</span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(47, 180, 29);" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><b class="">ayank@snorri</b></span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">:</span><span style="font-variant-ligatures: no-common-ligatures; color: #400bd9" class=""><b class="">~/Documents</b></span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">$ ./foo.py </span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">In main.</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(47, 180, 29);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">> </span><span style="font-variant-ligatures: no-common-ligatures" class="">/home/ayank/Documents/foo.py</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(16)</span><span style="font-variant-ligatures: no-common-ligatures; color: #2eaebb" class="">main</span><span style="font-variant-ligatures: no-common-ligatures; color: #400bd9" class="">()</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(64, 11, 217);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #2fb41d" class="">     14 </span><span style="font-variant-ligatures: no-common-ligatures; color: #b42419" class="">    </span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">print </span><span style="font-variant-ligatures: no-common-ligatures" class="">("In main.")</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #2fb41d" class="">     15 </span><span style="font-variant-ligatures: no-common-ligatures; color: #b42419" class="">    </span><span style="font-variant-ligatures: no-common-ligatures" class="">Pdb</span><span style="font-variant-ligatures: no-common-ligatures; color: #400bd9" class="">().</span><span style="font-variant-ligatures: no-common-ligatures" class="">set_trace</span><span style="font-variant-ligatures: no-common-ligatures; color: #400bd9" class="">()</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(47, 180, 29);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">---> 16 </span><span style="font-variant-ligatures: no-common-ligatures; color: #b42419" class="">    </span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">name</span><span style="font-variant-ligatures: no-common-ligatures; color: #400bd9" class="">()</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(47, 180, 29);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">     17 </span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(64, 11, 217);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #2fb41d" class="">     18 if</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> __name__ </span><span style="font-variant-ligatures: no-common-ligatures" class="">==</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">"__main__":</span></div></span><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">ipdb> </span></div></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""><span style="font-family: Helvetica; font-size: 12px;" class="">I now want to change the definition of</span><span style="font-family: Menlo; font-size: 11px;" class=""> print_name()</span> to print “Bob” instead of “Alice”. I can make this change in Emacs, send the new function to IPython with <span style="font-family: Menlo; font-size: 11px;" class="">C-c C-y f</span>, but when I then type <span style="font-family: Menlo; font-size: 11px;" class="">name()</span>, I get “Alice”. The reference to <span style="font-family: Menlo; font-size: 11px;" class="">print_name()</span> in <span style="font-family: Menlo; font-size: 11px;" class="">name()</span> is not getting updated to point to the new definition of <span style="font-family: Menlo; font-size: 11px;" class="">print_name</span><font face="Menlo" class=""><span style="font-size: 11px;" class="">(</span>)</font>. Likely I’m going about this process incorrectly, as I can make these kind of changes at an IPython3 prompt but not at an ipdb one.</div><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""><br class=""></div><div style="margin: 0px; font-stretch: normal; line-height: normal;" class="">Thanks,</div><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""><br class=""></div><div style="margin: 0px; font-stretch: normal; line-height: normal;" class="">Andreas</div></body></html>